透视DB2 SQL SubQuery

时间:2017-11-22 19:15:27

标签: sql db2 subquery pivot

此查询为我提供了年龄和国家/地区的人数结果。下面是我需要的图片。就像一个Pivot表。

enter image description here

    SELECT count(age) AS numberofpeople, age, country
    FROM (

    SELECT person_id,
    TIMESTAMPDIFF(256,CHAR(TIMESTAMP(a.admission_date) - TIMESTAMP(p.dateofbirth))) AS Age, 
    dateofbirth,
    a.admission_date

    FROM person p
    LEFT JOIN admission a ON p.person_id = a.person_id

  WHERE category = '1' 
    AND startdate BETWEEN '8/1/2016' AND '10/31/2017') AS a 
    WHERE age <= '10'
    GROUP BY country, age

谢谢

0 个答案:

没有答案