December 29, 2010 at 4:33 am
Hi All,
I'm struggling with some code. I have two SQL statements both give me different answers.
SELECT COUNT(crn) AS NoDayCase,[DIS SPEC DESC], [DIS CONS DESC], [DIS_YEAR], [DIS_MONTH], [admtype], [SPELL LOS], [INTMAN]
FROM ColossusUser.inf.tbl_PS_IXP_ADMS_AND_DIS as DC
GROUP BY [DIS SPEC DESC], [DIS CONS DESC], [DIS_YEAR], [DIS_MONTH],[admtype],[SPELL LOS], [INTMAN]
HAVING (DIS_YEAR = N'2010') AND (DIS_MONTH = N'March') AND admtype IN('11','12','13') AND [SPELL LOS] <= 0 AND (INTMAN = 'DC')
SELECT COUNT(crn) AS NoElectiveAdmissions,[DIS SPEC DESC], [DIS CONS DESC], [DIS_YEAR], [DIS_MONTH], [admtype]
FROM ColossusUser.inf.tbl_PS_IXP_ADMS_AND_DIS as EA
GROUP BY [DIS SPEC DESC], [DIS CONS DESC], [DIS_YEAR], [DIS_MONTH],[admtype]
HAVING (DIS_YEAR = N'2010') AND (DIS_MONTH = N'March') AND admtype IN('11','12','13')
what am trying to do is to create a new view to show a column with NoElectiveAdmissions, NoDayCase, [DIS SPEC DESC],[DIS_YEAR], [DIS_MONTH] and [DIS CONS DESC]
December 29, 2010 at 4:57 am
Of course they give you different answers - you have a different column list. Please will you supply sample data (INSERT statements), table DDL (CREATE TABLE statements) and desired results so that we can help you.
Thanks
John
December 29, 2010 at 5:22 am
sorry i wasn't clear enough.
from the two views i want the two results to be combined into one view.
i.e
NoDaycase noElective Dis_Spec Dis_Cons
16 23 ENT ConsA
19 38 Derm ConsC
basically combine the two views and link by Consultant and spec
December 29, 2010 at 8:41 am
jbon007 (12/29/2010)
sorry i wasn't clear enough.from the two views i want the two results to be combined into one view.
i.e
NoDaycase noElective Dis_Spec Dis_Cons
16 23 ENT ConsA
19 38 Derm ConsC
basically combine the two views and link by Consultant and spec
This code is joining on those fields, assuming that there is at least one record in each of the queries. If one can have zero records, with the other having >0, then you'll need to use a left or full join.
WITH CTE1 AS
(
SELECT COUNT(crn) AS NoDayCase,[DIS SPEC DESC], [DIS CONS DESC], [DIS_YEAR], [DIS_MONTH], [admtype], [SPELL LOS], [INTMAN]
FROM ColossusUser.inf.tbl_PS_IXP_ADMS_AND_DIS as DC
GROUP BY [DIS SPEC DESC], [DIS CONS DESC], [DIS_YEAR], [DIS_MONTH],[admtype],[SPELL LOS], [INTMAN]
HAVING (DIS_YEAR = N'2010') AND (DIS_MONTH = N'March') AND admtype IN('11','12','13') AND [SPELL LOS] <= 0 AND (INTMAN = 'DC')
), CTE2 AS
(
SELECT COUNT(crn) AS NoElectiveAdmissions,[DIS SPEC DESC], [DIS CONS DESC], [DIS_YEAR], [DIS_MONTH], [admtype]
FROM ColossusUser.inf.tbl_PS_IXP_ADMS_AND_DIS as EA
GROUP BY [DIS SPEC DESC], [DIS CONS DESC], [DIS_YEAR], [DIS_MONTH],[admtype]
HAVING (DIS_YEAR = N'2010') AND (DIS_MONTH = N'March') AND admtype IN('11','12','13')
)
SELECT CTE1.NoDayCase, CTE2.NoElectiveAdmissions, CTE1.[DIS SPEC DESC], CTE2.[DIS CONS DESC]
FROM CTE1
JOIN CTE2
ON CTE1.[DIS CONS DESC] = CTE2.[DIS CONS DESC]
AND CTE1.[DIS SPEC DESC] = CTE2.[DIS SPEC DESC]
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply