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


Query join


Query join

Author
Message
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)

Group: General Forum Members
Points: 38989 Visits: 38508
Start with something like this:



select
idDGIG,
count(idDGIG) as Number
FROM (
SELECT
LEFT (idDGIG, 2) AS idDGIG,
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)
UNION ALL
SELECT
LEFT (idDGIG, 2) AS idDGIG,
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
idDGIG




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
Lynn Pettis (3/28/2013)
Start with something like this:



select
idDGIG,
count(idDGIG) as Number
FROM (
SELECT
LEFT (idDGIG, 2) AS idDGIG,
FROM
TABLE_LONG
WHERE
LEFT (CA.idDGIG, 2) IN ('QM', 'QI', 'QO', 'QS')
AND DATE_START = DATE_ADD(CURDATE(), INTERVAL - 1 DAY)
UNION ALL
SELECT
LEFT (idDGIG, 2) AS idDGIG,
FROM
TABLE_SHORT
WHERE
LEFT (CA.idDGIG, 2) IN ('QM', 'QI', 'QO', 'QS')
AND DATE_START = DATE_ADD(CURDATE(), INTERVAL - 1 DAY))
GROUP BY
idDGIG





thank you, but:


SELECT
idDGIG,
count(idDGIG) as Number
FROM (
SELECT
LEFT (idDGIG, 2) AS idDGIG,
FROM
TABLE_LONG
WHERE
LEFT (CA.idDGIG, 2) IN ('QM', 'QI', 'QO', 'QS')
AND DATE_START = DATE_ADD(CURDATE(), INTERVAL - 1 DAY)
UNION ALL
SELECT
LEFT (idDGIG, 2) AS idDGIG,
FROM
TABLE_SHORT
WHERE
LEFT (CA.idDGIG, 2) IN ('QM', 'QI', 'QO', 'QS')
AND DATE_START = DATE_ADD(CURDATE(), INTERVAL - 1 DAY))
GROUP BY
idDGIG
) x;

+--------+--------+
| sGIG | number |
+--------+--------+
| QO | 5 |
+--------+--------+



Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)

Group: General Forum Members
Points: 38989 Visits: 38508
mrivero1961 (3/28/2013)
Lynn Pettis (3/28/2013)
Start with something like this:



select
idDGIG,
count(idDGIG) as Number
FROM (
SELECT
LEFT (idDGIG, 2) AS idDGIG,
FROM
TABLE_LONG
WHERE
LEFT (CA.idDGIG, 2) IN ('QM', 'QI', 'QO', 'QS')
AND DATE_START = DATE_ADD(CURDATE(), INTERVAL - 1 DAY)
UNION ALL
SELECT
LEFT (idDGIG, 2) AS idDGIG,
FROM
TABLE_SHORT
WHERE
LEFT (CA.idDGIG, 2) IN ('QM', 'QI', 'QO', 'QS')
AND DATE_START = DATE_ADD(CURDATE(), INTERVAL - 1 DAY))
GROUP BY
idDGIG





thank you, but:


SELECT
idDGIG,
count(idDGIG) as Number
FROM (
SELECT
LEFT (idDGIG, 2) AS idDGIG,
FROM
TABLE_LONG
WHERE
LEFT (CA.idDGIG, 2) IN ('QM', 'QI', 'QO', 'QS')
AND DATE_START = DATE_ADD(CURDATE(), INTERVAL - 1 DAY)
UNION ALL
SELECT
LEFT (idDGIG, 2) AS idDGIG,
FROM
TABLE_SHORT
WHERE
LEFT (CA.idDGIG, 2) IN ('QM', 'QI', 'QO', 'QS')
AND DATE_START = DATE_ADD(CURDATE(), INTERVAL - 1 DAY))
GROUP BY
idDGIG
) x;

+--------+--------+
| sGIG | number |
+--------+--------+
| QO | 5 |
+--------+--------+




You got the best I can give you since you have not provided anything usable for us to work with, no DDL (CREATE TABLE statement(s)) for the table(s) involved, no sample data in the form of INSERT INTO statements for the table(s). In addition, you are asking for MySQL answer on a Microsoft SQL Server forum.

You want better answers? You have to either help us help you or go find a MySQL forum where people will more than likely have a better chance of helping you because they will be more familiar with the product. The best we can do IF you give us what we need is a Standard SQL (not MySQL or T-SQL) answer.

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)
Jan Van der Eecken
Jan Van der Eecken
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2426 Visits: 6494
What Lynn has been trying to ask you so we can answer your question is the following:

1) What do your tables TABLE_LONG and TABLE_SHORT look like, i.e. the Data Definition Language (DDL) statements to create the tables.
2) Give us some input data that represents what you may encounter in your own data set (no confidential stuff please)
3) Give us the expected results given the above two, plus most importantly
4) Given the above, how do you get from point 2 to point 3, in other words, what is the exact algorithm to get there. That's what we call a "Business Specification".

Just one other thing, make sure that your test data (in point 2) represents all kinds of permutations you have to cater for, including edge cases. Think about your business cases so you don't leave any weird things that might happen but are "unlikely" out, because given Murphy's Law, they will bite you in the A at some point in time, trust me, I'm experienced enough to know.

So, after lots of talking, I'll help you along with points one and two:

1) DDL statement example


CREATE TABLE TABLE_LONG
(
DATE_START DATE NOT NULL,
idDGIG VARCHAR ( 10 ) NOT NULL
)



and whatever columns you have, with the correct datatypes, for all the tables that are required to solve your issue.

2) Test Data example


INSERT INTO TABLE_LONG ( DATE_START, idDGIG )
SELECT '2013-01-01', 'QMThisIsQM1' UNION ALL
SELECT '2013-01-17', 'QIThisIsQ1' UNION ALL
SELECT '2012-12-13', 'QOMaybe' UNION ALL
SELECT '2016-09-16', 'QS'



And that for all the tables you have in your problem

Points 3 and 4 you have to really supply by yourself.

BTW, I'm not meaning to offend you in any way, but you need to help us in order to help you. Guessing on our side doesn't get anyone anywhere. So please?

--------------------------------------------------------------------------
A little knowledge is a dangerous thing (Alexander Pope)

In order for us to help you as efficiently as possible, please read this before posting (courtesy of Jeff Moden)
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)

Group: General Forum Members
Points: 84885 Visits: 41069
mrivero1961 (3/28/2013)
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 |
+--------+--------+



We keep going round'n'round because we don't actually know what your data looks like. Please see the article at the first link in my signature below for how to help us help you get the best answer. Thanks.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
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
Now working, thank you.
SELECT Coalesce( sGIG, 'Tot' ) as sGIG, Sum(Number) as Num
FROM (
SELECT LEFT (idDGIG, 2) AS sGIG, COUNT(idDGIG) AS NUMBER
FROM TABLE_LONG
WHERE
LEFT (idDGIG, 2) IN ('QM', 'QI', 'QO', 'QS')
AND DATE_START = DATE_ADD(CURDATE(), INTERVAL - 1 DAY)
GROUP BY sGIG

UNION ALL

SELECT LEFT (idDGIG, 2) AS sGIG, COUNT(idDGIG) AS NUMBER
FROM TABLE_SHORT
WHERE
LEFT (idDGIG, 2) IN ('QM', 'QI', 'QO', 'QS')
AND DATE_START = DATE_ADD(CURDATE(), INTERVAL - 1 DAY)
GROUP BY sGIG
) AS sums
Group by sGIG WITH ROLLUP;

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



Jan Van der Eecken
Jan Van der Eecken
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2426 Visits: 6494
Great to hear you got it working. But please remember next time around you ask a question to please provide us with enough info to actually help you. So please read through Jeff's first link in his signature. We can't see what you see on your side, and without that info we're just taking a guess, probably wasting your time and ours as well. Anyhow, welcome to SSC!

--------------------------------------------------------------------------
A little knowledge is a dangerous thing (Alexander Pope)

In order for us to help you as efficiently as possible, please read this before posting (courtesy of Jeff Moden)
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
Jan Van der Eecken (3/31/2013)
Great to hear you got it working. But please remember next time around you ask a question to please provide us with enough info to actually help you. So please read through Jeff's first link in his signature. We can't see what you see on your side, and without that info we're just taking a guess, probably wasting your time and ours as well. Anyhow, welcome to SSC!



Of course !
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