Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Pivots date ranges with union data

$
0
0
Hi everyone,This is my first post and I will try to be as descriptive as possible What I am trying to accomplish is to make a few extra columns with specified date rangesI have FY14 date range in the parameters at the top .. I would like to add a FY 15 column so the year would move up by 1.and also I need to add 2 more columns Prior year current month and This year, current monthSorry I am kinda new and any tips would be appreciated<code>DECLARE @Fy14_start datetimeDECLARE @Fy14_end datetimeSET @Fy14_start = '2013-07-01'SET @Fy14_end = '2014-06-30'SELECT x.ACCOUNT_NAME, X.STATUS_CODE, COUNT(X.PATIENT_CODE) AS FY14 FROM (SELECT PD.NAME_LAST + ' ' + PD.NAME_FIRST AS ACCOUNT_NAME, PD.STATUS_CODE, PD.PATIENT_CODE FROM PT_Prospect_Date PD WHERE PD.NAME_LAST IS NOT NULL AND PD.NAME_LAST IS NOT NULL AND PD.STATUS_CODE LIKE 'P02' UNION SELECT RB.ORGANIZATION_NAME, PD.STATUS_CODE, PD.PATIENT_CODE FROM PT_Prospect_Date PD INNER JOIN RES_BASIC RB ON RB.RESOURCE_ID = PD.ASSOCIATED_FACILITY_ID WHERE RB.ORGANIZATION_NAME IS NOT NULL AND PD.STATUS_CODE LIKE 'P%' UNION SELECT CASE WHEN PD.NAME_FULL IS NOT NULL THEN 'Consumer' END , PD.STATUS_CODE, PD.PATIENT_CODE FROM PT_Prospect_Date PD WHERE PD.STATUS_CODE LIKE 'P01' ) X INNER JOIN PT_Prospect_Date PD ON X.PATIENT_CODE = PD.PATIENT_CODE WHERE PD.PROSPECT_ADMIT_DATE >= @Fy14_start AND PD.PROSPECT_ADMIT_DATE <= @Fy14_end AND X.STATUS_CODE LIKE 'P%' GROUP BY X.STATUS_CODE, X.ACCOUNT_NAME ORDER BY X.STATUS_CODE </code>sample outputACCOUNT_NAME | STATUS_CODE | FY14 | (FY15) | (prior year, current month) | (this year, current month) |xxxxxx P01 12xxxxxx P03 3Thanks Again!look forward to being an active member--M

Viewing all articles
Browse latest Browse all 3145

Trending Articles