34,593
社区成员
发帖
与我相关
我的任务
分享
Date TotalTest FirstPass RetestPass Fail TotalPass Bin0.00 Bin21.09 Bin24.12 Bin8.04 Bin9.26 Bin9.27
---------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
2009-08-06 10 6 2 2 8 0 0 0 0 0 0
2009-08-12 10 8 0 2 8 0 0 0 0 0 0
2009-08-14 12 8 0 4 8 0 0 0 0 0 0
Server: Msg 8115, Level 16, State 8, Line 1
Arithmetic overflow error converting numeric to data type numeric.
--测试数据
declare @Test table(testid int, productid int, starttime datetime, pass tinyint)
insert @test
select 1, 11, '2009-09-11 12:34:56', 0 union all
select 2, 12, '2009-09-11 12:34:57', 1 union all
select 3, 11, '2009-09-11 13:00:00', 1 union all
select 4, 12, '2009-09-11 12:34:57', 1 union all
select 5, 13, '2009-09-11 13:00:00', 0 union all
select 6, 13, '2009-09-11 14:01:00', 0 union all
select 7, 13, '2009-09-11 14:33:00', 0 union all
select 8, 13, '2009-09-12 11:34:22', 1 union all
select 9, 14, '2009-09-12 14:56:00', 0
declare @TestValue table(testvalueid int, testid int, bin decimal(4,2))
insert @testvalue
select 111, 1, 9.10 union all
select 112, 2, 0.00 union all
select 113, 3, 0.00 union all
select 114, 4, 0.00 union all
select 115, 5, 5.11 union all
select 116, 6, 5.11 union all
select 117, 7, 10.06 union all
select 118, 8, 0.00 union all
select 119, 9, 7.33
--临时表1
select convert(char(10), starttime, 120) as test_date, productid,
min(testid) as first_id, max(testid) as last_id
into # from @test
group by convert(char(10), starttime, 120), productid
--临时表2
select a.firstpass, a.retestpass, a.fail, a.totalpass, b.* into #1 from
(
select test_date,
sum(firstpass*retestpass) as firstpass,
sum(case when firstpass = 0 and retestpass = 1 then 1 else 0 end) as retestpass,
sum(case retestpass when 0 then 1 else 0 end) as fail,
sum(retestpass) as totalpass
from
(
select a.test_date, a.productid, b.pass as firstpass, c.pass as retestpass
from # a
join @test b on a.productid = b.productid and a.first_id = b.testid
join @test c on a.productid = c.productid and a.last_id = c.testid
) test
group by test_date
) a join
(
select a.test_date, c.bin
from # a
join @test b on a.productid = b.productid and a.last_id = b.testid
join @testvalue c on b.testid = c.testid
) b on a.test_date = b.test_date
--动态SQL
declare @s varchar(8000)
set @s='select test_date, firstpass + retestpass + fail as totaltest, firstpass, retestpass, fail, totalpass'
select @s=@s+', sum(case bin when ' + convert(varchar, bin) + ' then 1 else 0 end) [bin' + convert(varchar, bin) + ']'
from #1
group by bin
order by bin
set @s=@s+' from #1 group by test_date, firstpass, retestpass, fail, totalpass'
exec(@s)
/*
test_date totaltest firstpass retestpass fail totalpass bin0.00 bin7.33 bin10.06
---------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
2009-09-11 3 1 1 1 2 2 0 1
2009-09-12 2 1 0 1 1 1 1 0
*/
--改这样
SELECT @str = @str + ',SUM(CASE WHEN D.Bin='''+RTRIM(Bin)
+''' THEN 1 ELSE 0 END) AS [Bin'+RTRIM(Bin)+']'
FROM #TestValue AS A
WHERE EXISTS(SELECT * FROM #tmp WHERE A.TestID = MaxTestID)
GROUP BY Bin;
EXEC('SELECT A.[Date],COUNT(*) AS TotalTest,
SUM(B.Pass) AS FirstPass,
SUM(CASE WHEN C.Pass=1 AND B.Pass<>C.Pass THEN 1 ELSE 0 END) AS RetestPass,
SUM(A.Fail) AS Fail,
SUM(B.Pass)+SUM(CASE WHEN C.Pass=1 AND B.Pass<>C.Pass THEN 1 ELSE 0 END) AS TotalPass
FROM #tmp AS A
LEFT JOIN #Test AS B
ON A.StartTime = B.StartTime AND A.ProductID = B.ProductID
AND A.MinTestID = B.TestID
LEFT JOIN #Test AS C
ON A.EndTime = C.StartTime AND A.ProductID = C.ProductID
AND A.MaxTestID = C.TestID
LEFT JOIN #TestValue AS D
ON A.MaxTestID = D.TestID
GROUP BY A.[Date]');
不看Bin时,结果是否正确?