也可以。 select c1,c2,c3 from (select 总人数 as c1,男性人数 as c2,女性人数 as c3,0 as c4 union select count(1), sum(case 性别 when 男 then 1 else 0 end), sum(case 性别 when 女 then 1 else 0 end), 1 from tablename union select '愽土人员总数','研究生人员总数','本科人员总数',2 union select sum(case 文化程度 when 博土 then 1 else 0 end), sum(case 文化程度 when 研究生 then 1 else 0 end), sum(case 文化程度 when 本科 then 1 else 0 end),3 from tablename) Aorder by c4
看不太懂这语句case是什么? 而且这语句执行后出现提示为“未指定的错误”不知错在哪里
CREATE TABLE #T(中文姓名 NVARCHAR(20), 性别 NVARCHAR(20),文化程度 NVARCHAR(20)) INSERT INTO #T SELECT '张三f','女','大专' UNION ALL SELECT '仍的','男','博土' UNION ALL SELECT '僩尖','女','研究生' UNION ALL SELECT '李膛','男','本科' ---- select c1,c2,c3 from (select '总人数' as c1,'男性人数' as c2,'女性人数' as c3,0 as c4 union select CAST(count(1) AS NVARCHAR(5)), CAST(sum(case 性别 when '男' then 1 else 0 end) AS VARCHAR(10)), CAST(sum(case 性别 when '女' then 1 else 0 end) AS VARCHAR(10)) ,1 from #T union select '愽土人员总数','研究生人员总数','本科人员总数',2 union select CAST(sum(case 文化程度 when '博土' then 1 else 0 end) AS VARCHAR(10)), CAST(sum(case 文化程度 when '研究生' then 1 else 0 end) AS VARCHAR(10)), CAST(sum(case 文化程度 when '本科' then 1 else 0 end)AS VARCHAR(10)) , 3 from #T) Aorder by c4
select c1,c2,c3 from
(select 总人数 as c1,男性人数 as c2,女性人数 as c3,0 as c4
union
select count(1),
sum(case 性别 when 男 then 1 else 0 end),
sum(case 性别 when 女 then 1 else 0 end),
1
from tablename
union
select '愽土人员总数','研究生人员总数','本科人员总数',2
union
select sum(case 文化程度 when 博土 then 1 else 0 end),
sum(case 文化程度 when 研究生 then 1 else 0 end),
sum(case 文化程度 when 本科 then 1 else 0 end),3
from tablename) Aorder by c4
而且这语句执行后出现提示为“未指定的错误”不知错在哪里
CREATE TABLE #T(中文姓名 NVARCHAR(20), 性别 NVARCHAR(20),文化程度 NVARCHAR(20))
INSERT INTO #T SELECT '张三f','女','大专'
UNION ALL
SELECT '仍的','男','博土'
UNION ALL
SELECT '僩尖','女','研究生'
UNION ALL
SELECT '李膛','男','本科'
----
select c1,c2,c3 from
(select '总人数' as c1,'男性人数' as c2,'女性人数' as c3,0 as c4
union
select CAST(count(1) AS NVARCHAR(5)),
CAST(sum(case 性别 when '男' then 1 else 0 end) AS VARCHAR(10)),
CAST(sum(case 性别 when '女' then 1 else 0 end) AS VARCHAR(10))
,1
from #T
union
select '愽土人员总数','研究生人员总数','本科人员总数',2
union
select CAST(sum(case 文化程度 when '博土' then 1 else 0 end) AS VARCHAR(10)),
CAST(sum(case 文化程度 when '研究生' then 1 else 0 end) AS VARCHAR(10)),
CAST(sum(case 文化程度 when '本科' then 1 else 0 end)AS VARCHAR(10))
, 3
from #T) Aorder by c4