How do I simplify the below script. Hopefully to use one select statement.and not use UNION at all.
Select
Case
When xfe.ID in (5,8) Then 'BCC Matters'
When xfe.ID in (10) Then 'LCC Matters'
When xfe.ID in (1) Then 'BA Matters'
Else 'Unallocated'
End As 'Matter Group'
, Sum(Case When xmfs.M_FileStatus <> 5 then 1 Else 0 End) As 'Live Instructions'
, Sum(Case When xmfs.M_FileStatus = 0 then 1 Else 0 End) As 'New Instructions'
, Sum(Case When xmfs.M_FileStatus = 1 then 1 Else 0 End) As 'Attempting PTP'
, Sum(Case When xmfs.M_FileStatus = 2 then 1 Else 0 End) As 'Payment Arrangement'
, Sum(Case When xmfs.M_FileStatus = 3 then 1 Else 0 End) As 'No Arrangement'
From ExcaliburV4_WH.dbo.Matter xm with (Nolock)
Inner Join ExcaliburV4_WH.dbo.MatterFileStatus xmfs on xmfs.IDX = xm.M_IDX And xmfs.M_FileStatus <> 5
Inner Join ExcaliburV4_WH.dbo.BookListView xb on xb.ID = xm.M_ClientID
Inner Join ExcaliburV4_WH.dbo.Client xcl on xcl.ID = xb.ID
Left Join ExcaliburV4_WH.dbo.LawFirmEntities xfe on xfe.ID = xcl.C_LawFirmEntityID
Where xfe.ID in (5,8) -- BCC Matters
--Where xfe.ID in (10) -- LCC Matters
--Where xfe.ID in (1) -- BA Matters
--Where xfe.ID is null -- No Allocation
Group By xfe.ID
UNION ALL
Select
Case
When xfe.ID in (5,8) Then 'BCC Matters'
When xfe.ID in (10) Then 'LCC Matters'
When xfe.ID in (1) Then 'BA Matters'
Else 'Unallocated'
End As 'Matter Group'
, Sum(Case When xmfs.M_FileStatus <> 5 then 1 Else 0 End) As 'Live Instructions'
, Sum(Case When xmfs.M_FileStatus = 0 then 1 Else 0 End) As 'New Instructions'
, Sum(Case When xmfs.M_FileStatus = 1 then 1 Else 0 End) As 'Attempting PTP'
, Sum(Case When xmfs.M_FileStatus = 2 then 1 Else 0 End) As 'Payment Arrangement'
, Sum(Case When xmfs.M_FileStatus = 3 then 1 Else 0 End) As 'No Arrangement'
From ExcaliburV4_WH.dbo.Matter xm with (Nolock)
Inner Join ExcaliburV4_WH.dbo.MatterFileStatus xmfs on xmfs.IDX = xm.M_IDX And xmfs.M_FileStatus <> 5
Inner Join ExcaliburV4_WH.dbo.BookListView xb on xb.ID = xm.M_ClientID
Inner Join ExcaliburV4_WH.dbo.Client xcl on xcl.ID = xb.ID
Left Join ExcaliburV4_WH.dbo.LawFirmEntities xfe on xfe.ID = xcl.C_LawFirmEntityID
--Where xfe.ID in (5,8) -- BCC Matters
Where xfe.ID in (10) -- LCC Matters
--Where xfe.ID in (1) -- BA Matters
--Where xfe.ID is null -- No Allocation
Group By xfe.ID
UNION ALL
Select
Case
When xfe.ID in (5,8) Then 'BCC Matters'
When xfe.ID in (10) Then 'LCC Matters'
When xfe.ID in (1) Then 'BA Matters'
Else 'Unallocated'
End As 'Matter Group'
, Sum(Case When xmfs.M_FileStatus <> 5 then 1 Else 0 End) As 'Live Instructions'
, Sum(Case When xmfs.M_FileStatus = 0 then 1 Else 0 End) As 'New Instructions'
, Sum(Case When xmfs.M_FileStatus = 1 then 1 Else 0 End) As 'Attempting PTP'
, Sum(Case When xmfs.M_FileStatus = 2 then 1 Else 0 End) As 'Payment Arrangement'
, Sum(Case When xmfs.M_FileStatus = 3 then 1 Else 0 End) As 'No Arrangement'
From ExcaliburV4_WH.dbo.Matter xm with (Nolock)
Inner Join ExcaliburV4_WH.dbo.MatterFileStatus xmfs on xmfs.IDX = xm.M_IDX And xmfs.M_FileStatus <> 5
Inner Join ExcaliburV4_WH.dbo.BookListView xb on xb.ID = xm.M_ClientID
Inner Join ExcaliburV4_WH.dbo.Client xcl on xcl.ID = xb.ID
Left Join ExcaliburV4_WH.dbo.LawFirmEntities xfe on xfe.ID = xcl.C_LawFirmEntityID
--Where xfe.ID in (5,8) -- BCC Matters
--Where xfe.ID in (10) -- LCC Matters
Where xfe.ID in (1) -- BA Matters
--Where xfe.ID is null -- No Allocation
Group By xfe.ID
UNION ALL
Select
Case
When xfe.ID in (5,8) Then 'BCC Matters'
When xfe.ID in (10) Then 'LCC Matters'
When xfe.ID in (1) Then 'BA Matters'
Else 'Unallocated'
End As 'Matter Group'
, Sum(Case When xmfs.M_FileStatus <> 5 then 1 Else 0 End) As 'Live Instructions'
, Sum(Case When xmfs.M_FileStatus = 0 then 1 Else 0 End) As 'New Instructions'
, Sum(Case When xmfs.M_FileStatus = 1 then 1 Else 0 End) As 'Attempting PTP'
, Sum(Case When xmfs.M_FileStatus = 2 then 1 Else 0 End) As 'Payment Arrangement'
, Sum(Case When xmfs.M_FileStatus = 3 then 1 Else 0 End) As 'No Arrangement'
From ExcaliburV4_WH.dbo.Matter xm with (Nolock)
Inner Join ExcaliburV4_WH.dbo.MatterFileStatus xmfs on xmfs.IDX = xm.M_IDX And xmfs.M_FileStatus <> 5
Inner Join ExcaliburV4_WH.dbo.BookListView xb on xb.ID = xm.M_ClientID
Inner Join ExcaliburV4_WH.dbo.Client xcl on xcl.ID = xb.ID
Left Join ExcaliburV4_WH.dbo.LawFirmEntities xfe on xfe.ID = xcl.C_LawFirmEntityID
--Where xfe.ID in (5,8) -- BCC Matters
--Where xfe.ID in (10) -- LCC Matters
--Where xfe.ID in (1) -- BA Matters
Where xfe.ID is null -- No Allocation
Group By xfe.ID