SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Query join


Query join

Author
Message
mrivero1961
mrivero1961
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
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


Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26002 Visits: 17528
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 Modens 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)
mrivero1961
mrivero1961
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
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 |
+-------------+--------+--------+


Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26002 Visits: 17528
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. :-P

_______________________________________________________________

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 Modens 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)
mrivero1961
mrivero1961
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
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. :-P

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 |
+--------+-----------+


Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26002 Visits: 17528
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 Modens 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)
mrivero1961
mrivero1961
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
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.


Sad
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26002 Visits: 17528
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.


Sad


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 Modens 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)
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39217 Visits: 38529
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.

Cool
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)
mrivero1961
mrivero1961
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
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.


Sad


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 |
+--------+--------+


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search