我可以使用数据透视表在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
这可以在一个查询中使用吗?
答案 0 :(得分:0)
您只需使用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
答案 1 :(得分:0)
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
答案 2 :(得分:0)
试试这个
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