|
Here is a NOT-thought out response, but I wanted to throw something together that seemed to be a real simple solution. I imagine that it would depend on the size of the data set if this was a real bad Idea and the "letterlist" needs to come from somewhere, that can be done a different way if selecting a distinct list from the main source isn't valid. But in essence this meets the question asked.
create table schemaname.my_table (date1 date, letter varchar2(1)); declare begin delete from SCHEMANAME.MY_TABLE;
for i in 1..10 loop insert into SCHEMANAME.MY_TABLE values (to_date('2010-01-10','yyyy-mm-dd hh24:mi:ss'), 'B'); insert into SCHEMANAME.MY_TABLE Values( to_date('2010-03-10','yyyy-mm-dd hh24:mi:ss'), 'A') ; end loop; for i in 1..20 loop insert into SCHEMANAME.MY_TABLE values (to_date('2010-01-10','yyyy-mm-dd hh24:mi:ss'), 'C'); insert into SCHEMANAME.MY_TABLE Values( to_date('2010-03-10','yyyy-mm-dd hh24:mi:ss'), 'B') ; end loop; for i in 1..30 loop insert into SCHEMANAME.MY_TABLE values (to_date('2010-03-10','yyyy-mm-dd hh24:mi:ss'), 'C'); end loop; for i in 1..25 loop insert into SCHEMANAME.MY_TABLE values (to_date('2010-02-10','yyyy-mm-dd hh24:mi:ss'), 'C'); end loop; for i in 1..40 loop insert into SCHEMANAME.MY_TABLE values (to_date('2010-02-10','yyyy-mm-dd hh24:mi:ss'), 'A'); end loop; commit; end;
select date1, letter, count(*) from SCHEMANAME.MY_TABLE group by date1, letter order by date1, letter;
select distinct letter as letterlist from SCHEMANAME.MY_TABLE;
select date1, letterlist, sum(decode(letterlist,letter,1,0)) from (select distinct letter as letterlist from SCHEMANAME.MY_TABLE), SCHEMANAME.MY_TABLE group by date1, letterlist order by 1,2,3;
You can see the final query takes advantage of a cartesian join. This may be a bad idea in many cases, but it might be justifiable as the best solution since this does exactly what you asked. Just 2 cents, I'm sure this can be improved upon or maybe proven just plain dumb.
|