Different value counts on same column

前端 未结 3 1908
悲&欢浪女
悲&欢浪女 2020-12-19 10:46

I am new to Oracle. I have an Oracle table with three columns: serialno, item_category and item_status. In the third column the rows h

3条回答
  •  别那么骄傲
    2020-12-19 10:58

    You can either use CASE or DECODE statement inside the COUNT function.

      SELECT item_category,
             COUNT (*) total,
             COUNT (DECODE (item_status, 'serviceable', 1)) AS serviceable,
             COUNT (DECODE (item_status, 'under_repair', 1)) AS under_repair,
             COUNT (DECODE (item_status, 'condemned', 1)) AS condemned
        FROM mytable
    GROUP BY item_category;
    

    Output:

    ITEM_CATEGORY   TOTAL   SERVICEABLE UNDER_REPAIR    CONDEMNED
    ----------------------------------------------------------------
    chair           5       1           2               2
    table           5       3           1               1
    

提交回复
热议问题