按出生日期和性别分类统计每个年代的员工总人数,男性人数和女性人数
续上一篇,在上一篇<按出生日期分类统计每个年代的员工人数 >的基础上,统计出每个年代的总人数的同时,还要按性别分别统计出男性人数和女性人数的数量,表的结构如下:
1/****** 对象: Table [dbo].[HREmployee] 脚本日期: 08/11/2006 23:51:31 ******/
2SET ANSI_NULLS ON
3GO
4SET QUOTED_IDENTIFIER ON
5GO
6SET ANSI_PADDING ON
7GO
8CREATE TABLE [dbo].[HREmployee](
9 [strWorkNo] [varchar](30) COLLATE Chinese_PRC_CI_AS NOT NULL,
10 [strChName] [varchar](20) COLLATE Chinese_PRC_CI_AS NULL,
11 [strSex] [varchar](5) COLLATE Chinese_PRC_CI_AS NULL,
12 [DeptID] [int] NOT NULL,
13 [dtBirthday] [datetime] NULL,
14 [strNation] [varchar](20) COLLATE Chinese_PRC_CI_AS NULL,
15 [strNativePlace] [varchar](30) COLLATE Chinese_PRC_CI_AS NULL,
16 [strSpeciality] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
17 [strTechnologyName] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
18 [strForeignLanguage] [varchar](20) COLLATE Chinese_PRC_CI_AS NULL,
19 [strForeignDegree] [varchar](15) COLLATE Chinese_PRC_CI_AS NULL,
20 [strMarriageStatus] [varchar](10) COLLATE Chinese_PRC_CI_AS NULL,
21 [strPositionInformation] [varchar](30) COLLATE Chinese_PRC_CI_AS NULL,
22 [dtJoinDate] [datetime] NULL,
23 [Memo] [varchar](100) COLLATE Chinese_PRC_CI_AS NULL
24) ON [PRIMARY]
25
26GO
27SET ANSI_PADDING OFF
想得到像下面类似的结果:2SET ANSI_NULLS ON
3GO
4SET QUOTED_IDENTIFIER ON
5GO
6SET ANSI_PADDING ON
7GO
8CREATE TABLE [dbo].[HREmployee](
9 [strWorkNo] [varchar](30) COLLATE Chinese_PRC_CI_AS NOT NULL,
10 [strChName] [varchar](20) COLLATE Chinese_PRC_CI_AS NULL,
11 [strSex] [varchar](5) COLLATE Chinese_PRC_CI_AS NULL,
12 [DeptID] [int] NOT NULL,
13 [dtBirthday] [datetime] NULL,
14 [strNation] [varchar](20) COLLATE Chinese_PRC_CI_AS NULL,
15 [strNativePlace] [varchar](30) COLLATE Chinese_PRC_CI_AS NULL,
16 [strSpeciality] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
17 [strTechnologyName] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
18 [strForeignLanguage] [varchar](20) COLLATE Chinese_PRC_CI_AS NULL,
19 [strForeignDegree] [varchar](15) COLLATE Chinese_PRC_CI_AS NULL,
20 [strMarriageStatus] [varchar](10) COLLATE Chinese_PRC_CI_AS NULL,
21 [strPositionInformation] [varchar](30) COLLATE Chinese_PRC_CI_AS NULL,
22 [dtJoinDate] [datetime] NULL,
23 [Memo] [varchar](100) COLLATE Chinese_PRC_CI_AS NULL
24) ON [PRIMARY]
25
26GO
27SET ANSI_PADDING OFF
总人数 男性人数 女性人数 年代
4 3 1 1940
20 11 9 1950
50 28 22 1960
… … … …
下面是实现的语句,这是一位同事写的,在此非常感谢:
1SELECT 年代=Floor(Year(dtBirthday)/10.0)*10,男性人数=SUM(CASE strSex When '男' Then 1 Else 0 End),
2女性人数=SUM(CASE strSex When '男' Then 0 Else 1 End)
3,总人数=COUNT(1) From HREmployee
4GROUP By Floor(Year(dtBirthday)/10.0)*10
2女性人数=SUM(CASE strSex When '男' Then 0 Else 1 End)
3,总人数=COUNT(1) From HREmployee
4GROUP By Floor(Year(dtBirthday)/10.0)*10