CREATE TABLE [dbo].[t_useraccess]( [Id] [int] IDENTITY(1,1) not NULL, [UserId] [int] NULL, [usertype] [int] NULL, [OpenId] [nvarchar]( -)NULL, [Unionid] [nvarchar]( -)NULL, [CreationTime] [datetime] NULL, [AppId] [int] NULL, CONSTRAINT [pk_t_useraccess] PRIMARY KEY CLUSTERED ( [Id] ASC) with(Pad_index= OFF, Statistics_norecompute= OFF, Ignore_dup_key= OFF, Allow_row_locks= on, Allow_page_locks= on) on [PRIMARY]) on [PRIMARY]
SQL queries data based on year and month
SELECT ISNULL(NEWID(),'d1e57ca7-6eee-495a-be13-73d5e7d51f36') asId, useraccess. Year, Sum(January) January,Sum(February) February,Sum(March) March,Sum(April) April,SumMay ,Sum(June) June,Sum(July) July,Sum(August) August,Sum(September) September,Sum(October) October,Sum(November) November,Sum(December) December from ( SELECTYears as ' Year', Case whenMonths=1 ThencountsELSE 0 END 'January', Case whenMonths=2 ThencountsELSE 0 END 'February', Case whenMonths=3 ThencountsELSE 0 END 'March', Case whenMonths=4 ThencountsELSE 0 END 'April', Case whenMonths=5 ThencountsELSE 0 END ' May', Case whenMonths=6 ThencountsELSE 0 END 'June', Case whenMonths=7 ThencountsELSE 0 END 'July', Case whenMonths=8 ThencountsELSE 0 END 'August', Case whenMonths=9 ThencountsELSE 0 END 'September', Case whenMonths=Ten ThencountsELSE 0 END 'October', Case whenMonths= One ThencountsELSE 0 END 'November', Case whenMonths= A ThencountsELSE 0 END 'December' from (SELECT DatePart(Yy,creationtime) asyears,DatePart(mm,creationtime) months,Count(1) ascounts from [t_useraccess] GROUP by Year(creationtime),Month(CreationTime)) asUA) asuseraccessGROUP by Year
SQL queries data based on year and month