交叉表查询:
- TRANSFORM MIN(列)
- SELECT 身份证号 FROM
- (
- SELECT T1.身份证号,T1.列,T1.值,T1.排名,SUM(1) AS 排名2 FROM
- (SELECT A.身份证号,A.列,A.值,COUNT(B.值)+1 AS 排名 FROM
- (select 身份证号,蔬菜 AS 值,"蔬菜" AS 列 from [sheet1$A1:E5] UNION ALL
- select 身份证号,水果,"水果" from [sheet1$A1:E5] UNION ALL
- select 身份证号,清洁用品,"清洁用品" from [sheet1$A1:E5] UNION ALL
- select 身份证号,粮油,"粮油" from [sheet1$A1:E5])A
- LEFT JOIN
- (select 身份证号,蔬菜 AS 值 from [sheet1$A1:E5] UNION
- select 身份证号,水果 from [sheet1$A1:E5] UNION
- select 身份证号,清洁用品 from [sheet1$A1:E5] UNION
- select 身份证号,粮油 from [sheet1$A1:E5])B
- ON A.身份证号=B.身份证号 AND A.值<B.值
- GROUP BY A.身份证号,A.列,A.值)T1,
- (select 身份证号,蔬菜 AS 值,"蔬菜" AS 列 from [sheet1$A1:E5] UNION ALL
- select 身份证号,水果,"水果" from [sheet1$A1:E5] UNION ALL
- select 身份证号,清洁用品,"清洁用品" from [sheet1$A1:E5] UNION ALL
- select 身份证号,粮油,"粮油" from [sheet1$A1:E5])T2
- WHERE T1.身份证号=T2.身份证号 AND T1.值=T2.值 AND T1.列>=T2.列
- GROUP BY T1.身份证号,T1.列,T1.值,T1.排名
- )
- GROUP BY 身份证号,排名2
- PIVOT "第"&排名&"消费"
复制代码
效果:
若要将相同等级的消费类别放在同一单元格,在excel中无法仅使用SQL实现,需要借助VBA。 |