Skip to content
Advertisement

2 column with 2 different condition in one table

is it possible to create a query that will output 2 column with 2 different WHERE clause

this my ideal output:

| YEAR | WW | COUNT | *COUNT2 |

where count 1 is the result of the 1st WHERE clause. Which I already created. What I want is to have another column where in the value will be with different WHERE clause.

here’s my query:

SELECT extract(YEAR FROM EVT_TARGET) as Years, to_char(EVT_TARGET, 'ww') AS WorkWeek, COUNT(*) 
FROM r5events 
WHERE EVT_JOBTYPE = 'CORR' AND EVT_RSTATUS <> 'C' AND EVT_TARGET IS NOT NULL 
GROUP BY extract(YEAR FROM EVT_TARGET), to_char(EVT_TARGET, 'ww');

with this query I was able to get the 1st 3 column, my problem now is how to supply value on the 4th column with the value of EVT_RSTATUS = 'C'.

Any insight?

TIA

Advertisement

Answer

I think you want conditional aggregation:

SELECT 
    extract(YEAR FROM EVT_TARGET) as Years, 
    to_char(EVT_TARGET,'ww') AS WorkWeek, 
    SUM(CASE WHEN EVT_RSTATUS <> 'C' THEN 1 ELSE 0 END) AS cnt1,
    SUM(CASE WHEN EVT_RSTATUS =  'C' THEN 1 ELSE 0 END) AS cnt2
FROM r5events 
WHERE EVT_JOBTYPE ='CORR' AND EVT_TARGET IS NOT NULL 
GROUP BY extract(YEAR FROM EVT_TARGET), to_char(EVT_TARGET,'ww');
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement