How can I write SQL in Oracle in my case?

后端 未结 4 1289
情歌与酒
情歌与酒 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:19

    You will have to first take the count and pct in a subquery then use max+decode function to summarize both of them in the required fashion Check if the below query helps:

    SELECT Max(Decode(i.place,'Kitchen',cnt)) AS "Kitchecn"
         , Max(Decode(i.place,'Kitchen',pct)) AS "Pct"
         , Max(Decode(i.place,'Washroom',cnt)) AS "Washroom"
         , Max(Decode(i.place,'Washroom',pct)) AS "Pct"
         , Max(Decode(i.place,'Rooftop',cnt)) AS "Rooftop"
         , Max(Decode(i.place,'Rooftop',pct)) AS "Pct"
         , Max(Decode(i.place,'Garden',cnt)) AS "Garden"
         , Max(Decode(i.place,'Garden',pct)) AS "Pct"
      FROM (SELECT i.place
                 , Count(pim.injury_id) AS cnt
                 , (Count(pim.injury_id)*100/(SELECT Count(*) FROM person_injuryPlace_map)) AS pct 
              FROM person_injuryPlace_map pim
             INNER JOIN injury_place i ON i.id = pim.injury_id
             GROUP BY i.place)
    

提交回复
热议问题