我失去了對這個我有4列:SQL百分比
Market
,Count
,Disnerollment
,Enrollment
我需要得到disenrollment
和enrollment
之間的百分比但它們都是基於列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
您是否想要每個市場的百分比? – Ewan
是的,我需要爲每個市場的百分比 – lance9877