返回的结果包括结果代码是否已完成两次,即1个数据记录,但PZ01在同一记录上使用两次。我只想算一次。每条记录都有一个唯一的ID,我不想在此报告中显示。所以我希望这个运行给我总和,但是来自独特的记录ID,因此它不会被计算多次。
select
sum(case when result_code = 'PZ01' then 1 else 0 end) as Hub_Box_Activated
,sum(case when result_code = 'NOTI' then 1 else 0 end) as Not_Interested
,sum(case when result_code = 'PZ02' then 1 else 0 end) as No_Tablet_Installed
,sum(case when result_code = 'PZ03' then 1 else 0 end) as Customer_Query
,sum(case when result_code = 'PZ26' then 1 else 0 end) as NI_Security_Concerns
,sum(case when result_code = 'PZ27' then 1 else 0 end) as NI_Storage_Concerns
,sum(case when result_code = 'PZ28' then 1 else 0 end) as NI_Using_alternative_provider
,sum(case when result_code = 'PZ29' then 1 else 0 end) as NI_Too_Much_Hassle
from
history
where
list_id in ('1432','1604','1607')
答案 0 :(得分:2)
使用count
尝试distinct
。只需使用列名
record_id
即可
select
count(distinct case when result_code = 'PZ01' then record_id end) as Hub_Box_Activated
,count(distinct case when result_code = 'NOTI' then record_id end) as Not_Interested
,count(distinct case when result_code = 'PZ02' then record_id end) as No_Tablet_Installed
,count(distinct case when result_code = 'PZ03' then record_id end) as Customer_Query
,count(distinct case when result_code = 'PZ26' then record_id end) as NI_Security_Concerns
,count(distinct case when result_code = 'PZ27' then record_id end) as NI_Storage_Concerns
,count(distinct case when result_code = 'PZ28' then record_id end) as NI_Using_alternative_provider
,count(distinct case when result_code = 'PZ29' then record_id end) as NI_Too_Much_Hassle
from
history
where
list_id in ('1432','1604','1607')
答案 1 :(得分:1)
尝试在强制1值COUNT
语句中使用DISTINCT
和CASE
。请注意,这只会产生值1或0,因为使用特定值计算不同的result_code
将始终返回1(如果存在)或0(如果不存在)。
select
COUNT(DISTINCT(case when result_code = 'PZ01' then result_code end)) as Hub_Box_Activated
,COUNT(DISTINCT(case when result_code = 'NOTI' then result_code end)) as Not_Interested
,COUNT(DISTINCT(case when result_code = 'PZ02' then result_code end)) as No_Tablet_Installed
,COUNT(DISTINCT(case when result_code = 'PZ03' then result_code end)) as Customer_Query
,COUNT(DISTINCT(case when result_code = 'PZ26' then result_code end)) as NI_Security_Concerns
,COUNT(DISTINCT(case when result_code = 'PZ27' then result_code end)) as NI_Storage_Concerns
,COUNT(DISTINCT(case when result_code = 'PZ28' then result_code end)) as NI_Using_alternative_provider
,COUNT(DISTINCT(case when result_code = 'PZ29' then result_code end)) as NI_Too_Much_Hassle
from
history
where
list_id in ('1432','1604','1607')