定义下表:
CREATE TABLE Raw
(
PolicyNumber varchar(10) NOT NULL,
[Plan Code] varchar(10) NOT NULL,
CoverageNumber int NOT NULL,
[Coverage Issue Date] date NOT NULL,
IssueAge int NOT NULL,
InsuredDOB date NOT NULL,
TotalFace float NOT NULL,
[Termination Date] date NOT NULL,
TerminationReason varchar(10) NOT NULL,
Gender varchar(10) NOT NULL,
SmokerStatus varchar(10) NOT NULL,
AnnualizedPremium float NOT NULL,
Crcy varchar(10) NOT NULL
)
INSERT INTO [Raw]
VALUES
('3332808787','S20QA',1,CONVERT(date,'19990608',112),19,CONVERT(date,'19790807',112),20000,CONVERT(date,'20110107',112),'B','F','0',732,'90'),
('3157139132','S20QA',1,CONVERT(date,'19990609',112),33,CONVERT(date,'19650720',112),50000,CONVERT(date,'20060207',112),'B','F','0',2376,'90'),
('1622449413','S20QA',1,CONVERT(date,'19990408',112),21,CONVERT(date,'19770409',112),20000,CONVERT(date,'20070707',112),'B','F','0',756,'90'),
('1966749563','S20QA',1,CONVERT(date,'19990609',112),30,CONVERT(date,'19681008',112),100000,CONVERT(date,'20060907',112),'B','F','0',4488,'90'),
('4239149716','S30QA',1,CONVERT(date,'19990727',112),19,CONVERT(date,'19791206',112),10000,CONVERT(date,'20070225',112),'B','F','0',456,'90'),
('1532109564','025QA',1,CONVERT(date,'19990727',112),26,CONVERT(date,'19720808',112),10000,CONVERT(date,'20131226',112),'B','F','0',540,'90'),
('1737229652','025QA',1,CONVERT(date,'19990609',112),30,CONVERT(date,'19680610',112),10000,CONVERT(date,'20110310',112),'B','F','0',564,'90'),
('3613176106','S30QA',1,CONVERT(date,'19990917',112),21,CONVERT(date,'19771202',112),30000,CONVERT(date,'20060816',112),'B','F','0',1380,'90'),
('2812726759','S30QA',1,CONVERT(date,'19990924',112),21,CONVERT(date,'19780406',112),10000,CONVERT(date,'20120523',112),'B','F','0',442,'90'),
('1613214958','S20QA',1,CONVERT(date,'19990926',112),28,CONVERT(date,'19701229',112),10000,CONVERT(date,'20090825',112),'B','F','0',672,'90'),
('4124180592','D01DE',1,CONVERT(date,'20041105',112),28,CONVERT(date,'19760106',112),70000,CONVERT(date,'20161231',112),'1','F','0',506,'90'),
('2783562258','D01DE',1,CONVERT(date,'20041109',112),33,CONVERT(date,'19711020',112),50000,CONVERT(date,'20161231',112),'1','F','0',531,'90'),
('3641040493','D01DE',1,CONVERT(date,'20050606',112),45,CONVERT(date,'19600507',112),20000,CONVERT(date,'20150115',112),'D','F','0',480,'90'),
('2289932527','D01DE',1,CONVERT(date,'20010819',112),49,CONVERT(date,'19560520',112),40000,CONVERT(date,'20161231',112),'1','M','1',1990,'90'),
('3601742355','D01DE',1,CONVERT(date,'20030301',112),33,CONVERT(date,'19701002',112),50000,CONVERT(date,'20161231',112),'1','F','0',578,'90'),
('1830607130','D01DE',1,CONVERT(date,'20041218',112),31,CONVERT(date,'19741021',112),100000,CONVERT(date,'20150910',112),'B','F','0',854,'90'),
('2352605305','D01DE',1,CONVERT(date,'20020122',112),43,CONVERT(date,'19631210',112),50000,CONVERT(date,'20161231',112),'1','M','0',1555,'90'),
('2587270075','D01DE',1,CONVERT(date,'20040925',112),31,CONVERT(date,'19730321',112),20000,CONVERT(date,'20161231',112),'D','F','0',190,'90'),
('3733814516','D01DE',2,CONVERT(date,'20000925',112),32,CONVERT(date,'19730321',112),80000,CONVERT(date,'20160501',112),'B','F','0',758,'90'),
('1429938264','D01DE',1,CONVERT(date,'20011006',112),35,CONVERT(date,'19681107',112),20000,CONVERT(date,'20161231',112),'1','M','1',384,'90'),
('2041540074','025QA',1,CONVERT(date,'19990903',112),31,CONVERT(date,'19680116',112),10000,CONVERT(date,'20140218',112),'D','F','0',560,'90'),
('4287465979','020QA',1,CONVERT(date,'19990906',112),38,CONVERT(date,'19601025',112),20000,CONVERT(date,'20140222',112),'D','F','0',1344,'90'),
('1760875076','010QA',1,CONVERT(date,'20000327',112),48,CONVERT(date,'19510823',112),10000,CONVERT(date,'20070508',112),'D','F','0',1282,'90'),
('2433672739','S15VA',1,CONVERT(date,'20010305',112),36,CONVERT(date,'19641224',112),50000,CONVERT(date,'20110305',112),'D','F','0',3209,'90'),
('2821715890','020VA',1,CONVERT(date,'20011015',112),36,CONVERT(date,'19650526',112),10000,CONVERT(date,'20060507',112),'D','F','0',502,'90'),
('4056174634','010VA',1,CONVERT(date,'20011108',112),47,CONVERT(date,'19540731',112),10000,CONVERT(date,'20110720',112),'D','F','0',1006,'90'),
('4041953932','025VA',1,CONVERT(date,'20011230',112),31,CONVERT(date,'19701027',112),30000,CONVERT(date,'20090126',112),'D','F','0',1171,'90'),
('3929249579','D01VA',1,CONVERT(date,'20030621',112),55,CONVERT(date,'19480409',112),50000,CONVERT(date,'20151129',112),'D','F','0',2315,'90'),
('1473591771','D01VA',1,CONVERT(date,'20031027',112),22,CONVERT(date,'19810125',112),10000,CONVERT(date,'20110728',112),'D','M','0',36,'90'),
('3828362177','D01VA',1,CONVERT(date,'20030529',112),34,CONVERT(date,'19690312',112),50000,CONVERT(date,'20060622',112),'D','M','0',312,'90'),
('2030800439','D01VA',1,CONVERT(date,'20050518',112),29,CONVERT(date,'19750819',112),50000,CONVERT(date,'20130806',112),'B','F','0',311,'90'),
('1060719809','D01VA',1,CONVERT(date,'20031028',112),50,CONVERT(date,'19530324',112),10000,CONVERT(date,'20151202',112),'D','M','0',639,'90'),
('1688681321','010JR',1,CONVERT(date,'19990609',112),48,CONVERT(date,'19501216',112),10000,CONVERT(date,'20061206',112),'B','F','0',901,'90'),
('1963302451','030JR',1,CONVERT(date,'19990609',112),28,CONVERT(date,'19710126',112),20000,CONVERT(date,'20120823',112),'D','F','0',625,'90'),
('2973264800','S20JR',1,CONVERT(date,'19990610',112),25,CONVERT(date,'19731115',112),50000,CONVERT(date,'20150414',112),'B','F','0',1885,'90')
我想找出有多少个包含字符串 01 的计划代码,但我就是不知道如何在列中添加数字。
这是我想出的代码:
SELECT [Plan Code], COUNT(*) AS 'How Many'
FROM Raw
WHERE [Plan Code] LIKE '%01%'
GROUP BY [Plan Code];
它输出
计划代码 | 有多少 |
---|---|
010JR | 1 |
010QA | 1 |
010VA | 1 |
D01DE | 10 |
D01VA | 5 |
剩下要做的就是添加列中的数字(总计为 18),但我无法将其转换为代码。关于如何继续有什么想法吗?
只需从查询中删除 GROUP BY 子句和 [计划代码] 列即可。有用。在我的机器上测试过。
SELECT COUNT(*) AS 'How Many'
FROM Raw
WHERE [Plan Code] LIKE '%01%'
谢谢你。