我可以在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这在一个查询中是可能的吗?
发布于 2014-11-20 07:02:53
您可以只使用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发布于 2014-11-20 07:33:23
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发布于 2014-11-20 07:34:04
尝尝这个
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 https://stackoverflow.com/questions/27028533
复制相似问题