请找到以下方案,将前一个值相加,并将零替换为之前的值。
Create table joborder (
jo_no int(10),
jo_date datetime,
jo_status char(10)
);
insert into joborder values( 1,'01-03-13','A');
insert into joborder values( 2,'01-03-13','A');
Insert into joborder values (3,'01-03-13','A');
insert into joborder values( 4,'04-03-13','A');
insert into joborder values(5,'08-03-13','B');
insert into joborder values( 6,'12-03-13','C');
状态:
A
表示已完成
B
表示进度
C
表示开发
我想要一份报告如下
Date TotalJoborder A B C
---------------------------------------------
01-03-13 3 3 0 0
04-03-13 1 0 1 0
08-03-13 1 0 1 0
12-03-13 1 0 0 1
我想要一个如下报告,应该增加以前的值并显示之前的值。
Date TotalJoborder A B C
---------------------------------------------
01-03-13 3 3 0 0
04-03-13 4 3 1 0
08-03-13 5 3 1 1
12-03-13 6 4 1 1
答案 0 :(得分:0)
对于第一个输出,您可以执行以下操作:
select jo_date,
count(*) as total_job_order,
sum(case when jo_status= 'A' then 1 else 0 end) as status_a,
sum(case when jo_status= 'B' then 1 else 0 end) as status_b,
sum(case when jo_status= 'C' then 1 else 0 end) as status_c
from joborder
group by jo_date
order by jo_date;
SQLFiddle:http://sqlfiddle.com/#!2/5ab191/1
对于"跑步总数"你可以这样做:
select jo_date,
(select count(*) from joborder j2 where j2.jo_date <= t1.jo_date) as running_sum,
status_b,
status_c
from (
select jo_date,
sum(case when jo_status= 'A' then 1 else 0 end) as status_a,
sum(case when jo_status= 'B' then 1 else 0 end) as status_b,
sum(case when jo_status= 'C' then 1 else 0 end) as status_c
from joborder
group by jo_date
order by jo_date
) t1
顺便说一句:我几乎100%确定您不希望char(10)
列jo_status
,varchar(10)
而是{/ 1}}。
答案 1 :(得分:0)
选择jo_date, (从joborder j2中选择count(*),其中j2.jo_date&lt; = t1.jo_date)为running_sum, status_b, STATUS_C 来自( 选择jo_date, sum(jo_status ='A'然后1,0结束时的情况)为status_a, sum(jo_status ='B'然后1,0结束时的情况)为status_b, sum(jo_status ='C'然后1,0结束时的情况)为status_c 从工作订单 按jo_date分组 按jo_date排序 )t1