Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
guygs18
Starting Member
4 Posts |
Posted - 2012-01-10 : 08:10:47
|
| Hi allI have two queries, which I've stored as views, and would like to join them as follows:dbo.cases_openedType Opened Date CountGeneral 14/10/2011 60White Goods 14/10/2011 5Fridges 17/10/2011 1General 17/10/2011 79White Goods 17/10/2011 17General 18/10/2011 70etc. There are 3 types.dbo.cases_closedType Closed Date CountGeneral 14/10/2011 3General 17/10/2011 13General 18/10/2011 50General 19/10/2011 59White Goods 19/10/2011 9Fridges 20/10/2011 1General 20/10/2011 23White Goods 20/10/2011 18General 21/10/2011 30etc. As you can see, not all types will have cases opened and closed on the same day, so nulls would suffice for blanks. Ideally I would like to present the query as such:Type Date Opened Count Closed CountGeneral 14/10/2011 60 3White Goods 14/10/2011 5 0Fridges 14/10/2011 0 0General 17/10/2011 79 13White Goods 17/10/2011 17 0Fridges 17/10/2011 1 0General 18/10/2011 70 50White Goods 18/10/2011 13 0Fridges 18/10/2011 0 0General 19/10/2011 53 59White Goods 19/10/2011 9 9Fridges 19/10/2011 0 0General 20/10/2011 59 23White Goods 20/10/2011 7 18Fridges 20/10/2011 0 1etc. I could then filter out those records where opened/closed are both 0. Is this possible? Any help would be greatly appreciated.Thanks!Guy Southcott |
|
|
singularity
Posting Yak Master
153 Posts |
Posted - 2012-01-10 : 09:01:31
|
| [code]select isnull(a.type,b.type) as type, isnull(a.opened_date,b.closed_date) as [date], isnull(a.[count],0) as opened_count, isnull(b.[count],0) as closed_countfrom cases_opened afull join cases_closed b on a.type = b.type and a.opened_date = b.closed_date[/code] |
 |
|
|
guygs18
Starting Member
4 Posts |
Posted - 2012-01-10 : 09:21:05
|
| Thanks singularity! Much appreciated.Guy Southcott |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-10 : 09:26:01
|
quote: Originally posted by singularity
select isnull(a.type,b.type) as type, isnull(a.opened_date,b.closed_date) as [date], isnull(a.[count],0) as opened_count, isnull(b.[count],0) as closed_countfrom cases_opened afull join cases_closed b on a.type = b.type and a.opened_date = b.closed_date
i prefer COALESCE over ISNULL due to following reasonshttp://beyondrelational.com/blogs/madhivanan/archive/2007/10/04/isnull-or-coalesce.aspx------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|