0

Sorry for the beginner question. But how can I put these queries into one data set rather than multiple queries? They are all being taken out of the same table. Also as you can see there is an "Open_Time" This is a DATE format. How can I write that to say AND open_date is within the last 60 days?

SELECT COUNT(P_NUMBER)
FROM PROBLEM_REPORT
WHERE Assignment='Crosby'
AND Severity=4
AND Open_Time<=60;

SELECT COUNT(P_NUMBER)
FROM PROBLEM_REPORT
WHERE Assignment='Crosby'
AND Severity=5
AND Open_Time<=60;

SELECT COUNT(P_NUMBER)
FROM PROBLEM_REPORT
WHERE Assignment='Crosby'
AND Severity=4
AND Close_Time<=60;

SELECT COUNT(P_NUMBER)
FROM PROBLEM_REPORT
WHERE Assignment='Crosby'
AND Severity=4
AND Close_Time<=60;

SELECT COUNT(P_NUMBER)
FROM PROBLEM_REPORT
WHERE Assignment='EUC'
AND Severity=4
AND Open_Time<=60;

SELECT COUNT(P_NUMBER)
FROM PROBLEM_REPORT
WHERE Assignment='EUC'
AND Severity=5
AND Open_Time<=60;

SELECT COUNT(P_NUMBER)
FROM PROBLEM_REPORT
WHERE Assignment='EUC'
AND Severity=4
AND Close_Time<=60;

SELECT COUNT(P_NUMBER)
FROM PROBLEM_REPORT
WHERE Assignment='EUC'
AND Severity=4
AND Close_Time<=60;
4

3 回答 3

1

试试这个 sql。

SELECT COUNT(CASE WHEN Assignment = 'Crosby' AND Severity = 4 AND DATEDIFF(CURDATE(),Open_Time)<=60 THEN P_NUMBER END) as p1,
    COUNT(CASE WHEN Assignment = 'Crosby' AND Severity = 5 AND DATEDIFF(CURDATE(),Open_Time)<=60  THEN P_NUMBER END) as p2,
    COUNT(CASE WHEN Assignment = 'Crosby' AND Severity = 4 AND DATEDIFF(CURDATE(),Close_Time)<=60 THEN P_NUMBER END) as p3,
    COUNT(CASE WHEN Assignment = 'Crosby' AND Severity = 5 AND DATEDIFF(CURDATE(),Close_Time)<=60 THEN P_NUMBER END) as p4,
    COUNT(CASE WHEN Assignment = 'EUC' AND Severity = 4 AND DATEDIFF(CURDATE(),Open_Time)<=60 THEN P_NUMBER END) as p5,
    COUNT(CASE WHEN Assignment = 'EUC' AND Severity = 5 AND DATEDIFF(CURDATE(),Open_Time)<=60 THEN P_NUMBER END) as p6,
    COUNT(CASE WHEN Assignment = 'EUC' AND Severity = 4 AND DATEDIFF(CURDATE(),Close_Time)<=60 THEN P_NUMBER END) as p7,
    COUNT(CASE WHEN Assignment = 'EUC' AND Severity = 5 AND DATEDIFF(CURDATE(),Close_Time)<=60 THEN P_NUMBER END) as p8
FROM PROBLEM_REPORT
WHERE Assignment IN('EUC','Crosby')
AND Severity IN(4,5)
于 2013-06-03T16:03:24.387 回答
1

这样做的一种方法:

SELECT 
SUM(CASE WHEN Assignment = 'Crosby' AND Severity=4 AND Open_Time<=60 THEN 1 ELSE 0 END) AS P_number1
,SUM(CASE WHEN Assignment = 'Crosby' AND Severity=5 AND Open_Time<=60 THEN 1 ELSE 0 END) AS P_number2
,SUM(CASE WHEN Assignment = 'Crosby' AND Severity=4 AND close_time<=60 THEN 1 ELSE 0 END) AS P_number3
,SUM(CASE WHEN Assignment = 'Crosby' AND Severity=5 AND close_time<=60 THEN 1 ELSE 0 END) AS P_number4
,SUM(CASE WHEN Assignment = 'EUC' AND Severity=4 AND Open_Time<=60 THEN 1 ELSE 0 END) AS P_number5
,SUM(CASE WHEN Assignment = 'EUC' AND Severity=5 AND Open_Time<=60 THEN 1 ELSE 0 END) AS P_number6
,SUM(CASE WHEN Assignment = 'EUC' AND Severity=4 AND close_time<=60 THEN 1 ELSE 0 END) AS P_number7
,SUM(CASE WHEN Assignment = 'EUC' AND Severity=5 AND close_time<=60 THEN 1 ELSE 0 END) AS P_number8
FROM PROBLEM_REPORT
于 2013-06-03T16:04:46.800 回答
0

查询中的ThereWHERE子句将使查询速度更快,因为它只会对过滤后的行进行计算。

SELECT  COUNT(CASE WHEN Assignment = 'Crosby' AND Severity = 4 AND Open_Time <= 60 THEN P_NUMBER END),
        COUNT(CASE WHEN Assignment = 'Crosby' AND Severity = 5 AND Open_Time <= 60 THEN P_NUMBER END),
        COUNT(CASE WHEN Assignment = 'Crosby' AND Severity = 4 AND Close_Time <= 60 THEN P_NUMBER END),
        COUNT(CASE WHEN Assignment = 'Crosby' AND Severity = 5 AND Close_Time <= 60 THEN P_NUMBER END),
        COUNT(CASE WHEN Assignment = 'EUC' AND Severity = 4 AND Open_Time <= 60 THEN P_NUMBER END),
        COUNT(CASE WHEN Assignment = 'EUC' AND Severity = 5 AND Open_Time <= 60 THEN P_NUMBER END),
        COUNT(CASE WHEN Assignment = 'EUC' AND Severity = 4 AND Close_Time <= 60 THEN P_NUMBER END),
        COUNT(CASE WHEN Assignment = 'EUC' AND Severity = 5 AND Close_Time <= 60 THEN P_NUMBER END)
FROM    PROBLEM_REPORT
WHERE   Assignment IN ('Crosby', 'EUC') AND
        Severity IN (4,5) AND
        (Open_Time <= 60 OR Close_Time <= 60)
于 2013-06-03T16:05:19.647 回答