Skip to content
Advertisement

I have query to get some data but I need different result

select to_char(a.insertdatetime) as insertdatetime , b.category, count(1) as count
  from daily a
     , server b 
 where status != 'OK'
 group by b.category
         , to_char(a.insertdatetime) 
 order by to_char(a.insertdatetime)
          , b.category; 

this is the result

But I want the category to become a column and the count become data for each category.

I use this in Oracle

Advertisement

Answer

You can use the PIVOT as following:

SELECT * FROM
    (
    select to_char(a.insertdatetime) as insertdatetime , b.category
    from daily a, server b where status != 'OK'
    )
PIVOT
    (
    COUNT(1) FOR category IN (1 AS "1", 2 AS "2", 3 AS "3", 4 AS "4", 5 AS "5")
    )
ORDER BY insertdatetime;

— Update —

Your original query should have ' around the values in the IN clause of the PIVOT clause as follows:

SELECT
    *
FROM
    (
        SELECT
            TO_CHAR(A.INSERTDATETIME) AS INSERTDATETIME,
            B.CATEGORY
        FROM
            DAILY A,
            SERVER B
        WHERE
            STATUS != 'OK'
    ) PIVOT (
        COUNT ( 1 )
        FOR CATEGORY
        IN ( 'ELASTIC' AS "ELASTIC", 'IS' AS "IS", 'IS_BPM' AS "IS_BPM", 'TC' AS "TC", 'UM' AS "UM" )
    )
ORDER BY
    INSERTDATETIME;

Cheers!!

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement