34,590
社区成员
发帖
与我相关
我的任务
分享
select '数量' as [BNAME],
sum(case when day(OQCTime)=1 then Inspection else 0 end) as [1],
。。。。
sum(case when day(OQCTime)=31 then Inspection else 0 end) as [31]
from TB
--原理上就是这样的
select ltrim(1)
union all
select ltrim(4.5555)
/*
1
4.5555
*/
select '数量' as [BNAME],
ltrim(sum(case when day(OQCTime)=1 then floor(Inspection) else 0 end)) as [1],
ltrim(sum(case when day(OQCTime)=31 then floor(Inspection) else 0 end)) as [31]
from TB WHERE ....
union all
select 'WPP' as [BNAME],
ltrim(sum(case when day(OQCTime)=1 then WPP else 0 end)) as [1],
...
ltrim(sum(case when day(OQCTime)=31 then WPP else 0 end)) as [31]
from TB WHERE ....
--这样看就直观了
select 1 union all
select 4.5566
/*
1.0000
4.5566
*/
select '数量' as [BNAME],
sum(case when day(OQCTime)=1 then Inspection else 0 end) as [1],
。。。。
sum(case when day(OQCTime)=31 then Inspection else 0 end) as [31]
from TB WHERE ....
union all
select 'WPP' as [BNAME],
sum(case when day(OQCTime)=1 then WPP else 0 end) as [1],
...
sum(case when day(OQCTime)=31 then WPP else 0 end) as [31]
from TB WHERE ....
declare @TB table(OQCTime datetime,Inspection decimal(18,4))
insert into @TB
select '2012-01-01',2.3110 union all
select '2012-01-01',2.4110 union all
select '2012-01-02',1.2200 union all
select '2012-01-02',1.6000
--向下取整用floor
--向上取整用ceiling
select '数量' as [BNAME],
sum(case when day(OQCTime)=1 then floor(Inspection) else 0 end) as [1],
sum(case when day(OQCTime)=2 then floor(Inspection) else 0 end) as [2]
from @TB
/*
BNAME 1 2
----- --------------------------------------- ---------------------------------------
数量 6 4
*/
select '数量' as [BNAME],
sum(case when day(OQCTime)=1 then ceiling(Inspection) else 0 end) as [1],
sum(case when day(OQCTime)=31 then ceiling(Inspection) else 0 end) as [31]
from TB