Sql高手请进,关于统计的问题,详情见补充。
事故表:accident:accidentID:表主键;accidentName:事故名称;accidentDate:事故发生日期;accidentpart:责任部门;a...
事故表:accident:accidentID:表主键;accidentName:事故名称;accidentDate:事故发生日期;accidentpart:责任部门;accidentState:事故状态(1:草稿;2.已确认;3.已处理);
语句:create table accident (accidentID VARCHAR(36) not null,accidentNAME VARCHAR(36) not null, accidentDATE TIMESTAMP not null,accidentSTATE VARCHAR(36) not null, accidentpart VARCHAR(36) not null,constraint PK_accident_REG primarykey (accidentID));
选择了部门和时间段后,一条sql语句统计出该时间段内该部门下每个状态的事故的数量(效果如下):
求大神指教,感激不尽! 展开
语句:create table accident (accidentID VARCHAR(36) not null,accidentNAME VARCHAR(36) not null, accidentDATE TIMESTAMP not null,accidentSTATE VARCHAR(36) not null, accidentpart VARCHAR(36) not null,constraint PK_accident_REG primarykey (accidentID));
选择了部门和时间段后,一条sql语句统计出该时间段内该部门下每个状态的事故的数量(效果如下):
求大神指教,感激不尽! 展开
4个回答
展开全部
select accidentName,accidentpart,
sum(case when accidentState=1 then 1 else 0 end) as 草稿,
sum(case when accidentState=2 then 1 else 0 end) as 已确认,
sum(case when accidentState=3 then 1 else 0 end) as 已处理
from accident
group by accidentName,accidentpart
展开全部
Declare @beginDate as Date;--开始时间
Declare @endDate as Date;--结束时间
Select accidentPart as 责任部门,
accidentName as 事故名称,
Sum(case when accidentState = 1 then 1 else 0 end) as 草稿,
Sum(case when accidentState = 2 then 1 else 0 end) as 已确认,
Sum(case when accidentState = 3 then 1 else 0 end) as 已处理
From 事故表
Group by accidentPart,accidentName
Where accidentDate between @beginDate And @endDate --包含开始结束时间
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
select a.accidentName as 事故名称,a.accidentpart as 责任部门,
(select count(1) from accident b where a.accidentName = b.accidentName and a.accidentpart = b.accidentpart and accidentState = 1) as 草稿,
(select count(1) from accident b where a.accidentName = b.accidentName and a.accidentpart = b.accidentpart and accidentState = 2) as 已确认,
(select count(1) from accident b where a.accidentName = b.accidentName and a.accidentpart = b.accidentpart and accidentState = 3) as 已处理
from accident a
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-12-09
展开全部
DECLARE @accidentpart VARCHAR(30)
DECLARE @date1 DATE
DECLARE @date2 DATE
SET @accidentpart = '部门'
SET @date1 = '时间1'
SET @date2 = '时间2'
SELECT accidentName 事故名称 ,
accidentpart 责任部门 ,
SUM(CASE accidentState
WHEN '草稿' THEN 1
ELSE 0
END) 草稿 ,
SUM(CASE accidentState
WHEN '已确认' THEN 1
ELSE 0
END) 已确认 ,
SUM(CASE accidentState
WHEN '已处理' THEN 1
ELSE 0
END) 已处理
FROM accident
WHERE accidentpart = @accidentpart
AND accidentDate BETWEEN @date1 AND @date2
GROUP BY accidentName ,
accidentpart
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询