我有以下3个SQL查询,如下所示:
1- SELECT Player FROM ShotTarget Group By Player
2- SELECT COUNT( Target ) FROM ShotTarget Where Target = 'OnTarget' Group By Player
3- SELECT COUNT( TARGET ) FROM ShotTarget Group By Player
如何将这3个查询全部放在一起?我曾经尝试过工会和工会,但我无法让它发挥作用。
非常感谢帮助。
答案 0 :(得分:3)
这就是select
子句的用途。你可以在那里有多个列。
SELECT Player, COUNT( TARGET ),
SUM(case when Target = 'OnTarget' then 1 else 0 end)
FROM ShotTarget
Group By Player;
棘手的是Target = 'OnTarget'
。这使用条件聚合(聚合函数中的case
语句)。
答案 1 :(得分:2)
基本上,你试图获取每个玩家的统计数据,这推断出group by
条款。可以通过一些CASE
技巧来实现目标射击和其他射击之间的区别:
SELECT player,
COUNT (target) AS total_shots
COUNT (CASE target WHEN 'OnTarget' THEN 1 ELSE NULL END) AS shots_on_target
FROM ShotTarget
GROUP BY player
注意:COUNT
会忽略NULL
个值 - 这是shots_on_target
计算的基础。
答案 2 :(得分:0)
SELECT p.player, COUNT(st.target)
FROM player p
JOIN ShotTarget st ON st.player = p.player
WHERE st.Target = 'OnTarget'
GROUP BY p.player