一道SQL语句面试题,关于group by

tata_1980 2005-05-15 06:01:19
表内容:
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

--------------多谢!!!!
...全文
480 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
rfq 2005-05-16
  • 打赏
  • 举报
回复
select rq,sum(case shengfu when '胜' then 1 else 0 end) as 胜,sum(case shengfu when '负' then 1 else 0 end) as 负 from
表 group by rq

select rq ,(select count(*) from 表 b where a.rq=b.rq and b.shengfu='胜') as 胜 ,(select count(*) from 表 c where a.rq=c.rq and c.shengfu='负') as 负 from
表 a group by rq
tdtjjiao 2005-05-16
  • 打赏
  • 举报
回复
select [日期]=convert(char(10),日期,120),
[胜]=sum(case when [胜负]='胜' then 1 else 0 end,
[负]=sum(case when [胜负]='负' then 1 else 0 end
from tablename
group by convert(char(10),日期,120)
order by convert(char(10),日期,120)
xluzhong 2005-05-15
  • 打赏
  • 举报
回复
select [日期]=convert(char(10),日期,120),
[胜]=sum(case when [胜负]='胜' then 1 else 0 end,
[负]=sum(case when [胜负]='负' then 1 else 0 end
from tablename
group by convert(char(10),日期,120)
order by convert(char(10),日期,120)
guchengxilu 2005-05-15
  • 打赏
  • 举报
回复
SELECT 字段B ='胜利',字段B='负'INTO 新表 FROM 表 WHERE 字段A='2005-05-09' AND 字段A='2005-05-10'

SLECT * FROM 新表
这样行吗?~^_^
千万不要扔鸡蛋
hj0849cn 2005-05-15
  • 打赏
  • 举报
回复
select a.col001,a.a1 胜,b.b1 负 from
(select col001,count(col001) a1 from temp1 where col002='胜' group by col001) a,
(select col001,count(col001) b1 from temp1 where col002='负' group by col001) b
where a.col001=b.col001
talantlee 2005-05-15
  • 打赏
  • 举报
回复
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', '负')
select N.rq,N.勝,M.負 from (select rq,勝=count(*) from #tmp where shengfu='胜'group by rq)N inner join
(select rq,負=count(*) from #tmp where shengfu='负'group by rq)M on N.rq=M.rq

---純隨搶點分
Rotaxe 2005-05-15
  • 打赏
  • 举报
回复
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', '负')
select rq, sum(case when shengfu='胜' then 1 else 0 end)'胜' , sum(case when shengfu='负' then 1 else 0 end)'负' from #tmp group by rq

27,582

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧