轉載出處:https://blog.csdn.net/qq285679784/article/details/80426762
-
eg:
-
selectasset_info_src_cd,count(*) from asset wheredatediff(week,asset_setup_dt,getdate())=0
-
group by asset_info_src_cd
-
eg:
-
select asset_info_src_cd,count (asset_info_src_cd) from asset where datepart(week,asset_setup_dt) =datepart(week,getdate())
-
group by asset_info_src_cd
-
eg:
-
select asset_info_src_cd,count(*) from asset where asset_setup_dt >'20150706' and asset_setup_dt <=getdate()
-
group by asset_info_src_cd
-
本月統計(MySQL)
-
select * from booking where month(booking_time) = month(curdate()) and year(booking_time) = year(curdate())
-
本週統計(MySQL)
-
select * from spf_booking where month(booking_time) = month(curdate()) and week(booking_time) = week(curdate())
-
[SQLServer]
-
表名爲:tableName
-
時間字段名爲:theDate
-
查詢本月的記錄
-
select * from tableName where DATEPART(mm, theDate) = DATEPART(mm, GETDATE())
-
and DATEPART(yy, theDate) = DATEPART(yy, GETDATE())
-
查詢本週的記錄
-
select * from tableName where DATEPART(wk, theDate) = DATEPART(wk, GETDATE())
-
and DATEPART(yy, theDate) = DATEPART(yy, GETDATE())
-
查詢本季的記錄
-
select * from tableName where DATEPART(qq, theDate) = DATEPART(qq, GETDATE())
-
and DATEPART(yy, theDate) = DATEPART(yy, GETDATE())
-
其中:GETDATE()是獲得系統時間的函數。
-
如:
-
表:consume_record
-
字段:consume (money類型)
-
date (datetime類型)
-
請問怎麼寫四條sql語句分別按日,按周,按月,按季統計消費總量.
-
如:1月 1200元
-
2月 3400元
-
3月 2800元
-
--按日
-
select sum(consume),day([date]) from consume_record where year([date]) = '2006'
-
group by day([date])
-
--按周quarter
-
select sum(consume),datename(week,[date]) from consume_record where year([date]) = '2006'
-
group by datename(week,[date])
-
--按月
-
select sum(consume),month([date]) from consume_record where year([date]) = '2006'
-
group by month([date])
-
--按季
-
select sum(consume),datename(quarter,[date]) from consume_record where year([date]) = '2006'
-
group by datename(quarter,[date])
-
--指定日期你就看上面的例子變通下呀,無非就是一個聚合函數和Group by
-
select [date],sum(consume) from consume_record where [date] between '2006-06-01' and '2006-07-10'
-
group by [date]
-
統計博客聚合用戶點擊次數
-
SELECT author, SUM(hits) AS hits
-
FROM infos
-
GROUP BY author
-
ORDER BY hits DESC
-
統計昨天的記錄
-
SELECT *
-
FROM infos
-
WHERE (DATEDIFF(d, pubdate, GETDATE()) = 1)
-
統計本週的記錄
-
SELECT *
-
FROM infos
-
WHERE (DATEPART(yy, pubdate) = DATEPART(yy, GETDATE())) AND (DATEPART(week,
-
pubdate - 1) = DATEPART(week, GETDATE()))
-
統計本月的記錄:
-
SELECT *
-
FROM infos
-
WHERE (DATEPART(yy, pubdate) = DATEPART(yy, GETDATE())) AND (DATEPART([month],
-
pubdate - 1) = DATEPART([month], GETDATE()))
-
eg:
-
select asset_info_src_cd,count (asset_info_src_cd) from asset where datepart(week,asset_setup_dt) =datepart(week,getdate())
-
group by asset_info_src_cd
-
eg:
-
select asset_info_src_cd,count(*) from asset where asset_setup_dt >'20150706' and asset_setup_dt <=getdate()
-
group by asset_info_src_cd
SQL Server DATEPART() 函數
定義和用法
DATEPART() 函數用於返回日期/時間的單獨部分,比如年、月、日、小時、分鐘等等。
語法
DATEPART(datepart,date)
date 參數是合法的日期表達式。datepart 參數可以是下列的值:
datepart | 縮寫 |
---|---|
年 | yy, yyyy |
季度 | qq, q |
月 | mm, m |
年中的日 | dy, y |
日 | dd, d |
周 | wk, ww |
星期 | dw, w |
小時 | hh |
分鐘 | mi, n |
秒 | ss, s |
毫秒 | ms |
微妙 | mcs |
納秒 | ns |
實例
假設我們有下面這個 "Orders" 表:
OrderId | ProductName | OrderDate |
---|---|---|
1 | 'Computer' | 2008-12-29 16:25:46.635 |
我們使用如下 SELECT 語句:
SELECTDATEPART(yyyy,OrderDate)
AS OrderYear,DATEPART(mm,OrderDate)
AS OrderMonth,DATEPART(dd,OrderDate)
AS OrderDay FROM Orders WHERE OrderId=1
結果:
OrderYear | OrderMonth | OrderDay |
---|---|---|
2008 | 12 | 29 |