2016-10-13 50 views
0

我有以下代碼。如果它是確定的工作,則計數爲1;如果是其他工作,則計數爲0。如果是第三項工作,我需要一種計算0.5的方法。我已經嘗試過這一點,它似乎總是以整數計算。有沒有辦法使用SQL來做到這一點?我已經搜索並找不到這樣的方法來計算一些項目爲0.5。這將幫助生產總量是在訪問數據庫有條件地SQl總和物品

SELECT TOP 1000 
[Type of Work], 
COUNT(case when [Type of Work] IN 
('LEP Decisions', 
'Creditable Coverage', 
'Pends','Demographics', 
'Consents','POA','PCP','Housing Verifications', 
'LEP Cases') 
then 1 else Null END)as count 
    ,[User ID] 
FROM [Medicare_Enrollment].[dbo].[Sample] 
group by [Type of Work], [User ID] 

回答

1

我建議改變COUNTSUM(並由此總結10.50 - 代替數數Null):

select top 1000 
     [Type of Work], 
     sum (case 
       when [Type of Work] in ('LEP Decisions','Creditable Coverage','Pends','Demographics','Consents','POA','PCP','Housing Verifications','LEP Cases') then 
        1 
       when [Type of Work] in ('SOME OTHER WORK', 'THIRD WORK') then 
        0.5 
       else 
        0 -- nothing to add 
       end) as count, 
     [User ID] 
    from [Medicare_Enrollment].[dbo].[Sample] 
group by [Type of Work], 
     [User ID] 
+0

工作謝謝我知道我錯過了一些東西 – lance9877

1
SELECT TOP 1000 
[Type of Work], 
SUM(case when [Type of Work] IN ('LEP Decisions','Creditable Coverage','Pends','Demographics','Consents','POA','PCP','Housing Verifications','LEP Cases') then 1 
WHEN [Type of Work] IN ('') -- Put your work list 
THEN 0.5 
else 0 END)as count 
,[User ID] 
FROM [Medicare_Enrollment].[dbo].[Sample] 
group by [Type of Work], [User ID] 
+0

我相信COUNT也應該被SUM –

+0

取代哦,是的。我的錯。 – Esty