❶ sql語句查詢,怎麼把不同年份和月份的數據,按年 月來分組。
將你的日期欄位格式化為年月格式:DATE_FORMAT(date, '%Y-%m'),然後再用格式化的日期分組即可
❷ SQL中獲取某一年中有多少個星期
with cte as
(
select dt=dateadd(d,number,'1916-01-01')
from master..spt_values
where type = 'P' and dateadd(d,number,'1916-01-01') <= '1916-12-31'
)
select top 1 weekNum=datepart(wk,dt) from cte group by datepart(wk,dt) order by datepart(wk,dt) desc
調試通過,自己換下時間段
❸ sqlserver語句,按周分組統計數據,請各位大神指教小弟。
---把getdate()替換成你的日期欄位就可以了, between and的2個日期分別是你說的2個年頭年尾的情況
select case when getdate()>=cast('2014/1/1' as datetime) then datepart(week,getdate()) else 1 end as 第幾周
--from table_name
where getdate() between (case when datepart(weekday,cast('2014/1/1' as datetime))<>1 then
cast('2014/1/1' as datetime)-datepart(weekday,cast('2014/1/1' as datetime))+2
end )
and
(case when datepart(weekday,cast('2014/12/31' as datetime))<>6 then
cast('2014/12/31' as datetime)-datepart(weekday,cast('2014/12/31' as datetime))+1
end )
❹ sql server如何按季度分組統計所有的數據
和按月份組的原理是一樣的吧!
按月分組
按季度分組和按月分組的區別應該就是時間段的區別吧!
selectcasewhenmonth(date)=1ormonth(date)=2
ormonth(date)=3then'一季度'
whenmonth(date)=4ormonth(date)=5
ormonth(date)=6then'2季度'
whenmonth(date)=7ormonth(date)=8
ormonth(date)=9then'3季度'
whenmonth(date)=10ormonth(date)=11
ormonth(date)=12then'4季度'
else''end,sum(數量)
fromtable
groupby
casewhenmonth(date)=1ormonth(date)=2
ormonth(date)=3then'一季度'
whenmonth(date)=4ormonth(date)=5
ormonth(date)=6then'2季度'
whenmonth(date)=7ormonth(date)=8
ormonth(date)=9then'3季度'
whenmonth(date)=10ormonth(date)=11
ormonth(date)=12then'4季度'
else''end
❺ sql怎麼把一年中的每周數都顯示出來
LZ給的紅包實在少點。回頭給補上呀!
----------------------------
這個題的難度不再怎麼顯示周,而是中國的文化問題,中國把周一當第一天,而中國用的大多數資料庫都是國外的,國外基本都是把周日當第一天,所以按周統計或排序的基本都有誤差,很多函數也用不了。
使用的是SqlServer資料庫,別的資料庫也會有以上問題,LZ自己多思考,方法如下:
(datepart(week, [CreateDate] + @@DateFirst - 1)-1) 才是真正的在中國能用的一年中的第幾周。記住這是一年中的,不能跨年份,跨年份要用where 過濾年份。
第一:排序輸出
SELECT [CreateDate], (datepart(week, [CreateDate] + @@DateFirst - 1)-1)
FROM [ST_Inventory]
order by (datepart(week, [CreateDate] + @@DateFirst - 1)-1)
第二:分組輸出
SELECT MAX([CreateDate]), (datepart(week, [CreateDate] + @@DateFirst - 1)-1)
FROM [ST_Inventory]
group by (datepart(week, [CreateDate] + @@DateFirst - 1)-1)
❻ sql語句按照日期分組怎麼寫
1.如果你的時間格式是2012-01-13
17:35:52.217這樣的形式,(主要是那個01不要是1),用下面這個
SELECT
convert(varchar(10),時間,23),SUM(
數字數據
)
FROM
表
group
by
convert(varchar(10),時間,23)
2.如果你的時間格式不是上面的格式,先轉化成datetime:
SELECT
convert(varchar(10),cast(時間
as
datetime),23)
,SUM(數字數據)
FROM
表
group
by
convert(varchar(10),cast(時間
as
datetime),23)
3.如果報錯,請追問~
❼ sql如何按周一、周二。。。周日 分組查詢
使用日期的datepart函數。
select DATEPART(WEEKDAY,getdate())
❽ sql查詢一年數據,以昨日8點到今日8點為今日數據,按日期排列,如何破
column_a>=時間A and column_a <= dateadd(day,時間A,1) order by column_a asc
❾ sql 實現按日期分組
需要用convert函數轉換日期格式,並且需要用group by來實現分組。
1、創建測試表及插入數據:
createtabletest
(tdatedatetime,
salint);
insertintotestvalues('2015-09-2312:22:22',100);
insertintotestvalues('2015-09-2301:54:34',456);
insertintotestvalues('2015-09-2414:32:35',45);
insertintotestvalues('2015-09-2422:23:43',67);
insertintotestvalues('2015-09-2519:43:22',234);
insertintotestvalues('2015-09-2508:14:12',67);
insertintotestvalues('2015-09-2604:53:34',45);
insertintotestvalues('2015-09-2609:46:54',78);
2、執行sql語句:
selectconvert(varchar(10),tdate,120)tdate,sum(sal)salfromtestgroupbyconvert(varchar(10),tdate,120);
3、執行結果:
❿ sql按一年的每一周查詢數據
取得指令時間的周數,再按該周數分組。
select
datepart(wk,指令時間),sum(數量)
from
表
group
by
datepart(wk,指令時間)
將該sql語句中文字的部分換成你的內容。