How can I write SQL in Oracle in my case?

后端 未结 4 1284
情歌与酒
情歌与酒 2020-12-11 06:58

So, Here are the tables-

create table person (
id number,
name varchar2(50)
);

create table injury_place (
id number,
place varchar2(50)
);

create table pe         


        
4条回答
  •  执笔经年
    2020-12-11 07:04

    If you use Oracle 11g or above you can use pivot function for your required output.

    SELECT *
    FROM (
        SELECT id
            ,place
            ,round((
                    cnt / sum(cnt) OVER (
                        ORDER BY NULL
                        )
                    ) * 100, 2) AS pct
        FROM (
            SELECT a.id
                ,a.place
                ,count(a.id) AS cnt
            FROM injury_place a
            JOIN person_injuryPlace_map b ON a.id = b.injury_id
            GROUP BY a.id
                ,a.place
            )
        )
    pivot(max(id) , max(pct) pct FOR place IN (
                'kitchen' AS kitchen
                ,'Washroom' Washroom
                ,'Rooftop' Rooftop
                ,'Garden' Garden
                ))
    

提交回复
热议问题