SQL - 计算每天特定列中的不同项目

时间:2013-07-05 22:32:55

标签: sql

Date                Time        Method          User                   App
2013/5/24        19:39:33        PUT        advantage_user        adv_cat_arch
2013/5/24        19:36:04        SETACL        advantage_user        adv_cat_arch
2013/5/24        19:36:11        PUT        advantage_user        adv_cat_arch
2013/5/24        19:36:12        SETACL        advantage_user        adv_cat_arch
2013/5/24        19:36:19        PUT        advantage_user        adv_cat_arch
2013/5/24        19:36:19        SETACL        advantage_user        adv_cat_arch
2013/5/24        19:36:27        PUT        advantage_user        adv_cat_arch

我想计算每一天的每一种方法,例如,

Date        Method(PUT)    Method(SETACL)  Method(Get)
2013/5/24       5                2             3
2013/5/25       2                1             5

这是我的SQL查询:

SELECT Fas_ops_metrics_test4_jun.Date as Date, 
       Fas_ops_metrics_test4_jun.Method as Method
FROM Fas_ops_metrics_test4_jun
GROUP BY Fas_ops_metrics_test4_jun.Method
ORDER BY Fas_ops_metrics_test4_jun.Method;
谁能帮帮我?谢谢

2 个答案:

答案 0 :(得分:1)

如果我理解正确,您可以使用COUNTCASE执行此操作:

SELECT Date, 
    COUNT(CASE WHEN Method = 'PUT' THEN 1 END) 'Method(PUT)',
    COUNT(CASE WHEN Method = 'SETACL' THEN 1 END) 'Method(SETACL)',
    COUNT(CASE WHEN Method = 'GET' THEN 1 END) 'Method(GET)'
FROM YourTable
GROUP BY Date

编辑,鉴于您使用的是MS Access,请使用IIF代替CASE,使用SUM可能更容易:

SELECT Date, 
    SUM(IIF(Method = 'PUT', 1, 0)) as 'Method(PUT)',
    SUM(IIF(Method = 'SETACL', 1, 0)) as 'Method(SETACL)',
    SUM(IIF(Method = 'GET', 1, 0)) as 'Method(GET)'
FROM YourTable
GROUP BY Date

答案 1 :(得分:0)

Select count (method) , method
from yourTable 
Where Date = '2013/5/24'
group by method

这将为您提供两列,每个方法和方法的计数。