March 28, 2013 at 2:05 pm
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
March 28, 2013 at 2:16 pm
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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
March 28, 2013 at 2:24 pm
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 |
+-------------+--------+--------+
March 28, 2013 at 2:28 pm
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
March 28, 2013 at 2:32 pm
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 |
+--------+-----------+
March 28, 2013 at 2:32 pm
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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
March 28, 2013 at 3:03 pm
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.
🙁
March 28, 2013 at 3:12 pm
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
March 28, 2013 at 3:20 pm
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.
March 28, 2013 at 3:27 pm
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 |
+--------+--------+
March 28, 2013 at 3:37 pm
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
March 28, 2013 at 3:55 pm
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 |
+--------+--------+
March 28, 2013 at 4:08 pm
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.
March 28, 2013 at 5:34 pm
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?
March 30, 2013 at 1:15 pm
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
Change is inevitable... Change for the better is not.
Viewing 15 posts - 1 through 15 (of 17 total)
You must be logged in to reply to this topic. Login to reply