博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sql 根据年份、月份查询数据
阅读量:5337 次
发布时间:2019-06-15

本文共 2550 字,大约阅读时间需要 8 分钟。

CREATE TABLE [dbo].[T_UserAccess](    [Id] [int] IDENTITY(1,1) NOT NULL,    [UserId] [int] NULL,    [UserType] [int] NULL,    [OpenId] [nvarchar](50) NULL,    [UnionId] [nvarchar](50) 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 根据年份、月份查询数据

SELECT ISNULL(NEWID(), 'd1e57ca7-6eee-495a-be13-73d5e7d51f36') AS Id,       userAccess.Year,       Sum(January) January,       Sum(February) February,       Sum(March) March,       Sum(April) April,       Sum(May) May,       Sum(June) June,       Sum(July) July,       Sum(August) August,       Sum(September) September,       Sum(October) October,       Sum(November) November,       Sum(December) DecemberFROM  ( SELECT years AS 'Year',           CASE               WHEN months=1 THEN counts               ELSE 0           END 'January',           CASE                WHEN months=2 THEN counts                ELSE 0            END 'February',                CASE                WHEN months=3 THEN counts                ELSE 0                END 'March',            CASE                WHEN months=4 THEN counts                ELSE 0                END 'April',                CASE                WHEN months=5 THEN counts                ELSE 0                END 'May',            CASE                WHEN months=6 THEN counts                ELSE 0                END 'June',            CASE                WHEN months=7 THEN counts                ELSE 0                END 'July',            CASE                WHEN months=8 THEN counts                ELSE 0                END 'August',            CASE                WHEN months=9 THEN counts                ELSE 0                END 'September',            CASE                WHEN months=10 THEN counts                ELSE 0                END 'October',            CASE                WHEN months=11 THEN counts                ELSE 0                END 'November',            CASE                WHEN months=12 THEN counts                ELSE 0                END 'December'   FROM     (SELECT datepart(yy,CreationTime) AS years,             datepart(mm,CreationTime) months,                                       count(1)AS counts      FROM [T_UserAccess]      GROUP BY year(CreationTime),               month(CreationTime))AS ua ) AS userAccessGROUP BY Year

 

转载于:https://www.cnblogs.com/zyj649261718/p/6323258.html

你可能感兴趣的文章
Linux命令学习手册-printf命令(转)
查看>>
理解Lock例子
查看>>
Spring课程 Spring入门篇 6-3 ProxyFactoryBean及相关内容(下)
查看>>
Javascript禁止父元素滚动条滚动, pc、移动端均有效
查看>>
跳转网页
查看>>
Silverlight 4 安装出现语言版本错误
查看>>
Matlab——表达式 阵列与矩阵的创建
查看>>
匿名管道 双向通信 需要两个
查看>>
Android系统手机端抓包方法
查看>>
Vue的filter属性
查看>>
PHP CI 框架简单使用(二)
查看>>
RabbitMq
查看>>
POJ2155:Matrix
查看>>
scala 打印一个乘法口诀表 (<<scala 编程>> P87)
查看>>
Python虚拟环境
查看>>
在Bootstrap4中使用垂直居中
查看>>
个人作业——软件产品案例分析
查看>>
测试时test
查看>>
15分钟学会使用Git和远程代码库
查看>>
八皇后问题的实现
查看>>