求教一个数据库查询的问题

q87422229 2012-10-24 02:22:02
有一张Report表,存到是周报信息,现在有开始时间,结束时间,提交状态3个字段,提交状态有正常,延迟两种,我想要以

开始时间,结束时间,正常数,延迟数 这种方式来输出查询结果要怎么弄呢?如果再加上一个未提交要怎么判断输出呢?
...全文
67 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
andy_liucj 2012-10-24
  • 打赏
  • 举报
回复
create table #table
(
begin_time datetime,
end_time datetime,
status bit
)


insert #table
select '2012/10/20', '2012/10/23', 1 union all
select '2012/9/24', '2012/10/24', 0 union all
select '2012/10/10', '2012/10/15', 1 union all
select '2012/10/10', '2012/10/15', null union all
select '2012/10/20', '2012/10/23', 0 union all
select '2012/10/20', '2012/10/23', 0 union all
select '2012/10/20', '2012/10/23', 1 union all
select '2012/9/24', '2012/10/24', 0 union all
select '2012/9/24', '2012/10/24', 1


select begin_time, end_time ,
sum(case when status=1 then 1 else 0 end) normal_cnt,
sum(case when status=0 then 1 else 0 end) postpone_cnt,
sum(case when status IS null then 1 else 0 end) unsumbited
from #table group by begin_time, end_time

begin_time end_time normal_cnt postpone_cnt unsumbited
----------------------- ----------------------- ----------- ------------ -----------
2012-10-10 00:00:00.000 2012-10-15 00:00:00.000 1 0 1
2012-10-20 00:00:00.000 2012-10-23 00:00:00.000 2 2 0
2012-09-24 00:00:00.000 2012-10-24 00:00:00.000 1 2 0

(3 row(s) affected)
發糞塗牆 2012-10-24
  • 打赏
  • 举报
回复
那是你业务上去顶的。不是代码来顶,代码只是实现你的业务
q87422229 2012-10-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

SQL code
select 开始时间,结束时间,sum(case when 提交状态='正常' then 1 else 0 end ) '正常数',
sum(case when 提交状态='延迟' then 1 else 0 end ) '延迟数',
sum(case when 提交状态='未提交' then 1 else 0 end ) '未提交数'
from report
group……
[/Quote]
噢噢,是这样呢,不过未提交的或者说没写的该怎么算呢,总人数-(正常+延迟)?
發糞塗牆 2012-10-24
  • 打赏
  • 举报
回复
select 开始时间,结束时间,sum(case when 提交状态='正常' then 1 else 0 end ) '正常数',
sum(case when 提交状态='延迟' then 1 else 0 end ) '延迟数',
sum(case when 提交状态='未提交' then 1 else 0 end ) '未提交数'
from report
group by 开始时间,结束时间

22,209

社区成员

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

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