[英]Combining the separate queries from multiple tables
Table1表格1
ID | First | Last | Branch
111 Amy A 10
222 Yuki B 11
333 Smith C 10
444 David D 12
555 Mike E 13
Table2表2
Branch | Food | State
10 Burger MI
11 Taco CA
99 Taco CA
13 Taco CA
12 Burger MI
Table3表3
Food | State | Rating
Burger MI bad
Taco CA bad
Steak TX good
QUESTION: How to make the table below:问题:如何制作下表:
Rating | CountState | CountID
Bad 2 5
Good 1 0
I am a novice to SQL, so I tried my best to deliver my thoughts into this question.我是 SQL 的新手,所以我尽力将我的想法传达给这个问题。 I am thinking of UNION ALL of two queries but failed.我正在考虑 UNION ALL 的两个查询,但失败了。 Any idea is appreciated.任何想法表示赞赏。 Thank you very much.非常感谢你。
SELECT a.Rating, COUNT(a.Rating) AS CountState
FROM Table1 a
GROUP BY a.Rating
ORDER BY a.Rating
Looking at your data and what you require for your sample output, it appears you just need a count of Table1 IDs and a distinct count of Table3 States - if that's not correct you need to clarify your relationships and the data better.查看您的数据以及您对示例输出的要求,似乎您只需要一个 Table1 ID 的计数和一个 Table3 状态的不同计数 - 如果这不正确,您需要更好地阐明您的关系和数据。
The following should give you your desired results以下应该给你你想要的结果
select t3.rating,
count(distinct t3.state) CountState,
Count(t1.Id) CountId
from t3
left join t2 on t2.state=t3.state
left join t1 on t1.branch=t2.branch
group by t3.rating
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.