Question : Multi-table SQl Query - not quite a join, how to do it?

4 tables.

1. tblDistrict (DistrictID, DistrictName)
2. tblParam (ParamID, DomainName, Description)
3. tblSales1 (SalesID, DistrictIDFK,ParamIDFK, Date)
4. tblSales2 (SalesID,DistrictIDFK,ParamIDFK, Date)

In tblSales1, DomainName="74", in tblSales2, DomainName="75"

For EACH DISTRICT, I Need EVERY Description possible, and a COUNT of # of entrys in tblSales1 and tblSales2 that corrispond to their approprite DomainNames.

So...

If DomainName = "75"  has 10 items, and only 5 of those items exist in tblSales1, i need to see all 10 items descriptions where domainName=75

I have

~~~~~~~~~~~~~~
Select District.Name, Description, count(Description) from
tblParam left outer join tblSales1 on tblSales1.ParamIDFK = tblParam.ParamID
inner join tblDistrict on tblSales1.districtIDFK = tblDistrict.DistrictID
where DomainName=74

UNION

Select District.Name, Description, count(Description) from
tblParam left outer join tblSales2 on tblSales2.ParamIDFK = tblParam.ParamID
inner join tblDistrict on tblSales2.districtIDFK = tblDistrict.DistrictID
where DomainName=75
~~~~~~~~~~~~~~

Obviously, this isn't working quite right, it ONLY shows the items in tblSales1 or tblSales2 & there is 1 more items than there should be.

Any help?

Answer : Multi-table SQl Query - not quite a join, how to do it?

you have criteria:
where DomainName=75
so your resultset is limited to records in the parameter table (joined to tblSales). so if you have districts without entries in tblSales they will not be returned.
you need a field to join districts to tblparam otherwise you need to then union join back to tbldistricts to get the districts with no sales.
Random Solutions  
 
programming4us programming4us