SQL server把少列多行拼接成多列多行
展开全部
SELECT [date],
MAX(CASE WHEN [id]='a' THEN [金额] ELSE NULL END) AS 'a.金额',
MAX(CASE WHEN [id]='a' THEN [笔数] ELSE NULL END) AS 'a.笔数',
MAX(CASE WHEN [id]='b' THEN [金额] ELSE NULL END) AS 'b.金额',
MAX(CASE WHEN [id]='b' THEN [笔数] ELSE NULL END) AS 'b.笔数',
MAX(CASE WHEN [id]='c' THEN [金额] ELSE NULL END) AS 'c.金额',
MAX(CASE WHEN [id]='c' THEN [笔数] ELSE NULL END) AS 'c.笔数',
MAX(CASE WHEN [id]='d' THEN [金额] ELSE NULL END) AS 'd.金额',
MAX(CASE WHEN [id]='d' THEN [笔数] ELSE NULL END) AS 'd.笔数',
MAX(CASE WHEN [id]='e' THEN [金额] ELSE NULL END) AS 'e.金额',
MAX(CASE WHEN [id]='e' THEN [笔数] ELSE NULL END) AS 'e.笔数'
FROM 表XXX GROUP BY [date];
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询