PDA

View Full Version : Get all results from where clause even value doestn exits.



aravindhan_3
07-29-2011, 02:24 AM
Hi I need your help for onle of my SQL query

I have SQL query like this

Select
Week_Number As Week,
Store_Number,
Department,
Sum(Sales) As Sales
From
Sales_Table
Where Store_Number In (1,2,3)
And Department In ('A','B','C')
and Week_Number = 1

Group By 1,2,3


This would give me the sales number only for the stores & department which are there in the where clause. If no sales available for the store 2 the that will not be shown in the output.

But I need that store also on the result but with zero value, that is look data for all the stores in the where clause, if sales exists get the sales else get zero

I have attached the excel sheet for which clearly shows my requirement.

Thanks for your help

Norie
07-30-2011, 01:06 PM
Do you have a table for departments?

If you did this would be a straightforward left join query.

That would return all the records from the departments tables and only the matching records from the other tables.