表结构如下:
日期 赛况
2012-12-11 胜
2012-12-11 负
2012-12-11 负
2012-12-12 负
2012-12-12 胜
2012-12-13 胜
2012-12-14 胜
要求:
日期 胜 负
2012-12-11 1 2
2012-12-12 1 1
2012-12-13 1 0
2012-12-14 1 0
请问怎么写sql语句?
就是简单的行列转换
SELECT 日期 ,SUM(胜) AS 胜 ,SUM(负) AS 负 FROM( SELECT 日期 ,CASE 赛况 WHEN ‘胜’ THEN 1 ELSE 0 END AS 胜 ,CASE 赛况 WHEN ‘负’ THEN 1 ELSE 0 END AS 负 FROM XXTABLE ) A GROUP BY 日期
http://stackoverflow.com/questions/564997/join-two-sql-queries
SELECT 日期, SUM(胜) AS 胜, SUM(负) AS 负 FROM ( SELECT 日期, 1 AS 胜, 0 AS 负 FROM Table_赛况 WHERE 赛况='胜' UNION SELECT 日期, 0 AS 胜, 1 AS 负 FROM Table_赛况 WHERE 赛况='负' ) GROUP BY 日期
SELECT
日期
,SUM(胜) AS 胜
,SUM(负) AS 负
FROM(
SELECT
日期
,CASE 赛况 WHEN '胜' THEN 1 ELSE 0 END AS 胜
,CASE 赛况 WHEN '负' THEN 1 ELSE 0 END AS 负
FROM dbo.Match
) A
GROUP BY 日期