SQL多表查询问题

这个昵称已经存在了 2013-07-19 10:29:11
表结构如下:



CREATE TABLE [dbo].[test88](
[id] [int] IDENTITY(1,1) NOT NULL, --ID 自增长
[isType] [int] NULL, --类型 1增加 2减少
[fraction] [int] NULL, --分数
[operateTime] [datetime] NULL --时间
(




现在求的功能是:查询出每天增加和减少的积分.

实现如下格式


增加 减少 时间
0 9 2013-07-11
2 0 2013-07-12
7 1 2013-07-13
0 2 2013-07-14
2 8 2013-07-15
3 2 2013-07-16
1 7 2013-07-17
0 3 2013-07-18


请问怎么查询出来哦?


如果只有一个类型还好查

select count(id) as idcount,CONVERT(varchar(10),operateTime,20) as operateTime

from test88 group by CONVERT(varchar(10),operateTime,20)


两个我不知道怎么查才好了.求助...

...全文
121 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
雷肿么了 2013-07-20
  • 打赏
  • 举报
回复
兰州,结贴。
引用 4 楼 ap0405140 的回复:
try this,

select sum(case when isType=1 then fraction else 0 end) '增加',
       sum(case when isType=2 then fraction else 0 end) '减少',
       convert(varchar(10),operateTime,20) '时间'
 from test88 
 group by convert(varchar(10),operateTime,20)
kobemadi 2013-07-19
  • 打赏
  • 举报
回复
感觉有点难懂,Mark一下再说。
哥眼神纯洁不 2013-07-19
  • 打赏
  • 举报
回复

select 
(select SUM(fraction) from test88 
where isType=1 and 
CONVERT(varchar(10),a.operateTime,20)=CONVERT(varchar(10),operateTime,20)) 增加,
(select SUM(fraction) from test88 
where isType=2 and 
CONVERT(varchar(10),a.operateTime,20)=CONVERT(varchar(10),operateTime,20)) 增加,
[operateTime] 时间 from test88 a
这样查就行
唐诗三百首 2013-07-19
  • 打赏
  • 举报
回复
try this,

select sum(case when isType=1 then fraction else 0 end) '增加',
       sum(case when isType=2 then fraction else 0 end) '减少',
       convert(varchar(10),operateTime,20) '时间'
 from test88 
 group by convert(varchar(10),operateTime,20)
wanglingzhong 2013-07-19
  • 打赏
  • 举报
回复
行转列问题,具体百度

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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