Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Query join Expand / Collapse
Author
Message
Posted Thursday, March 28, 2013 2:05 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, May 29, 2013 9:25 AM
Points: 14, Visits: 25
Hi there, hope in your help.

In my DB I've two tables: TABLE_LONG and TABLE_SHORT.
The two tables are equal but in TABLE_LONG a recording long events and in TABLE_SHORT a recording short events.

If count number of long events, I've this output:
SELECT
COALESCE (idDGIG, 'Tot') AS sGIG,
`NUMBER`
FROM
(
SELECT
LEFT (idDGIG, 2) AS idDGIG,
COUNT(idDGIG) AS NUMBER
FROM
TABLE_LONG
WHERE
1
AND (
LEFT (idDGIG, 2) LIKE '%QM%'
OR LEFT (idDGIG, 2) LIKE '%QI%'
OR LEFT (idDGIG, 2) LIKE '%QO%'
OR LEFT (idDGIG, 2) LIKE '%QS%'
)
AND DATE_START = DATE_ADD(CURDATE(), INTERVAL - 1 DAY)
GROUP BY
LEFT (idDGIG, 2) WITH ROLLUP
) AS Q;
+--------+--------+
| sGIG | NUMBER |
+--------+--------+
| QI | 9 |
| QM | 2 |
| QO | 6 |
| QS | 5 |
| Tot | 22 |
+--------+--------+
5 rows in set

If count number of short events, I've this output:
SELECT
COALESCE (idDGIG, 'Tot') AS sGIG,
NUMBER
FROM
(
SELECT
LEFT (idDGIG, 2) AS sGIG,
COUNT(idDGIG) AS NUMBER
FROM
TABLE_SHORT
WHERE
1
AND (
LEFT (idDGIG, 2) LIKE '%QM%'
OR LEFT (idDGIG, 2) LIKE '%QI%'
OR LEFT (idDGIG, 2) LIKE '%QO%'
OR LEFT (idDGIG, 2) LIKE '%QS%'
)
AND DATE_START = DATE_ADD(CURDATE(), INTERVAL - 1 DAY)
GROUP BY
LEFT (idDGIG, 2) WITH ROLLUP
) AS Z;
+--------+-----------+
| sGIG | NUMBER |
+--------+-----------+
| QI | 2 |
| QM | 2 |
| QO | 16 |
| QS | 6 |
| Tot | 26 |
+--------+-----------+
5 rows in set

Now I need tried join two tables with this query; I think in output total events ( long + short ): 22+26 = 48.
Instead I've this wrong output (1144), why?
Can you help me.
Thanks in advance.

SELECT
DATE_START,
COALESCE (idDGIG, 'Tot') AS sGIG,
`NUMBER`
FROM
(
SELECT
CA.DATE_START AS DATE_START,
LEFT (CA.idDGIG, 2) AS sGIG,
COUNT(CA.idDGIG) + COUNT(A.EVENT) AS NUMBER
FROM
TABLE_LONG CA
JOIN TABLE_SHORT A ON CA.DATE_START = A.DATE_START
WHERE
CA.DATE_START = DATE_ADD(CURDATE(), INTERVAL - 1 DAY)
AND (
LEFT (CA.idDGIG, 2) LIKE '%QM%'
OR LEFT (CA.idDGIG, 2) LIKE '%QI%'
OR LEFT (CA.idDGIG, 2) LIKE '%QO%'
OR LEFT (CA.idDGIG, 2) LIKE '%QS%'
)
GROUP BY
LEFT (CA.idDGIG, 2) WITH ROLLUP
) AS SSS;
+-------------+--------+--------+
| DATE_START | sGIG | NUMBER |
+-------------+--------+--------+
| 2013-03-27 | QI | 468 |
| 2013-03-27 | QM | 104 |
| 2013-03-27 | QO | 312 |
| 2013-03-27 | QS | 260 |
| 2013-03-27 | Tot | 1144 |
+-------------+--------+--------+
5 rows in set

Post #1436719
Posted Thursday, March 28, 2013 2:16 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:16 PM
Points: 13,301, Visits: 12,159
The queries you posted have some stuff missing.

In all cases you don't need to get the left 2 characters and then use like '%%'. The left can't possibly return anymore than 2 characters. Those could be greatly simplified to

LEFT (idDGIG, 2) in ('QM', 'QI', 'QO', 'QS')

Here is a snippet from your first query to show you what I mean about stuff missing.

SELECT
LEFT (idDGIG, 2) AS idDGIG,
COUNT(idDGIG) AS NUMBER
FROM
TABLE_LONG
WHERE
1
AND (
LEFT (idDGIG, 2) LIKE '%QM%'
OR LEFT (idDGIG, 2) LIKE '%QI%'
OR LEFT (idDGIG, 2) LIKE '%QO%'
OR LEFT (idDGIG, 2) LIKE '%QS%'
)
AND DATE_START = DATE_ADD(CURDATE(), INTERVAL - 1 DAY)

You have Where 1 and...

this doesn't work.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1436727
Posted Thursday, March 28, 2013 2:24 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, May 29, 2013 9:25 AM
Points: 14, Visits: 25
Thank you for help.
I've modified the query, but the output not change:

SELECT
DATE_START,
COALESCE (idDGIG, 'Tot') AS sGIG,
`NUMBER`
FROM
(
SELECT
CA.DATE_START AS DATE_START,
LEFT (CA.idDGIG, 2) AS sGIG,
COUNT(CA.idDGIG) + COUNT(A.EVENT) AS NUMBER
FROM
TABLE_LONG CA
JOIN TABLE_SHORT A ON CA.DATE_START = A.DATE_START
WHERE
CA.DATE_START = DATE_ADD(CURDATE(), INTERVAL - 1 DAY)
AND LEFT (CA.idDGIG, 2) IN ('QM', 'QI', 'QO', 'QS')
GROUP BY
LEFT (CA.idDGIG, 2) WITH ROLLUP
) AS SSS;
+-------------+--------+--------+
| DATE_START | sGIG | NUMBER |
+-------------+--------+--------+
| 2013-03-27 | QI | 468 |
| 2013-03-27 | QM | 104 |
| 2013-03-27 | QO | 312 |
| 2013-03-27 | QS | 260 |
| 2013-03-27 | Tot | 1144 |
+-------------+--------+--------+

Post #1436734
Posted Thursday, March 28, 2013 2:28 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:16 PM
Points: 13,301, Visits: 12,159
mrivero1961 (3/28/2013)
Thank you for help.
I've modified the query, but the output not change:

SELECT
DATE_START,
COALESCE (idDGIG, 'Tot') AS sGIG,
`NUMBER`
FROM
(
SELECT
CA.DATE_START AS DATE_START,
LEFT (CA.idDGIG, 2) AS sGIG,
COUNT(CA.idDGIG) + COUNT(A.EVENT) AS NUMBER
FROM
TABLE_LONG CA
JOIN TABLE_SHORT A ON CA.DATE_START = A.DATE_START
WHERE
CA.DATE_START = DATE_ADD(CURDATE(), INTERVAL - 1 DAY)
AND LEFT (CA.idDGIG, 2) IN ('QM', 'QI', 'QO', 'QS')
GROUP BY
LEFT (CA.idDGIG, 2) WITH ROLLUP
) AS SSS;
+-------------+--------+--------+
| DATE_START | sGIG | NUMBER |
+-------------+--------+--------+
| 2013-03-27 | QI | 468 |
| 2013-03-27 | QM | 104 |
| 2013-03-27 | QO | 312 |
| 2013-03-27 | QS | 260 |
| 2013-03-27 | Tot | 1144 |
+-------------+--------+--------+



It wasn't supposed to change the output. It is however a lot easier to read. It did provide a query that will actually work though.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1436737
Posted Thursday, March 28, 2013 2:32 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, May 29, 2013 9:25 AM
Points: 14, Visits: 25
Sean Lange (3/28/2013)
mrivero1961 (3/28/2013)
Thank you for help.
I've modified the query, but the output not change:

SELECT
DATE_START,
COALESCE (idDGIG, 'Tot') AS sGIG,
`NUMBER`
FROM
(
SELECT
CA.DATE_START AS DATE_START,
LEFT (CA.idDGIG, 2) AS sGIG,
COUNT(CA.idDGIG) + COUNT(A.EVENT) AS NUMBER
FROM
TABLE_LONG CA
JOIN TABLE_SHORT A ON CA.DATE_START = A.DATE_START
WHERE
CA.DATE_START = DATE_ADD(CURDATE(), INTERVAL - 1 DAY)
AND LEFT (CA.idDGIG, 2) IN ('QM', 'QI', 'QO', 'QS')
GROUP BY
LEFT (CA.idDGIG, 2) WITH ROLLUP
) AS SSS;
+-------------+--------+--------+
| DATE_START | sGIG | NUMBER |
+-------------+--------+--------+
| 2013-03-27 | QI | 468 |
| 2013-03-27 | QM | 104 |
| 2013-03-27 | QO | 312 |
| 2013-03-27 | QS | 260 |
| 2013-03-27 | Tot | 1144 |
+-------------+--------+--------+



It wasn't supposed to change the output. It is however a lot easier to read. It did provide a query that will actually work though.

thank you, but I don't understand because when join the two tables the outup is wrong:
+-------------+--------+--------+
| DATE_START | sGIG | NUMBER |
+-------------+--------+--------+
| 2013-03-27 | QI | 468 |
| 2013-03-27 | QM | 104 |
| 2013-03-27 | QO | 312 |
| 2013-03-27 | QS | 260 |
| 2013-03-27 | Tot | 1144 |
+-------------+--------+--------+

TABLE_LONG
+--------+--------+
| sGIG | NUMBER |
+--------+--------+
| QI | 9 |
| QM | 2 |
| QO | 6 |
| QS | 5 |
| Tot | 22 |
+--------+--------+

TABLE_SHORT
+--------+-----------+
| sGIG | NUMBER |
+--------+-----------+
| QI | 2 |
| QM | 2 |
| QO | 16 |
| QS | 6 |
| Tot | 26 |
+--------+-----------+

Post #1436741
Posted Thursday, March 28, 2013 2:32 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:16 PM
Points: 13,301, Visits: 12,159
This is MySql. If you want to join multiple queries together you probably need to use UNION.

I really can't tell from what you have posted what the actual issue is here. I can try to help but without ddl (create table statements) and sample data (insert statements) it is pretty hard to figure out what is going on.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1436742
Posted Thursday, March 28, 2013 3:03 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, May 29, 2013 9:25 AM
Points: 14, Visits: 25
Sean Lange (3/28/2013)
This is MySql. If you want to join multiple queries together you probably need to use UNION.

I really can't tell from what you have posted what the actual issue is here. I can try to help but without ddl (create table statements) and sample data (insert statements) it is pretty hard to figure out what is going on.


Post #1436754
Posted Thursday, March 28, 2013 3:12 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:16 PM
Points: 13,301, Visits: 12,159
mrivero1961 (3/28/2013)
Sean Lange (3/28/2013)
This is MySql. If you want to join multiple queries together you probably need to use UNION.

I really can't tell from what you have posted what the actual issue is here. I can try to help but without ddl (create table statements) and sample data (insert statements) it is pretty hard to figure out what is going on.




I just don't know what you are trying to do here. You talk about joining the tables but it seems like you really want a union. do you want to get the results for both queries?


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1436757
Posted Thursday, March 28, 2013 3:20 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 8:19 PM
Points: 23,286, Visits: 32,012
Here is your join (slightly modified by me):


FROM
TABLE_LONG CA
INNER JOIN TABLE_SHORT A
ON CA.DATE_START = A.DATE_START


Look closely at your join condition. For each date, you are going to get a cartesian product. If TABLE_LONG has 5 records for 2013-03-27 and TABLE_SHORT has 8 records for the same date you will get 40 records for 2013-03-27 in the resultant table created by the join. What you need to do is UNION the base records you need for the query and do your aggregation there.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1436758
Posted Thursday, March 28, 2013 3:27 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, May 29, 2013 9:25 AM
Points: 14, Visits: 25
mrivero1961 (3/28/2013)
Sean Lange (3/28/2013)
This is MySql. If you want to join multiple queries together you probably need to use UNION.

I really can't tell from what you have posted what the actual issue is here. I can try to help but without ddl (create table statements) and sample data (insert statements) it is pretty hard to figure out what is going on.




I need join the TABLE_LONG and TABLE_SHORT for in output the sum of number of events.

TABLE_SHORT
+--------+-----------+
| sGIG | NUMBER |
+--------+-----------+
| QI | 2 |
| QM | 2 |
| QO | 16 |
| QS | 6 |
| Tot | 26 |
+--------+-----------+

TABLE_LONG
+--------+--------+
| sGIG | NUMBER |
+--------+--------+
| QI | 9 |
| QM | 2 |
| QO | 6 |
| QS | 5 |
| Tot | 22 |
+--------+--------+

output:
+--------+--------+
| sGIG | NUMBER |
+--------+--------+
| QI | 11 |
| QM | 4 |
| QO | 22 |
| QS | 11 |
| Tot | 48 |
+--------+--------+

Post #1436762
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse