一道SQL语句题
表内容:
2005-05-09 胜
2005-05-09 胜
2005-05-09 负
2005-05-09 负
2005-05-10 胜
2005-05-10 负
2005-05-10 负
如果要生成下列结果, 该如何写sql语句?
??????????? 胜 负
2005-05-09 2 2
2005-05-10 1 2
create table #tmp(rq varchar(10),shengfu nchar(1))
insert into #tmp values('2005-05-09','胜')
insert into #tmp values('2005-05-09','胜')
insert into #tmp values('2005-05-09','负')
insert into #tmp values('2005-05-09','负')
insert into #tmp values('2005-05-10','胜')
insert into #tmp values('2005-05-10','负')
insert into #tmp values('2005-05-10','负')
------解决方案--------------------
/*
DROP TABLE TMP;
CREATE TABLE TMP(RQ VARCHAR2(20),SHENGFU VARCHAR2(5));
INSERT INTO TMP VALUES('2005-05-09','胜');
INSERT INTO TMP VALUES('2005-05-09','胜');
INSERT INTO TMP VALUES('2005-05-09','负');
INSERT INTO TMP VALUES('2005-05-09','负');
INSERT INTO TMP VALUES('2005-05-10','胜');
INSERT INTO TMP VALUES('2005-05-10','负');
INSERT INTO TMP VALUES('2005-05-10','负');*/
SELECT RQ,
SUM(DECODE(SHENGFU, '胜', 1, 0)) AS 胜,
SUM(DECODE(SHENGFU, '负', 1, 0)) AS 负
FROM TMP
GROUP BY RQ;
注意结贴.....
------解决方案--------------------估计lz又去面试了
多看看经典SQL大全吧
------解决方案--------------------行列转换都写烂了吧。