0

我有一个表名为 JOB_Details ,数据显示如下:

Age           Department:IT         
              Male     Female 
30yrs         5          1              
30-34yrs      3          2              
35-39yrs      4          4              
40-49yrs      2          3              
50-54yrs      1          0              

并且输出假设会像:

Age           Department:IT          Total
              Male     Female 
30yrs         5          1              6
30-34yrs      3          2              5
35-39yrs      4          4              8
40-49yrs      2          3              5
50-54yrs      1          0              1
Total         15         10             25

我该怎么做?因为当使用 count 和 group by 函数时,结果将不是我想要的。有人可以给我一些建议吗?为了您的信息,我使用 Oracle。

4

2 回答 2

2
SELECT AGE, MALE, FEMALE, SUM(MALE + FEMALE) AS TOTAL
FROM JOB_DETAILS
GROUP BY AGE

编辑:

如果您想要运行总数,则必须使用ROLLUP(SQL Server)

SELECT AGE, MALE, FEMALE, SUM(MALE + FEMALE) AS TOTAL
FROM JOB_DETAILS
GROUP BY AGE
WITH ROLLUP 
于 2012-10-11T08:37:52.310 回答
0

尝试union all像这样使用:

SELECT age, Male, Female, nvl(Male,0) + nvl(Female,0) as total
FROM JOB_Details 
union all
SELECT 'TOTAL', sum(Male), sum(Female), sum(nvl(Male,0) + nvl(Female,0))
FROM JOB_Details 

有观点:

create view vJOB_Details 
SELECT (CASE WHEN age='30' THEN Count(EMPLID) 
             WHEN age='34' THEN Count(EMPLID) 
             WHEN age='39' THEN Count(EMPLID) 
             WHEN age='49' THEN Count(EMPLID) 
             WHEN age='54' THEN Count(EMPLID) 
             ELSE NULL 
        END) TOTAL_COLUMN ,
        (CASE WHEN GENDER1='M' THEN Count(EMPLID) 
              WHEN GENDER1='F' THEN Count(EMPLID) 
              ELSE NULL 
         END) TOTAL_GENDER 
FROM job_details 
GROUP BY age,GENDER 

SELECT age, Male, Female, nvl(Male,0) + nvl(Female,0) as total
FROM vJOB_Details 
union all
SELECT 'TOTAL', sum(Male), sum(Female), sum(nvl(Male,0) + nvl(Female,0))
FROM vJOB_Details; 
于 2012-10-11T08:41:15.467 回答