I have 3 columns doc_date, chem_date, st_date
all 3 columns belong to different tables:
doc
doc_date count(x)
01-02-2012 2
02-02-2012 3
chem
chem_date count(x)
04-02-2012 1
06-02-2012 0
stock
st_date count(x)
01-02-2012 1
03-02-2012 5
I want to write select clause like this
case doc_date
when '01' then count(x),
case chem_date
when '01' then count(x),
case st_date
when '01' then count(x)
end as '01',
case doc_date
when '02' then count(x),
case chem_date
when '02' then count(x),
case st_date
when '02' then count(x)
end as '02',
....up to 31 days
If some case statements have an entry on same date e.g if doc_date and st_date both exist on ’01-02-2012′ then their respective count should be added to the final count(x) means count(x) + count(x)
So, the answer should be:
01 02 03 04 05 06 07 08 09 10 11 12.. up to 31 days
3 3 5 1 0 count(x) value for particular date
Explanation of the output: Here, the starting value for date 01 is 2 for doc table and for stock table the value is 1. So, the final value will become addition of them which is 3
The same rule will be applicable for others.
Any suggestions how can I achieve this output in SQL? Or any other way?
Here’s a pseudo code showing how it can be done:
If the
doc,chemandstare combined usingjoinsand have different schema, you may need to do pivot, either using multiple join to itself or usecase... when...:I am working with an ambiguous question here, so clarify as needed,