Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
SQL gurus: Single SQL query from 3 tables and conditions?
What are alternatives to get result in single query from following tables and conditions:
table_1:
id_name ... name
1 ......__.......bob
2 ......__.......sam
3 ......__.......jim
4 ......__.......sally
5 ......__.......kelly
6 ......__.......tom
table_2
id_s . succ . id_name .... date
1 ....... 15 .......1 ....... 2011-02-20
2 ....... 26 .......5 ....... 2011-02-21
3 ....... 45 .......4 ....... 2011-02-22
4 ....... 85 .......1 ....... 2011-02-23
5 ....... 11 .......2 ....... 2011-02-24
6 ....... 34 .......6 ....... 2011-02-25
7 ....... 16 .......1 ....... 2011-02-26
8 ....... 70 .......3 ....... 2011-02-27
9 ....... 52 .......5 ....... 2011-03-01
10 ..... 46 .......2 ....... 2011-03-02
11 ....... 5 .......1 ....... 2011-03-05
12 ..... 19 .......3 ....... 2011-03-06
13 ..... 28 .......4 ....... 2011-03-08
14 ..... 56 .......6 ....... 2011-03-09
table_3
id_f . fail . id_name . date
1 ..... 10 .... 3 ..... 2011-02-20
2 ...... 4 ..... 6 ..... 2011-02-21
3 ...... 6 ..... 4 ..... 2011-02-22
4 ..... 12 .... 2 ..... 2011-02-22
5 ..... 17 .... 5 ..... 2011-02-23
6 ..... 24 .... 3 ..... 2011-02-24
7 ..... 13 .... 4 ..... 2011-02-25
8 ..... 31 .... 1 ..... 2011-02-25
9 ..... 29 .... 2 ..... 2011-02-26
10 ... 15 .... 5 ..... 2011-02-26
11 .... 3 ..... 1 ..... 2011-02-27
12 .... 1 ..... 2 ..... 2011-02-28
13 .... 4 ..... 4 ..... 2011-03-01
14 .... 8 ..... 3 ..... 2011-03-01
15 .... 5 ..... 2 ..... 2011-03-02
16 .... 6 ..... 5 ..... 2011-03-02
17 .... 4 ..... 4 ..... 2011-03-03
18 .. 15 ..... 4 ..... 2011-03-04
19 .... 2 ..... 3 ..... 2011-03-05
20 .. 18 ..... 5 ..... 2011-03-05
21 .. 22 ..... 1 ..... 2011-03-06
22 .... 5 ..... 4 ..... 2011-03-07
23 .... 1 ..... 2 ..... 2011-03-08
24 .. 19 ..... 6 ..... 2011-03-09
Report conditions:
Fields: Name _ Sum(succ) _ Sum(fail) _ Count(succ) _ Count(fail)
Period: date>=2011-02-22 & date<=2011-03-05
SortByName
Report result:
name Sum(succ) Sum(fail) Count(succ) Count(fail)
bob ........ 106 .......... 34 .......... 3 ............ 2
jim ........... 70 .......... 34 .......... 1 ............ 3
kelly ........ 52 ........... 56 ......... 1 ............ 4
sally ........ 45 ........... 42 ......... 1 ............ 5
sam ......... 57 ........... 47 ......... 2 ........... 4
tom ......... 34 ............. 0 ......... 1 ............ 0
Thanks in advance.
Actually I have more than million rows, and looks like MySQL does my query forever (creating tmp table).. I've waited for 1 hour with no output..
Robert D, Can you actually write full SQL script for your answer? Because in current form it's a bit useless..
2 Answers
- ?Lv 41 decade agoFavorite Answer
You would use INNER JOIN to join the tables at the forign keys.
so something like
Select table_1.name, sum(table_2.succ), sum(table_3.fail), count(table_2.succ), count(table_3.fail) from table_1 inner join table_2 on table_1.id_name = table_2.id_name inner join table_3 on table_1.id_name = table_3.id_name where table_2.date between 2011-02-22 and 2011-02-05 sortby table_1.name
that should work, or at least give you a good start.
- Anonymous1 decade ago
Use INNER Joins or you will end up with all possible combinations.