标签:group by target cts sele prim 数据 obj get exists
https://bbs.csdn.net/topics/392217817?page=1
问题:
--创建测试表 IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N‘[dbo].[TB]‘) AND type in (N‘U‘)) DROP TABLE [dbo].[TB1] GO CREATE TABLE [dbo].[TB1]( [卡状态] [nvarchar] (20) NULL, [卡类型] [nvarchar](50) NULL, ) ON [PRIMARY] GO --插入测试数据 INSERT INTO [TB1] ([卡状态],[卡类型]) SELECT N‘发卡‘,N‘正式卡‘ UNION ALL SELECT N‘发卡‘,N‘正式卡‘ UNION ALL SELECT N‘发卡‘,N‘临式卡‘ UNION ALL SELECT N‘制卡‘,N‘临式卡‘ UNION ALL SELECT N‘制卡‘,N‘正式卡‘ GO SELECT * FROM TB1
回帖:
SELECT 卡状态 + ‘数‘ , COUNT(1) AS 总数 , SUM(CASE WHEN 卡类型 = ‘正式卡‘ THEN 1 ELSE 0 END) AS 正式卡 , SUM(CASE WHEN 卡类型 = ‘临式卡‘ THEN 1 ELSE 0 END) AS 临式卡 FROM TB1 GROUP BY 卡状态
标签:group by target cts sele prim 数据 obj get exists
原文地址:https://www.cnblogs.com/yadongliang/p/9120831.html