22,209
社区成员
发帖
与我相关
我的任务
分享
38 1 44200101 200607 1 1 1.1 0.8
200607相应记录的(bdj1+bdj2)/2 =1
你怎么得出1.1来的?
insert into a select ssdh, qydh, cpdm, '200707' as InputYearMonth ,0 as bdj1, 0 as bdj2, (bdj1+bdj2)/2as jdj, (select (bdj1+bdj2)/2 as tdj from a where InputYearMonth = '200607') as tdj
from a
where InputYearMonth='200706' and ssdh='38' and qydh='1'
insert into a
select ssdh, qydh, cpdm, '200707' as InputYearMonth ,0 as bdj1, 0 as bdj2,
(bdj1+bdj2)/2as jdj,
(
SELECT TOP 1 (bdj1+bdj2)/2
FROM a
WHERE InputYearMonth=200606
AND ssdh =b.ssdh AND qrydh=b.qrydh
) as tdj
from a as b
where InputYearMonth='200706' and ssdh='38' and qydh='1'
insert into a select ssdh, qydh, cpdm, '200707' as InputYearMonth ,0 as bdj1, 0 as bdj2, (bdj1+bdj2)/2as jdj, 0 as tdj
from a
where InputYearMonth='200706' and ssdh='38' and qydh='1'
create function fsum(@bdj1 float ,@bdj2 float)
returns float
as begin
declare @jdj float
set @jdj = (@bdj1+@bdj2)/2
return @jdj
end -- 函数
insert into a select ssdh, qydh, cpdm, '200707' as InputYearMonth ,0 as bdj1, 0 as bdj2, dbo.fsum(bdj1,bdj2)as jdj, 0 as tdj
from a
where InputYearMonth='200706' and ssdh='38' and qydh='1' -- 我用的日期为varchar(20)型的
insert into a
select ssdh, qydh, cpdm, '200707' as InputYearMonth ,0 as bdj1, 0 as bdj2,
(bdj1+bdj2)/2as jdj,
(
select top 1 (bdj1+bdj2)/2
from a
where InputYearMonth=convert(char(4),convert(int,left('200707',4))-1)+right('200707',2)
and ssdh=b.ssdh and qydh=b.qydh and cpdm=b.cpdm
) as tdj
from a as b
where InputYearMonth='200706' and ssdh='38' and qydh='1'