多个值的SQL百分比

时间:2015-04-15 15:06:25

标签: sql sql-server-2008 variables percentage

我迷失了,我有4列:

MarketCountDisnerollmentEnrollment

我需要获得disenrollmentenrollment之间的百分比,但是它们基于MBRstatus列,我制作了两个单独的案例陈述,以便将其分开。

我不知道如何设置这样设置的百分比这样的百分比,但我也不知道如何让列分开另一种方式任何帮助将被赞赏

我已经包含了查询

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

1 个答案:

答案 0 :(得分: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