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 3:37 PM


SSC-Insane

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

Group: General Forum Members
Last Login: Yesterday @ 9:33 PM
Points: 23,277, Visits: 31,997
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





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 #1436764
Posted Thursday, March 28, 2013 3:55 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, May 29, 2013 9:25 AM
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 |
+--------+--------+


Post #1436771
Posted Thursday, March 28, 2013 4:08 PM


SSC-Insane

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

Group: General Forum Members
Last Login: Yesterday @ 9:33 PM
Points: 23,277, Visits: 31,997
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.



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 #1436778
Posted Thursday, March 28, 2013 5:34 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, August 13, 2014 4:58 AM
Points: 2,467, Visits: 6,437
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)
Post #1436799
Posted Saturday, March 30, 2013 1:15 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 1:13 AM
Points: 36,978, Visits: 31,499
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.




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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1437230
Posted Sunday, March 31, 2013 3:22 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, May 29, 2013 9:25 AM
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 |
+--------+--------+


Post #1437284
Posted Sunday, March 31, 2013 5:46 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, August 13, 2014 4:58 AM
Points: 2,467, Visits: 6,437
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)
Post #1437285
Posted Sunday, March 31, 2013 12:50 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, May 29, 2013 9:25 AM
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 !
Post #1437313
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse