首先,我只想说我是SQL Server的新手,很抱歉,如果这些查询看起来很可怕......
我们正在使用SQL Server 2008。
我正在对我们的售票软件的数据库运行查询,以了解所有" Aging"每组的票都有。然而,似乎返回的数字不是预期的。
例如,运行以下查询。结果如下所示
SELECT
'Group' = CASE A1.GroupName
WHEN 'Internet' THEN 'Server'
WHEN 'Programmers' THEN 'DBA/Apps'
WHEN 'Help Desk' THEN 'PIV Badge'
ELSE A1.GroupName
END,
ISNULL((SELECT COUNT(C2.CallID) FROM [HEAT].[heat].[CallLog] C2 LEFT JOIN [HEAT].[heat].[Asgnmnt] A2 ON C2.CallID = A2.CallID WHERE C2.RecvdDate <= DATEADD(dd, -15, GETDATE()) AND C2.RecvdDate >= DATEADD(dd, -30, GETDATE()) AND A2.GroupName = A1.GroupName AND C2.CallStatus = 'Open' AND C2.Category <> 'welfare' AND C2.CustType <> 'IFS' AND A2.Resolution = '' GROUP BY A2.GroupName), 0) As '15+',
ISNULL((SELECT COUNT(C3.CallID) FROM [HEAT].[heat].[CallLog] C3 LEFT JOIN [HEAT].[heat].[Asgnmnt] A2 ON C3.CallID = A2.CallID WHERE C3.RecvdDate <= DATEADD(dd, -31, GETDATE()) AND C3.RecvdDate >= DATEADD(dd, -60, GETDATE()) AND A2.GroupName = A1.GroupName AND C3.CallStatus = 'Open' AND C3.Category <> 'welfare' AND C3.CustType <> 'IFS' AND A2.Resolution = '' GROUP BY A2.GroupName), 0) As '30+',
ISNULL((SELECT COUNT(C4.CallID) FROM [HEAT].[heat].[CallLog] C4 LEFT JOIN [HEAT].[heat].[Asgnmnt] A2 ON C4.CallID = A2.CallID WHERE C4.RecvdDate <= DATEADD(dd, -61, GETDATE()) AND C4.RecvdDate >= DATEADD(dd, -70, GETDATE()) AND A2.GroupName = A1.GroupName AND C4.CallStatus = 'Open' AND C4.Category <> 'welfare' AND C4.CustType <> 'IFS' AND A2.Resolution = '' GROUP BY A2.GroupName), 0) As '60+',
ISNULL((SELECT COUNT(C5.CallID) FROM [HEAT].[heat].[CallLog] C5 LEFT JOIN [HEAT].[heat].[Asgnmnt] A2 ON C5.CallID = A2.CallID WHERE C5.RecvdDate <= DATEADD(dd, -91, GETDATE()) AND C5.RecvdDate >= DATEADD(dd, -120, GETDATE()) AND A2.GroupName = A1.GroupName AND C5.CallStatus = 'Open' AND C5.Category <> 'welfare' AND C5.CustType <> 'IFS' AND A2.Resolution = '' GROUP BY A2.GroupName), 0) As '90+',
ISNULL((SELECT COUNT(C6.CallID) FROM [HEAT].[heat].[CallLog] C6 LEFT JOIN [HEAT].[heat].[Asgnmnt] A2 ON C6.CallID = A2.CallID WHERE C6.RecvdDate <= DATEADD(dd, -121, GETDATE()) AND A2.GroupName = A1.GroupName AND C6.CallStatus = 'Open' AND C6.Category <> 'welfare' AND C6.CustType <> 'IFS' AND A2.Resolution = '' GROUP BY A2.GroupName), 0) As '120+',
ISNULL((SELECT COUNT(C7.CallID) FROM [HEAT].[heat].[CallLog] C7 LEFT JOIN [HEAT].[heat].[Asgnmnt] A2 ON C7.CallID = A2.CallID WHERE C7.RecvdDate <= DATEADD(dd, -15, GETDATE()) AND A2.GroupName = A1.GroupName AND C7.CallStatus = 'Open' AND C7.Category <> 'welfare' AND C7.CustType <> 'IFS' AND A2.Resolution = '' GROUP BY A2.GroupName), 0) As 'Total'
FROM
[HEAT].[heat].[CallLog] C1
LEFT JOIN
[HEAT].[heat].[Asgnmnt] A1 ON C1.CallID = A1.CallID
WHERE
C1.CallStatus = 'Open'
AND C1.Category <> 'welfare'
AND C1.CustType <> 'IFS'
AND A1.Resolution = ''
AND (A1.GroupName = 'WAN' OR
(A1.GroupName = 'EITS Desktop Support'
AND A1.Assignee = 'EITS Desktop Support'
AND C1.CallType <> 'Information') OR
A1.GroupName = 'EITS Help Desk' OR
(A1.GroupName = 'Help Desk' AND A1.Assignee = 'EITS PIV Badge') OR
A1.GroupName = 'Internet' OR
A1.GroupName = 'Telecomm' OR
A1.GroupName = 'Programmers' OR
A1.GroupName = 'Unix' OR
A1.GroupName = 'Web' OR
A1.GroupName = 'Microwave')
GROUP BY
A1.GroupName
ORDER BY
COUNT(*) DESC;
结果:
Group |15+ |30+ |60+ |90+ |120+ |Total
WAN |20 |17 |5 |2 |44 |100
EITS Desktop Support |27 |15 |36 |0 |15 |96
Telecomm |12 |15 |0 |1 |9 |50
Server |3 |8 |1 |0 |21 |34
Web |0 |6 |2 |2 |14 |30
Unix |1 |0 |0 |0 |27 |29
DBA/Apps |0 |5 |3 |4 |21 |35
EITS Help Desk |4 |2 |0 |0 |0 |6
PIV Badge |4 |8 |1 |8 |44 |74
Microwave |0 |3 |0 |0 |13 |16
当使用下一个查询尝试查看哪些门票应该出现时,对于EG&#34; PIV BADGE&#34;它显示的门票是不同的。这是因为这个查询似乎只是将数据限制为像它应该的那样,我的问题是为什么不是第一个查询做同样的事情?
SELECT '15+' AS 'Days', C1.CallID AS 'Ticket#', A1.WhoAcknow AS 'Acknowledged By', C1.RecvdDate AS 'Received On', C1.Category, COUNT(*) As Assignments FROM [HEAT].[heat].[CallLog] C1 LEFT JOIN [HEAT].[heat].[Asgnmnt] A1 ON C1.CallID = A1.CallID WHERE C1.RecvdDate <= DATEADD(dd, -15, GETDATE()) AND C1.RecvdDate >= DATEADD(dd, -30, GETDATE()) AND C1.CallStatus = 'Open' AND C1.Category <> 'welfare' AND C1.CustType <> 'IFS' AND A1.Resolution = '' AND (A1.GroupName = 'Help Desk' AND A1.Assignee = 'EITS PIV Badge') GROUP BY C1.CallID, A1.WhoAcknow, C1.RecvdDate, C1.Category UNION ALL
SELECT '30+' AS 'Days', C1.CallID AS 'Ticket#', A1.WhoAcknow AS 'Acknowledged By', C1.RecvdDate AS 'Received On', C1.Category, COUNT(*) As Assignments FROM [HEAT].[heat].[CallLog] C1 LEFT JOIN [HEAT].[heat].[Asgnmnt] A1 ON C1.CallID = A1.CallID WHERE C1.RecvdDate <= DATEADD(dd, -31, GETDATE()) AND C1.RecvdDate >= DATEADD(dd, -60, GETDATE()) AND C1.CallStatus = 'Open' AND C1.Category <> 'welfare' AND C1.CustType <> 'IFS' AND A1.Resolution = '' AND (A1.GroupName = 'Help Desk' AND A1.Assignee = 'EITS PIV Badge') GROUP BY C1.CallID, A1.WhoAcknow, C1.RecvdDate, C1.Category UNION ALL
SELECT '60+' AS 'Days', C1.CallID AS 'Ticket#', A1.WhoAcknow AS 'Acknowledged By', C1.RecvdDate AS 'Received On', C1.Category, COUNT(*) As Assignments FROM [HEAT].[heat].[CallLog] C1 LEFT JOIN [HEAT].[heat].[Asgnmnt] A1 ON C1.CallID = A1.CallID WHERE C1.RecvdDate <= DATEADD(dd, -61, GETDATE()) AND C1.RecvdDate >= DATEADD(dd, -70, GETDATE()) AND C1.CallStatus = 'Open' AND C1.Category <> 'welfare' AND C1.CustType <> 'IFS' AND A1.Resolution = '' AND (A1.GroupName = 'Help Desk' AND A1.Assignee = 'EITS PIV Badge') GROUP BY C1.CallID, A1.WhoAcknow, C1.RecvdDate, C1.Category UNION ALL
SELECT '90+' AS 'Days', C1.CallID AS 'Ticket#', A1.WhoAcknow AS 'Acknowledged By', C1.RecvdDate AS 'Received On', C1.Category, COUNT(*) As Assignments FROM [HEAT].[heat].[CallLog] C1 LEFT JOIN [HEAT].[heat].[Asgnmnt] A1 ON C1.CallID = A1.CallID WHERE C1.RecvdDate <= DATEADD(dd, -91, GETDATE()) AND C1.RecvdDate >= DATEADD(dd, -120, GETDATE()) AND C1.CallStatus = 'Open' AND C1.Category <> 'welfare' AND C1.CustType <> 'IFS' AND A1.Resolution = '' AND (A1.GroupName = 'Help Desk' AND A1.Assignee = 'EITS PIV Badge') GROUP BY C1.CallID, A1.WhoAcknow, C1.RecvdDate, C1.Category UNION ALL
SELECT '120+' AS 'Days', C1.CallID AS 'Ticket#', A1.WhoAcknow AS 'Acknowledged By', C1.RecvdDate AS 'Received On', C1.Category, COUNT(*) As Assignments FROM [HEAT].[heat].[CallLog] C1 LEFT JOIN [HEAT].[heat].[Asgnmnt] A1 ON C1.CallID = A1.CallID WHERE C1.RecvdDate <= DATEADD(dd, -121, GETDATE()) AND C1.CallStatus = 'Open' AND C1.Category <> 'welfare' AND C1.CustType <> 'IFS' AND A1.Resolution = '' AND (A1.GroupName = 'Help Desk' AND A1.Assignee = 'EITS PIV Badge') GROUP BY C1.CallID, A1.WhoAcknow, C1.RecvdDate, C1.Category ORDER BY C1.RecvdDate ASC
结果:
Days |Ticket# |Acknowledged By |Received On |Category |Assignments
120+ |01114360 |NULL |2014-07-16 |Helpdesk |1
120+ |01160133 |NULL |2014-12-01 |Helpdesk |1
30+ |01190768 |NULL |2015-02-24 |Helpdesk |1
30+ |01194911 |NULL |2015-03-06 |Helpdesk |1
15+ |01203392 |NULL |2015-03-26 |Helpdesk |1
15+ |01206822 |NULL |2015-04-03 |Helpdesk |1
我做错了什么?
答案 0 :(得分:2)
我重写了您的初步查询,所以希望您不要介意。但这是我提出的:
WITH t
AS (
SELECT CASE A1.GroupName
WHEN 'Internet'
THEN 'Server'
WHEN 'Programmers'
THEN 'DBA/Apps'
WHEN 'Help Desk'
THEN 'PIV Badge'
ELSE A1.GroupName
END AS [Group]
,CASE
WHEN DATEDIFF(day, C1.RecvdDate, GETDATE()) > 15
AND DATEDIFF(day, C1.RecvdDate, GETDATE()) <= 30
THEN 1
ELSE 0
END AS '15+'
,CASE
WHEN DATEDIFF(day, C1.RecvdDate, GETDATE()) > 30
AND DATEDIFF(day, C1.RecvdDate, GETDATE()) <= 60
THEN 1
ELSE 0
END AS '30+'
,CASE
WHEN DATEDIFF(day, C1.RecvdDate, GETDATE()) > 60
AND DATEDIFF(day, C1.RecvdDate, GETDATE()) <= 90
THEN 1
ELSE 0
END AS '60+'
,CASE
WHEN DATEDIFF(day, C1.RecvdDate, GETDATE()) > 90
AND DATEDIFF(day, C1.RecvdDate, GETDATE()) <= 120
THEN 1
ELSE 0
END AS '90+'
,CASE
WHEN DATEDIFF(day, C1.RecvdDate, GETDATE()) > 120
THEN 1
ELSE 0
END AS '120+'
FROM [HEAT].[heat].[CallLog] C1
LEFT JOIN [HEAT].[heat].[Asgnmnt] A1 ON C1.CallID = A1.CallID
WHERE C1.CallStatus = 'Open'
AND C1.Category <> 'welfare'
AND C1.CustType <> 'IFS'
AND A1.Resolution = ''
AND (
(
A1.GroupName = 'EITS Desktop Support'
AND A1.Assignee = 'EITS Desktop Support'
AND C1.CallType <> 'Information'
)
OR (
A1.GroupName = 'Help Desk'
AND A1.Assignee = 'EITS PIV Badge'
)
OR A1.GroupName IN (
'WAN'
,'EITS Help Desk'
,'Internet'
,'Telecomm'
,'Programmers'
,'Unix'
,'Web'
,'Microwave'
)
)
)
SELECT t.[group]
,sum(t.[15+]) AS '15+'
,sum(t.[30+]) AS '30+'
,sum(t.[60+]) AS '60+'
,sum(t.[90+]) AS '90+'
,sum(t.[120+]) AS '120+'
,(sum(t.[15+]) + sum(t.[30+]) + sum(t.[60+]) + sum(t.[90+]) + sum(t.[120+])) AS total
FROM t
WHERE t.[Group] = 'PIV Badge'
GROUP BY t.[Group];