[英]SQL query to return most common, 2nd most common, 3rd most common occurrences
我可以使用数据透视表在Excel中完成此操作,但我想找出一种直接从单个SQL查询中执行此操作的方法。 假设我有一份水果及其新鲜度的清单:
**Fruit Freshness**
Banana New
Banana Ripe
Apple Ripe
Orange Old
Cherry New
Orange Ripe
Apple Old
Banana Old
Apple New
Apple New
Orange Ripe
Banana Old
Orange New
Cherry New
Cherry Ripe
我想计算“新鲜度”出现的次数,然后将它们排列为最频繁,第二最频繁,依此类推。 结果看起来像这样:
**Fruit Most common 2nd most common 3rd most common**
Banana New Old Ripe
Apple Old New Ripe
Orange Ripe New Old
Cherry New Ripe NA
一个查询有可能吗?
您可以只使用GROUP_CONCAT()聚合函数:
SELECT
Fruit, GROUP_CONCAT(Freshness ORDER BY cnt DESC) as Common
FROM (
SELECT Fruit, Freshness, COUNT(*) cnt
FROM
fruits
GROUP BY
Fruit, Freshness
) s
这将返回如下值:
Fruit | Common
---------------------
Banana | New,Old,Ripe
Cherry | New,Ripe
... | ...
但是,如果要将结果分为三列,则可以合并上一个查询并使用SUBSTRING_INDEX()从逗号分隔的值中提取第一个,第二个和第三个值:
SELECT
Fruit,
SUBSTRING_INDEX(Common, ',', 1) AS most,
CASE WHEN CommonLIKE '%,%'
THEN SUBSTRING_INDEX(SUBSTRING_INDEX(Common, ',', 2), ',', -1) END AS second_most,
CASE WHEN CommonLIKE '%,%,%'
THEN SUBSTRING_INDEX(SUBSTRING_INDEX(Common, ',', 3), ',', -1) END AS third_most
FROM (
SELECT
Fruit, GROUP_CONCAT(Freshness ORDER BY cnt DESC) as Common
FROM (
SELECT Fruit, Freshness, COUNT(*) cnt
FROM
fruits
GROUP BY
Fruit, Freshness
) s
GROUP BY
Fruit
) s
SQL Server 2008及更高版本:
select fruit, [1], [2], [3] from
( select row_number() over (partition by fruit order by ct desc) as rn, fruit, freshness from (
select count(1) as ct, fruit, freshness from f
group by fruit, freshness ) g ) src
PIVOT
(
MAX(Freshness)
FOR rn in ([1], [2], [3])
) pvt
尝试这个
create table #fr (Fruit varchar(20), Freshness varchar(20))
insert #fr values
('Banana' , 'New'),('Banana' , 'Ripe'),('Apple' , 'Ripe'),('Orange' , 'Old'),('Cherry' , 'New'),
('Orange' , 'Ripe'),('Apple' , 'Old'),('Banana' , 'Old'),('Apple' , 'New'),('Apple' , 'New'),
('Orange' , 'Ripe'),('Banana', 'Old'),('Orange' , 'New'),('Cherry', 'New'),('Cherry', 'Ripe')
SELECT Fruit,
[1] Most_Common,
[2] Second_Common,
[3] Third_common
FROM (SELECT Fruit,Freshness,
Row_number()OVER(partition BY Fruit ORDER BY Count(*) DESC) rn
FROM #fr
GROUP BY Fruit,Freshness) a
PIVOT (Max(Freshness)
FOR rn IN([1],[2],[3])) piv
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.