我有两个看起来像这样的表:
人:
1 Andy Relative
2 Jim Friend
3 Anderson Friend
4 Pamela Relative
喜欢:
1 Pizza
1 Soda
2 Pizza
3 Soda
4 Pizza
4 Soda
我需要多个条件的多个计数。如果它只是我正在寻找的一个字符串值,我可以计算(Case ...)但我需要包含这个查询,每个id查找两个字符串:
代码计算比萨饼和苏打水:
select relation, count(*)
from People p
join likes l1 on l1.id = p.id and pizzavalue = 'Pizza'
join likes l2 on l2.id = p.id and pizzavalue = 'Soda'
group by relation
这给了我:
Relative 2
Friend 0
然后我需要加入其他列,这些列也按计数关系分组,例如只计算苏打水和披萨。这是预期的结果:
Relation | Pizza and Soda | Pizza Only | Soda Only
Relative 2 2 2
Friend 0 1 1
答案 0 :(得分:2)
您可以使用pivot
select *
from
(
select
distinct
p.id,
relation,
(select likes + ' ' from @likes l2 where l2.id=l1.id order by likes for xml path('') ) as likes
from @likes l1
inner join @people p on l1.id = p.id
) s
pivot
(count(id) for likes in ([pizza],[soda],[pizza soda])) p
答案 1 :(得分:1)
您可能需要稍微调整连接以获得所需的结果,但这样可以让您朝着正确的方向前进。
select Relationship, Count(l1.Likes) 'Pizza Only', Count(l2.likes) 'Soda Only', Count(PS.Id) 'Both' FROM @People p LEFT Outer join @likes l1 on l1.id = p.id and l1.Likes = 'Pizza' LEFT Outer join @likes l2 on l2.id = p.id and l2.Likes = 'Soda' LEFT Outer JOIN (select p2.Id, Count(distinct(Likes)) count FROM @People p2 INNER JOIN likes l3 ON p2.Id = l3.Id WHERE Likes in ( 'Pizza','Soda') Group by p2.Id Having Count(distinct(Likes))> 1) PS ON p.Id = pS.id GROUP BY RelationShip
答案 2 :(得分:0)
这可能有帮助吗?
DECLARE @People TABLE (
Id INT PRIMARY KEY,
Name VARCHAR(50),
Relationship VARCHAR(50));
INSERT INTO @People VALUES (1, 'Andy', 'Relative');
INSERT INTO @People VALUES (2, 'Jim', 'Friend');
INSERT INTO @People VALUES (3, 'Anderson', 'Friend');
INSERT INTO @People VALUES (4, 'Pamela', 'Relative');
DECLARE @Likes TABLE (
Id INT,
Likes VARCHAR(50));
INSERT INTO @Likes VALUES (1, 'Pizza');
INSERT INTO @Likes VALUES (1, 'Soda');
INSERT INTO @Likes VALUES (2, 'Pizza');
INSERT INTO @Likes VALUES (3, 'Soda');
INSERT INTO @Likes VALUES (4, 'Pizza');
INSERT INTO @Likes VALUES (4, 'Soda');
WITH Likes AS (
SELECT
l.Id,
SUM(CASE WHEN l.Likes = 'Pizza' THEN 1 ELSE 0 END) AS Pizza,
SUM(CASE WHEN l.Likes = 'Soda' THEN 1 ELSE 0 END) AS Soda
FROM
@Likes l
GROUP BY
l.Id)
SELECT
p.Relationship,
SUM(CASE WHEN l.Pizza = 1 AND l.Soda = 0 THEN 1 ELSE 0 END) AS PizzaOnly,
SUM(CASE WHEN l.Pizza = 0 AND l.Soda = 1 THEN 1 ELSE 0 END) AS SodaOnly,
SUM(CASE WHEN l.Pizza = 1 AND l.Soda = 1 THEN 1 ELSE 0 END) AS PizzaAndSoda
FROM
@People p
LEFT JOIN Likes l ON l.Id = p.Id
GROUP BY
p.Relationship;
结果:
Relationship PizzaOnly SodaOnly PizzaAndSoda
Friend 1 1 0
Relative 0 0 2