[英]How to count data depending on the date?
我有一個查詢,我得到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
我想按日期獲取它,而我編寫的新查詢並沒有給我我想要的信息。 根據execoffice_date的年份,這可以給我COUNT。正在給我的東西對我來說甚至沒有意義。
我創建了一個虛擬表,我無法將其放在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
更新:根據新信息進行更改
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
如果這是不正確的,則應嘗試更清楚地指定所需的內容,包括樣本數據和所需的結果。
SELECT execoffice_date, COUNT(*) AS 'employeetotal'
FROM CSEReduxResponses
GROUP BY execoffice_date;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.