日期:2014-05-17  浏览次数:20604 次

求高手指教:用sql做民族人口构成的统计报表(真的不知道怎么回复帖子)
库为bt_201110
人口表 resident里有民族表 nation和性别表 sex,

表resident结构
PK_RESIDENT AUTOID FULLNAME SEX NATION
469029404000617504160 469029404000617504114 陈小珍 2 19

表nation结构为:
NATION_CODE NATION_NAME
1 汉族
10 朝鲜族
11 满族

表sex结构为:
Code CodeName
1 男  
2 女  
0 未知的性别  
9 未说明的性别  

请问做这样的报表统计如何用sql来实现呢?求高手指点~
之前的贴不知道往哪里回复 只好重新发详细的帖

序号 民族 合计 男 女
  合计总数 4 2 2
1 汉族 2 1 1
2 朝鲜族 1 1 0
3 满族 1 0 1
4 少数民族人口 2 1 1
  民族人口比重 50% 50% 50%



------解决方案--------------------
這里寫一個例子.
SQL code

use Tempdb
go
--> --> 
 
if not object_id(N'Tempdb..#nation') is null
    drop table #nation
Go
Create table #nation([NATION_CODE] int,[NATION_NAME] nvarchar(3))
Insert #nation
select 1,N'汉族' union all
select 10,N'朝鲜族' union all
select 19,N'满族'
Go
use Tempdb
go
--> --> 
 
if not object_id(N'Tempdb..#Sex') is null
    drop table #Sex
Go
Create table #Sex([Code] int,[CodeName] nvarchar(6))
Insert #Sex
select 1,N'男' union all
select 2,N'女' union all
select 0,N'未知的性别' union all
select 9,N'未说明的性别'
Go

if not object_id(N'Tempdb..#resident') is null
    drop table #resident
Go
Create table #resident([PK_RESIDENT] NVARCHAR(50),[AUTOID] NVARCHAR(50),[FULLNAME] nvarchar(3),[SEX] int,[NATION] int)
Insert #resident
select '469029404000617504160','469029404000617504114',N'陈小珍',2,19
Go


SELECT 
ISNULL(a.[NATION_NAME],N'總計') AS 民族,
COUNT(1)AS 合计,
sum(CASE WHEN a.[CodeName]=N'男' THEN 1 ELSE 0 END) AS 男,
sum(CASE WHEN a.[CodeName]=N'男' THEN 1 ELSE 0 END) AS 女
FROM 
(SELECT * FROM #nation AS a,#Sex AS b WHERE b.[CodeName]  LIKE N'[男女]')a
LEFT JOIN #resident AS b ON b.[SEX]=a.[Code] AND b.[NATION]=a.[NATION_CODE]
GROUP BY a.[NATION_NAME] WITH rollup

------解决方案--------------------
SQL code
看看是否这样的结果

use Tempdb
go
--> --> 
 
if not object_id(N'Tempdb..#nation') is null
    drop table #nation
Go
Create table #nation([NATION_CODE] int,[NATION_NAME] nvarchar(20))
Insert #nation
select 1,N'汉族' union all
select 10,N'朝鲜族' union all
select 19,N'满族'
Go
use Tempdb
go
--> --> 
 
if not object_id(N'Tempdb..#Sex') is null
    drop table #Sex
Go
Create table #Sex([Code] int,[CodeName] nvarchar(6))
Insert #Sex
select 1,N'男' union all
select 2,N'女' union all
select 0,N'未知的性别' union all
select 9,N'未说明的性别'
Go

if not object_id(N'Tempdb..#resident') is null
    drop table #resident
Go
Create table #resident([PK_RESIDENT] NVARCHAR(50),[AUTOID] NVARCHAR(50),[FULLNAME] nvarchar(3),[SEX] int,[NATION] int)
Insert #resident
select '469029404000617504160','469029404000617504114',N'陈小珍',2,19
Go

--序号 民族 合计 男 女

select 
    序号=ROW_NUMBER()over(order by groups,case when 民族=N'民族' then 1 else 2 end),民族,合计,男,女
from 
(SELECT 
ISNULL(a.[NATION_NAME],N'合计总数') AS 民族,
rtrim(COUNT(b.[SEX]))AS 合计,
rtrim(count(CASE WHEN a.[CodeName]=N'男' THEN b.[SEX] END)) AS 男,
rtrim(count(CASE WHEN a.[CodeName]=N'女' THEN b.[SEX] END)) AS 女,
case when GROUPING(a.[NATION_NAME])=1 then 0 else 1 end as groups
FROM 
(SELECT * FROM #nation AS a,#Sex AS b WHERE b.[CodeName]  LIKE N'[男女]')a
LEFT JOIN #resident AS b ON b.[SEX]=a.[Code] AND b.[NATION]=a.[NATION_CODE]
GROUP BY a.[NATION_NAME] WITH rollup

union all
select '少数民族人口' as 民族,rtrim(COUNT(b.[SEX]))AS 合计,
rtrim(count(CASE WHEN a.[CodeName]=N'男' THEN b.[SEX] END)) AS 男,
rtrim(count(CASE WHEN a.[CodeName]=N'女' THEN b.[SEX] END)) AS 女,
groups=2
from
(SELECT * FROM #nation AS a,#Sex AS b WHERE b.[CodeName]  LIKE N'[男女]')a
inner JOIN #resident AS b ON b.[SEX]=a.[Code] AND b.[NATION]=a.[NATION_CODE] and a.[NATION_NAME] not in(N'汉族')
union all
select '民族人口比重' as 民族,
str(sum(case when a.[NATION_NAME]!=N'汉族' then 1 else 0 end)*100.0/sum(1),5,2)+'%' AS 合计,
str(sum(CASE WHEN a.[CodeName]=N'男'