如何根据日期统计数据?

时间:2014-06-09 21:52:57

标签: sql sql-server-2008

我有一个查询,得到我得到一个员工的COUNT 其中execoffice_status = 1并且有效:

SELECT employeedept, execoffice_status, employee ,COUNT(*) AS 'employeetotal'
    FROM CSEReduxResponses
    WHERE execoffice_status = 1
    GROUP BY execoffice_status, employee,employeedept

我想要做的是根据execoffice_date的日期获取COUNT 我试图在这里做:

SELECT employeedept, execoffice_status, YEAR_cse =YEAR(execoffice_date),employee ,COUNT(*) AS 'employeetotal'
FROM CSEReduxResponses
WHERE execoffice_status = 1
GROUP BY execoffice_status, employee,employeedept,execoffice_date

我想按日期得到它,我写的新查询并没有得到我想要的信息。 哪个是给我COUNT取决于execoffice_date是什么年份。什么做的是给我它不 甚至对我有意义。

我创建了一个虚拟表,我不能把它放在sqlfiddle上(不知道其他任何地方放它)因为 不是sqlfiddle不能正常工作,必须关闭。

 create table CSEReduxResponses (employeedept int, execoffice_status int, employee int, execoffice_date datetime);
    insert into (employeedept , execoffice_status , employee , execoffice_date )
    values (1,1,10,'2014-05-08'),
    (1,1,10,'2014-05-08'),
    (1,1,10,'2014-05-08'),
    (2,2,11,'2014-05-08'),
    (2,2,11,'2015-05-08'),
    (2,2,11,'2015-05-08'),
    (2,2,11,'2015-05-08'),
    (2,2,11,'2015-04-08');

从上面的数据我想得到

employeedept | execoffice_status | employee | totalstars | year_cse
--------------------------------------------------------------------
1            | 1                  | 10      |3          |2014
2            | 1                  | 11      |1           |2014
2            | 1                  | 11      |4           |2015

2 个答案:

答案 0 :(得分:1)

更新:根据新信息进行更改

SELECT employeedept, execoffice_status, employee, COUNT(*) AS 'totalstars', YEAR_cse =YEAR(execoffice_date)
FROM CSEReduxResponses
WHERE execoffice_status = 1
GROUP BY employeedept, execoffice_status, YEAR(execoffice_date), employee 

答案 1 :(得分:0)

如果这不正确,您应该尝试更清楚地指定您想要的内容,包括样本数据和所需结果。

SELECT execoffice_date, COUNT(*) AS 'employeetotal'
    FROM CSEReduxResponses
    GROUP BY execoffice_date;