sql按月統計
A. sql按月統計,按日分組
declare @yf int
declare @rqxx varchar (30)
declare @rqsx varchar (30)
set @yf = '' --輸入月份
set @rqxx =convert(varchar(30),@yf-1)+'月'+'26日'+'14:30:00'
set @rqxx =convert(varchar(30),@yf)+'月'+'25日'+'14:30:00'
select sum(交易量) from a where 交易時間 between @rqxx and @rqsx
這個只是個思路
上面我把@rqxx,@rqsx 定義為 varchar 你在具體定義時 要和a中交易時間一致,時間格式也要改 而且這個月份只能實現 2月--12月查詢 有需要的話自己改下
B. SQL璇鍙ャ傛湁涓鍫嗘暟鎹鍑犱竾鏉°傛椂闂磋寖鍥2010-05-01鍒頒粖澶┿傜幇鍦ㄨ佺粺璁″嚭姣忓ぉ錛屾瘡涓鏈堟湁澶氬皯鏉℃暟鎹璺熴
姣忓ぉ
select count(1),to_char(StartTime,'yyyy-mm-dd') from tablename where UrlTarget like '%Agent%' and to_char(StartTime,'yyyy-mm-dd') >='2010-05-01' group by to_char(StartTime,'yyyy-mm-dd') order by to_char(StartTime,'yyyy-mm-dd') ;
姣忔湀
select count(1),to_char(StartTime,'mm') from tablename where UrlTarget like '%Agent%' and to_char(StartTime,'yyyy-mm-dd') >='2010-05-01' group by to_char(StartTime,'mm') order by to_char(StartTime,'mm') ;
C. sql多表聯合按月統計全年的數據,該怎麼處理
具體的語句跟數據結構和統計要求有關,請祥豎扒參考下謹昌列SQL語句
假設有數據表
產量1(id,日期,產量)
產量2(id,日期,產量)
匯總纖伍出兩張表2017年每月的總產量
selectmonth(t.日期)as月份,sum(t.產量)as月產量
from(select日期,產量from產量1where日期
between'2017-01-01'and'2017-12-31'unionall
select日期,產量from產量2where日期
between'2017-01-01'and'2017-12-31')t
groupbymonth(t.日期);
D. sql 鍒嗙被鎸夋湀緇熻
鏂規硶涓錛氳繖縐嶆柟娉曞垪鍚嶆槸鍥哄畾鐨勩
select 鍚嶇О,
sum(case when substring(convert(varchar(7),鏃ユ湡,120),6,2)='09' then 鍑哄敭鏁伴噺 else 0 end) as '9鏈',
sum(case when substring(convert(varchar(7),鏃ユ湡,120),6,2)='10' then 鍑哄敭鏁伴噺 else 0 end) as '10鏈'
from 琛
group by 鍚嶇О
鏂規硶浜岋細榪欑嶆柟娉曡櫧鐒墮夯鐑︿竴浜涳紝浣嗘槸鏀鎸佸垪鍚嶄笉紜瀹氱殑鎯呭喌銆
declare @sql varchar(2000)
declare @mon varchar(2)
set @sql = 'select 鍚嶇О'
select @sql = @sql + ',sum(case mon when '''+mon+''' then 鍑哄敭鏁伴噺 end) ['+mon+'鏈圿'
from (select distinct substring(convert(varchar(7),鏃ユ湡,120),6,2) mon from 琛) as a
select @sql = @sql+' from 琛 group by 鍚嶇О'
exec(@sql)