2015-04-15 228 views
0

我失去了對這個我有4列:SQL百分比

MarketCountDisnerollmentEnrollment

我需要得到disenrollmentenrollment之間的百分比但它們都是基於列MBRstatus,我做了兩個單獨的案件陳述,以便讓它分開。

我不知道我怎樣才能從這個查詢calulate比例正在建立這樣的,但是我也不知道我怎麼能拿列分離的另一種方式的任何幫助將不勝感激

我已經包括查詢

use medicare_enrollment 
---Variable set to find the first of the current reporting year 
Declare @1stofYear as Date 
set @1stofYear = DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0) 


---Variable set to find the end of most recent quarter 
Declare @endofqrtr as date 
set @endofqrtr = convert(varchar,DATEADD(MONTH,DATEDIFF(MONTH,0,getdate()),-1),101) 


SELECT  MARKET 
     , COUNT(MARKET) AS COUNT ,Disenrollment,Enrollment  
from (SELECT DISTINCT 
         MBR_FIRST_NAME, MBR_LAST_NAME, MBR_DOB, MBR_MEDICARE_ID, MBR_COV_EFF_DATE, MBR_COV_END_DATE, MBR_ADMIN_DIV_CD AS MARKET, 
         CI_GRP_ID, MBR_STATUS_CD, COUNT(MBR_STATUS_CD) AS count, 
         CASE WHEN MBR_STATUS_CD = 'I' THEN 'Disenrollment' End as Disenrollment, Case WHEN MBR_STATUS_CD = 'A' THEN 'Enrollment' END AS Enrollment 
FROM   VW_CI_Snapshot_Today AS c 
WHERE  (NOT (PROD_TYPE_CODE = 'MCD')) AND (MBR_STATUS_CD IN ('I', 'A')) AND (MBR_ADMIN_DIV_CD IN ('CT', 'IA', 'IL', 'MA', 'ME', 'MI', 'MN', 'MT', 'ND', 'NE', 'NH', 'NJ', 
         'NY', 'RI', 'SD', 'VT', 'WI', 'WY')) AND (MBR_COV_END_DATE BETWEEN @1stofYear AND @endofqrtr) OR 
         (NOT (PROD_TYPE_CODE = 'MCD')) AND (MBR_STATUS_CD IN ('I', 'A')) AND (MBR_ADMIN_DIV_CD IN ('CT', 'IA', 'IL', 'MA', 'ME', 'MI', 'MN', 'MT', 'ND', 'NE', 'NH', 'NJ', 
         'NY', 'RI', 'SD', 'VT', 'WI', 'WY')) AND (ISNULL(MBR_COV_END_DATE, '12/31/9999') = '12/31/9999') 
GROUP BY MBR_STATUS_CD, MBR_ADMIN_DIV_CD,MBR_LAST_NAME, MBR_FIRST_NAME, MBR_DOB, MBR_MEDICARE_ID, MBR_COV_EFF_DATE, MBR_COV_END_DATE, CI_GRP_ID, MBR_ADMIN_DIV_CD) as tblCount 
Group by MARKET,Enrollment,Disenrollment,count 
order by MARKET 
+0

您是否想要每個市場的百分比? – Ewan

+0

是的,我需要爲每個市場的百分比 – lance9877

回答

0

我對您的查詢做了一些更改。注意如果您使用group by,則不需要使用distinct。

SELECT MARKET , COUNT(MARKET) AS MCOUNT ,Sum(Disenrollment) as Disenrollment, Sum(Enrollment) as Enrollment, Case when Sum(MBRcount) <> 0 then Sum(Disenrollment)/Sum(MBRcount) else 0 end as Disenroll, 
Case when Sum(MBRcount) <> 0 then Sum(Enrollment)/Sum(MBRcount) else 0 end as Enroll 
from (SELECT MBR_FIRST_NAME, MBR_LAST_NAME, MBR_DOB, MBR_MEDICARE_ID, MBR_COV_EFF_DATE, MBR_COV_END_DATE, MBR_ADMIN_DIV_CD AS MARKET, 
CI_GRP_ID, MBR_STATUS_CD, COUNT(MBR_STATUS_CD) AS MBRcount, 
CASE WHEN MBR_STATUS_CD = 'I' THEN 1 else 0 End as Disenrollment, Case WHEN MBR_STATUS_CD = 'A' THEN 1 else 0 END AS Enrollment 
FROM VW_CI_Snapshot_Today AS c 
WHERE (NOT (PROD_TYPE_CODE = 'MCD')) AND (MBR_STATUS_CD IN ('I', 'A')) AND (MBR_ADMIN_DIV_CD IN ('CT', 'IA', 'IL', 'MA', 'ME', 'MI', 'MN', 'MT', 'ND', 'NE', 'NH', 'NJ', 'NY', 'RI', 'SD', 'VT', 'WI', 'WY')) 
AND (MBR_COV_END_DATE BETWEEN @1stofYear AND @endofqrtr) OR (NOT (PROD_TYPE_CODE = 'MCD')) AND (MBR_STATUS_CD IN ('I', 'A')) 
AND (MBR_ADMIN_DIV_CD IN ('CT', 'IA', 'IL', 'MA', 'ME', 'MI', 'MN', 'MT', 'ND', 'NE', 'NH', 'NJ', 'NY', 'RI', 'SD', 'VT', 'WI', 'WY')) 
AND (ISNULL(MBR_COV_END_DATE, '12/31/9999') = '12/31/9999') 
GROUP BY MBR_STATUS_CD, MBR_ADMIN_DIV_CD,MBR_LAST_NAME, MBR_FIRST_NAME, MBR_DOB, MBR_MEDICARE_ID, MBR_COV_EFF_DATE, MBR_COV_END_DATE, CI_GRP_ID, MBR_ADMIN_DIV_CD) as tblCount 
Group by MARKET 
order by MARKET 
+0

這幾乎是我需要的,但它似乎dienroll總是0 – lance9877

+0

謝謝我能夠操縱這到我所需要的 – lance9877