SQL queries data based on year and month

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.