分组查询

njlywy 2009-11-06 10:28:10
在Match表中有两个字段:
matchTime(datetime)
result(int,1表示胜利,0表示输)
第一天有3场比赛,结果1,0,1
第二天有2场比赛,结果1,0
第三天有2场比赛,结果1,0

通过sql语句查询出如下结果:
比赛时间(只包含yyyy-MM-dd) 胜 负

要求用到分组,字连接,convert格式化时间
...全文
70 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
njlywy 2009-11-06
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 fredrickhu 的回复:]
不知道5楼的是不是你要的
[/Quote]

答案完美正确
dtxh168 2009-11-06
  • 打赏
  • 举报
回复
[Quote=引用楼主 njlywy 的回复:]
在Match表中有两个字段:
matchTime(datetime)
result(int,1表示胜利,0表示输)
第一天有3场比赛,结果1,0,1
第二天有2场比赛,结果1,0
第三天有2场比赛,结果1,0

通过sql语句查询出如下结果:
比赛时间(只包含yyyy-MM-dd)              胜                    负
 
要求用到分组,字连接,convert格式化时间
[/Quote]
dtxh168 2009-11-06
  • 打赏
  • 举报
回复
select convert(varchar(10),matchTime,120) as 比赛时间,
sum(v) as 胜,
sum(f) as 负
from(
select matchTime,1 as v,0 as f from Match where result=1
union all
select matchTime,0,1 from Match where result=0
) t
group by convert(varchar(10),matchTime,120)
--小F-- 2009-11-06
  • 打赏
  • 举报
回复
不知道5楼的是不是你要的
--小F-- 2009-11-06
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2009-11-06 22:40:33
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([matchTime] datetime,[result] varchar(5))
insert [tb]
select '2009-11-5','1,0,1' union all
select '2009-11-6','1,0' union all
select '2009-11-7','1,0'
--------------开始查询--------------------------
;with f as
(
select
a.matchTime,b.result
from
(select matchTime,result=convert(xml,'<root><v>'+replace(result,',','</v><v>')+'</v></root>') from Tb)a
outer apply
(select result=C.v.value('.','nvarchar(100)') from a.result.nodes('/root/v')C(v))b
)
select
convert(varchar(10),matchtime,120) as matchTime,
sum(case result when 1 then 1 else 0 end) as 胜,
sum(case result when 0 then 1 else 0 end) as 输
from
f
group by
convert(varchar(10),matchtime,120)
----------------结果----------------------------
/* matchTime 胜 输
---------- ----------- -----------
2009-11-05 2 1
2009-11-06 1 1
2009-11-07 1 1

(3 行受影响)


*/
rucypli 2009-11-06
  • 打赏
  • 举报
回复
create funcation test(@date datetime)
return varchar(200)
as
begin
decalre @temp varchar(200)
set @temp = ''
sleect @temp =@temp + result
from tb
where matchtime=@date
end

select matchtime,test(matchtime)
from tb
group by matchtime
njlywy 2009-11-06
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 fredrickhu 的回复:]
什么地方用到字连接了?
[/Quote]

请问下,还有更好的办法么??
--小F-- 2009-11-06
  • 打赏
  • 举报
回复
什么地方用到字连接了?
--小F-- 2009-11-06
  • 打赏
  • 举报
回复
行列转换 
select
convert(varchar(10),matchtime,120) as matchTime,
max(case result when 1 then 1 else 0 end) as 胜,
max(case result when 0 then 1 else 0 end) as 输
from
tb
group by
convert(varchar(10),matchtime,120)

27,579

社区成员

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

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