【发布时间】:2021-05-31 01:30:05
【问题描述】:
关于如何解决这个问题的任何想法?
在下面的查询中,我想将结果中所有在瑞士计数至少为 1 的名称的组合中。
我已经尝试根据我认为它的工作原理创建一个临时表,但必须有一个最简单的方法来解决:
SELECT c.name,
COUNT(CASE WHEN country = 'Germany' THEN 1 END) as 'Germany',
COUNT(CASE WHEN country = 'London' THEN 1 END) as 'London',
COUNT(CASE WHEN country = 'Switzerland' THEN 1 END) as 'Switzerland',
COUNT(CASE WHEN country NOT IN('London', 'Ireland') THEN 1 END) as 'others'
FROM a_products p
JOIN a_customers c
on id_customer = c.id
Group By name
可能的解决方案:
select *
into #Swiss
from a_products
where country = 'Switzerland'
SELECT c.name,
COUNT(CASE WHEN country = 'Germany' THEN 1 END) as 'Germany',
COUNT(CASE WHEN country = 'London' THEN 1 END) as 'London',
COUNT(CASE WHEN country = 'Switzerland' THEN 1 END) as 'Switzerland',
COUNT(CASE WHEN country NOT IN('London', 'Ireland') THEN 1 END) as 'others'
FROM a_products p
JOIN a_customers c
on p.id_customer = c.id
JOIN #Swiss s
on p.id_customer = s.id_customer
where s.id_customer is not null
Group By name
【问题讨论】:
-
请提供样本输入和输出。这将很容易检查。 :-)
标签: sql sql-server-2008