我们在SQL中有许多带有测试结果数据的表。
示例表:
Timestamp SN FreqMin CarrierFreq FreqMax
2015-06-01 00123445 1000 1500 2000
2015-06-01 100000000 1000 0 2000
2015-06-01 00123446 1000 1500 2000
2015-06-01 100123445 1000 2500 2000
我们想查询表格来分析测试结果。
我在以下查询中得到'AS'附近的'语法不正确'。
SELECT Count(*) AS 'Total Records',
CASE
WHEN LEN(SN) = '8' THEN (SUM(CASE WHEN LEN(SN) = '8' THEN 1 ELSE 0 END)) AS 'Pass'
WHEN SN = '100000000' THEN SUM(1) AS 'IR Failure'
WHEN SN != '100000000' AND LEN(SN) = '9' THEN
SUM(CASE WHEN LEN(SN) = '9' THEN 1 ELSE 0 END) AS 'Fail',
SUM(CASE WHEN CarrierFreq NOT BETWEEN FreqMin AND FreqMax THEN 1 ELSE 0 END) AS 'Carrier Freq Fail'
!Add in a lot more tests similar to the line above
END
FROM [SERVER].[dbo].[Table]
WHERE TestDate >= '2015-06-01'
我想要完成的是以下内容:
如果LEN(SN)='8',则递增Pass列值,不再对该行进行分析。 如果SN ='100000000',那么它是IR故障,增加IR故障列并且不对该行进行任何更多分析。 如果SN!='100000000'且LEN(SN)='9',则检查故障并计算每个故障。
如果我只是在做我的查询
SUM(CASE WHEN CarrierFreq NOT BETWEEN FreqMin AND FreqMax THEN 1 ELSE 0 END)
AS'运营商频率失败'
语句,我可以正确返回所有数据。但是在计数器中存在错误的失败,因为任何具有IR失败的测试都将具有不会通过的默认数据。
所以从上表中可以得出结果:
Total Records = 4
Pass = 2
IR Failure = 1
Fail = 1
Carrier Freq Fail = 1