SQL 涉及的查询问题(我猜是 SUM、Group By、Order by?也许是总计,甚至是计数)

SQL Query Problem Involving (SUM, Group By, Order by, I guess? and maybe total, or even count)

通过SQL查询,找出总交易额最高的Top 5,分别是哪个行业?以及该行业的商店数量?

我的 SQL 数据如下所示:

Store Name Industry Transaction Value
Ace A 196
Ace A 193
Area A 168
Apple A 165
Boy B 145
Boy B 143
Bull B 136
Bread B 131
Cat C 116
Cat C 106
Cake C 104
Candy C 102
Dog D 101
Dog D 92
Door D 80
Daddy D 75
Egg E 70
Egg E 67
Earl E 66
Eagle E 61

仅供参考,交易额最高的前 5 名是:

No. Store Name Industry Total Transaction Value
1 Ace A 389
2 Boy B 288
3 Cat C 222
4 Dog D 193
5 Area A 168

SQL 查询结果应如下所示:

Industry No. of Stores
A 2
B 1
C 1
D 1
E 0
select a.industry, sum(case when b.name is null then 0 else 1 end) as no 
from
    (select distinct industry from transactions ) a
left join
    (select name, industry 
    from transactions 
    group by name, industry
    order by sum(transaction_vaule) desc limit 5) b
on a.industry = b.industry
group by a.industry
order by a.industry

我想我有一个解决方案。请检查我使用的代码 Common Table Expression ,CASE,SUM and group by =>

WITH CTE AS
(
SELECT industry, SUM(TransactionValue) AS Transaction_Value, 
COUNT(StoreName) AS StoreCount  FROM MYTable
    GROUP BY StoreName,industry
    ORDER BY SUM(TransactionValue) DESC
    Limit 5
)
SELECT T1.industry,
       SUM((CASE WHEN c.industry IS NULL THEN 0
       ELSE 1 END)) as CT
FROM
(SELECT DISTINCT Industry FROM MYTable) AS T1
LEFT JOIN CTE as c ON T1.industry=c.industry
GROUP BY  T1.industry

注意:子查询不是最佳实践,但在您的情况下,我认为不会有性能问题。另外,请检查代码,因为我没有安装 Snowflake SQL 数据库,所以可能会有一些明显的语法错误 .

要获得确定性结果,您必须了解关系。假设前 9 个结果是

Cat/A/600, Dog/A/500, Cat/B/500, Dog/B/400, Cat/C/300, Dog/C/300, Cat/D/300, Dog/D/200, Cat/E/100

哪个是前五名? Cat/C/300 或 Dog/C/300 或 Cat/D/300?或者其中 none 个?如果我们任意选择一行(按 LIMIT 5FETCH FIRST 5 ROWS ONLY),我们更喜欢一个行业而不是另一个行业。

在标准 SQL 中,我们有子句 FETCH FIRST 5 ROWS WITH TIES,但遗憾的是,snowflake 没有此功能。但是它确实具有 DENSE_RANK。它对我的示例行进行排名:

#1: Cat/A/600
#2: Dog/A/500
#2: Cat/B/500
#3: Dog/B/400
#4: Cat/C/300
#4: Dog/C/300
#4: Cat/D/300
#5: Dog/D/200
#6: Cat/E/100

因为前五个值分别是 600、500、400、300 和 200。

查询:

select industry, count(case when rnk <= 5 then 1 end) as stores
from
(
  select industry, dense_rank() over (order by sum(transaction_value) desc) as rnk
  from mytable
  group by store_name, industry
) ranked
group by industry
order by industry;

如果您只想显示热门行业:

select industry, count(*) as stores
from
(
  select industry, dense_rank() over (order by sum(transaction_value) desc) as rnk
  from mytable
  group by store_name, industry
) ranked
where rnk <= 5
group by industry
order by industry;