Suppose I have an sql statement
select a.Date, count(a.Id) as Apples from dbo.apples as a group by a.Date
which yields a result
Date Apples
10/1/2011 4
10/2/2011 6
10/4/2011 8
And I have a second query
select o.Date, count(o.Id) as Oranges from dbo.Oranges as o group by o.Date
which yields the result
Date Oranges
10/2/2011 3
10/3/2011 5
10/4/2011 7
I would like to combine the two results into a single result like
Date Apples Oranges
10/1/2011 4 0
10/2/2011 6 3
10/3/2011 0 5
10/4/2011 8 7
I tried
select select a.Date, count(a.Id) as Apples from dbo.apples as a group by a.Date
union
select o.Date, count(o.Id) as Oranges from dbo.Oranges as o group by o.Date
which didn’t give me the expected results, what I got was more like
Date Apples
10/1/2011 4
10/2/2011 6
10/2/2011 3
10/3/2011 5
10/4/2011 8
10/4/2011 7
with no mention of Oranges.
What should the final sql statement look like (I prefer performant and concise over not).
1 Answer