問題描述
我有一個要求,比如必須提取用戶選擇的日期范圍內的所有記錄,選擇從 2011 年 1 月 15 日到 2011 年 8 月 20 日開始的所有員工,并按日期分組.
I have a requirement like have to pull all records in the date range the user selected, selecting all employees who started from 15-Jan-2011 to 20-Aug-2011 and group by date.
我應該如何為此編寫 SQL 查詢:
How should I write SQL query for this:
SELECT *
FROM employees
WHERE startdate >= '15-jan-2011'
AND startdate <= '20-aug-2011'
GROUP BY startdate
推薦答案
絕對可以.這將導致過濾日期范圍內的記錄,然后按有數據的每一天對其進行分組.
Absolutely. It will result in filtering the records on your date range and then grouping it by each day where there is data.
應該注意的是,您只能選擇開始日期,然后選擇您正在計算的任何聚合.否則,它應該可以正常工作.
It should be noted that you will only be able to select the startdate and then whatever aggregates you're calculating. Otherwise, it should work perfectly fine.
例如,此查詢將為您提供每個開始日期的員工數:
For example, this query will give you a count of employees for each startdate:
SELECT startdate, count(*)
FROM employees
WHERE startdate >= '15-jan-2011'
AND startdate <= '20-aug-2011'
GROUP BY startdate
這篇關于我們可以使用具有相同字段名的 group by 和 where 條件嗎的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!