如何计算1列中的多个内容

时间:2014-08-05 06:56:35

标签: sql

我有一个表empnameagegender

     empname   age    gender
     Nana      24       F
     Jillian   28       M
     Sally     29       F
     David     30       M
     Mike      35       M
     Daisy     37       F

问题是我如何计算女性的性别<30岁,女性> 30岁,男性<30岁,男性> 30岁,来自employee

     female<30  female>30  male<30  male>30
         2          1         1        2

4 个答案:

答案 0 :(得分:3)

最简单的方法是使用多个子查询:

SELECT
(SELECT COUNT(*) FROM employee WHERE gender = F AND age<30) AS 'Female Below 30',
(SELECT COUNT(*) FROM employee WHERE gender = F AND age>=30) AS 'Female Above 30',
(SELECT COUNT(*) FROM employee WHERE gender = M AND age<30) AS 'Male Below 30',
(SELECT COUNT(*) FROM employee WHERE gender = M AND age>=30) AS 'Male Above 30'

我认为在这种情况下使用PIVOT更好:

SELECT  [FB30],
        [FA30],
        [MB30],
        [MA30]
FROM    (
            SELECT  empname, 
                    gender + CASE WHEN age<30 THEN 'B30' ELSE 'A30' END AS SexAge 
            FROM    Employee
        ) AS SourceTable
        PIVOT
        (
            COUNT(empname)
            FOR SexAge IN ([FB30],[FA30],[MB30],[MA30])
        ) AS PivotTable;

查看 SQL SERVER Fiddle Demo

经过一些搜索(我不是进入Oracle ......)我最终为PIVOT做了这个:

WITH
   T
AS
(
    SELECT "empname" ,
           "gender" || CASE WHEN "age"<30 THEN 'B30' ELSE 'A30' END AS "SexAge"
    FROM  Employee
)
SELECT
   SUM(FB30) AS FB30,SUM(FA30) AS FA30,SUM(MB30) AS MB30,SUM(MA30) AS MA30
FROM
   T
PIVOT
(
   COUNT(*)
   FOR
      ("SexAge")
   IN
      ('FB30' AS FB30,'FA30' AS FA30,'MB30' AS MB30,'MA30' AS MA30)
);

Oracle PIVOT Demo

因为您要求DECODE示例:

WITH
   T
AS
(
    SELECT "empname" ,
           "gender" || DECODE(TRUNC("age"/30),0,'B30','A30') AS "SexAge"
    FROM  Employee
)
SELECT
   SUM(FB30) AS FB30,SUM(FA30) AS FA30,SUM(MB30) AS MB30,SUM(MA30) AS MA30
FROM
   T
PIVOT
(
   COUNT(*)
   FOR
      ("SexAge")
   IN
      ('FB30' AS FB30,'FA30' AS FA30,'MB30' AS MB30,'MA30' AS MA30)
);

Oracle DECODE PIVOT Demo

答案 1 :(得分:2)

您可以这样查询:

select
  count(case when gender='F' and age >= 30 then 1 end) "Female>=30",
  count(case when gender='F' and age < 30 then 1 end) "Female<30",
  count(case when gender='M' and age >= 30 then 1 end) "Male>=30",
  count(case when gender='M' and age < 30 then 1 end) "Male>30"
from ...

您只需阅读一次表格即可获得所需内容。当表很大时,这可能会有很大的不同。

答案 2 :(得分:0)

请尝试:

select
    sum(case when gender='F' and age<30 then 1 end) [female<30],
    sum(case when gender='F' and age>=30 then 1 end) [female>30],
    sum(case when gender='M' and age<30 then 1 end) [male<30],
    sum(case when gender='M' and age>=30 then 1 end) [male>30]
from
    tbl

答案 3 :(得分:0)

你可以这样做:

SELECT  SUM(CASE
               WHEN gender = "F" and age < 30
               THEN 1
               ELSE 0
        END ) AS female_lt_30
,
        SUM(CASE
               WHEN gender = "F" and age >= 30
               THEN 1
               ELSE 0
        END ) AS female_ge_30
,
        SUM(CASE
               WHEN gender = "M" and age < 30
               THEN 1
               ELSE 0
        END ) AS male_lt_30
,
        SUM(CASE
               WHEN gender = "M" and age >= 30
               THEN 1
               ELSE 0
        END ) AS female_ge_30

FROM the_table;