Manipulate results to display rows as columns

前端 未结 3 813
终归单人心
终归单人心 2020-12-19 18:07

I have two tables. Table A contains a list of departments, there are 20 in total.

See image

\"enter

相关标签:
3条回答
  • 2020-12-19 18:48
    SELECT
      A.DEPTNAME,
      (SELECT COUNT(*) FROM TABLEA A2 INNER JOIN TABLEB B ON B.TableAID = A2.TableAID WHERE B.EMPLOYEE_CLASS = 'Full time' AND A2.TableAID = A.TableAID) FullTimeNo,
      (SELECT COUNT(*) FROM TABLEA A2 INNER JOIN TABLEB B ON B.TableAID = A2.TableAID WHERE B.EMPLOYEE_CLASS = 'Part time' AND A2.TableAID = A.TableAID) PartTimeNo
    FROM
      TABLEA
    
    0 讨论(0)
  • 2020-12-19 18:59

    You want a PIVOT

    select *
    from (tableb) s
    pivot (Max(staffno) for employee_class in ([Full Time],[Part Time])) p
    
    0 讨论(0)
  • 2020-12-19 19:08

    Try this:

    select A.DEPTNAME, 
           sum(case when B.EMPLOYEE_CLASS='Full time' then B.STAFF_NO else 0) as FullTimeNo,
           sum(case when B.EMPLOYEE_CLASS='Part time' then B.STAFF_NO else 0) as PartTimeNo,
    from TabA A
    join TabB B on A.DEPTNAME=B.DEPTNAME
    group by A.DEPTNAME
    
    0 讨论(0)
提交回复
热议问题