I have some SQL code which I execute to feed to a spreadsheet for percentages. Out_code used to have a value of "0" or "1". Now it has multiple values for non-zero codes.
I want to make my output have only 2 lines per date: a line for "0" and a line for "1", but I have tried decode and case, but each only changes the value on the line, and does not combine the counts. What do I need to do to get a single count for all zero and non-zero values per date?
Produces this output:
Departure Date Type Trips
-------------- ------- ----------
12/08/2008 0 790
12/08/2008 1 1
12/08/2008 1 1
12/08/2008 1 14
12/08/2008 1 53
12/08/2008 1 2482
12/12/2008 0 750
12/12/2008 1 1
12/12/2008 1 1
12/12/2008 1 6
12/12/2008 1 37
12/12/2008 1 2539
This is the output I want:
Departure Date Type Trips
-------------- ------- ----------
12/08/2008 0 790
12/08/2008 1 2551
12/12/2008 0 750
12/12/2008 1 2584
I want to make my output have only 2 lines per date: a line for "0" and a line for "1", but I have tried decode and case, but each only changes the value on the line, and does not combine the counts. What do I need to do to get a single count for all zero and non-zero values per date?
Code:
col "Departure Date" format a14
col out_code format a7 JUSTIFY right heading "Type"
col "Trips" format 999,999
prompt Trips...
select (substr(to_char(depart_date), 5, 2) || '/' ||
substr(to_char(depart_date), 7, 2) || '/' ||
substr(to_char(depart_date), 1, 4)) as "Departure Date",
CASE WHEN (out_code = 0) THEN '0'
ELSE '1'
END as out_code,
count(*) as "Trips"
from departure_table
where depart_date >= to_char(sysdate-10, 'yyyymmdd')
group by depart_date, out_code
order by to_date(depart_date, 'yyyymmdd'), out_code;
Produces this output:
Departure Date Type Trips
-------------- ------- ----------
12/08/2008 0 790
12/08/2008 1 1
12/08/2008 1 1
12/08/2008 1 14
12/08/2008 1 53
12/08/2008 1 2482
12/12/2008 0 750
12/12/2008 1 1
12/12/2008 1 1
12/12/2008 1 6
12/12/2008 1 37
12/12/2008 1 2539
This is the output I want:
Departure Date Type Trips
-------------- ------- ----------
12/08/2008 0 790
12/08/2008 1 2551
12/12/2008 0 750
12/12/2008 1 2584