簡體   English   中英

案例陳述中的計數功能

[英]Count function in a case statement

我有以下查詢計數成員:

`SELECT 
distinct count(cst_recno) AS [Member ID],
adr_country AS Country
FROM 
mb_membership
JOIN mb_member_type on mbt_key = mbr_mbt_key
JOIN co_customer ON cst_key=mbr_cst_key and mbr_delete_flag =0 and 
cst_delete_flag=0 
LEFT JOIN co_individual ON cst_key=ind_cst_key and ind_delete_flag=0
LEFT JOIN co_customer_x_customer ON cxc_cst_key_1 = co_customer.cst_key and 
(cxc_end_date is null or datediff(dd,getdate(),cxc_end_date) >=0) and 
cxc_rlt_code='Chapter Member' 
LEFT JOIN co_chapter ON cxc_cst_key_2=chp_cst_key 
LEFT JOIN co_customer_x_address ON cst_cxa_key=cxa_key 
LEFT JOIN co_address ON adr_key=cxa_adr_key 
LEFT JOIN co_country on adr_country=cty_code
LEFT JOIN co_region on rgn_key=cty_rgn_key 
LEFT JOIN vw_client_uli_member_type  WITH (NOLOCK)  ON cst_key=mem_cst_key 
WHERE mbr_join_date >= '7/1/2015' and mbt_code not like '%Council%'
AND ind_int_code <> 'staff' and cst_org_name_dn not like '%urban land ins%' 
and cst_eml_address_dn not like '%@uli.org'
and adr_country ='singapore'
group by adr_country`

查詢返回:

Member ID   Country
145         Singapore

但是,我知道它包括一些重復項,因為當我取出一個count函數並且沒有group by子句時,它將返回140個不同的行。 當我檢查為什么其中包含一些重復項時,是由其中一個日期引起的。 你能建議嗎? 我基本上想運行查詢,以顯示140個不同的Member ID計數:

Member ID   Country
140         Singapore

而不是SELECT distinct count(cst_recno)...使用SELECT count(distinct cst_recno)...

SELECT 
count(distinct cst_recno) AS [Member ID],
adr_country AS Country
FROM 
mb_membership
JOIN mb_member_type on mbt_key = mbr_mbt_key
JOIN co_customer ON cst_key=mbr_cst_key and mbr_delete_flag =0 and 
cst_delete_flag=0 
LEFT JOIN co_individual ON cst_key=ind_cst_key and ind_delete_flag=0
LEFT JOIN co_customer_x_customer ON cxc_cst_key_1 = co_customer.cst_key and 
(cxc_end_date is null or datediff(dd,getdate(),cxc_end_date) >=0) and 
cxc_rlt_code='Chapter Member' 
LEFT JOIN co_chapter ON cxc_cst_key_2=chp_cst_key 
LEFT JOIN co_customer_x_address ON cst_cxa_key=cxa_key 
LEFT JOIN co_address ON adr_key=cxa_adr_key 
LEFT JOIN co_country on adr_country=cty_code
LEFT JOIN co_region on rgn_key=cty_rgn_key 
LEFT JOIN vw_client_uli_member_type  WITH (NOLOCK)  ON cst_key=mem_cst_key 
WHERE mbr_join_date >= '7/1/2015' and mbt_code not like '%Council%'
AND ind_int_code <> 'staff' and cst_org_name_dn not like '%urban land ins%' 
and cst_eml_address_dn not like '%@uli.org'
and adr_country ='singapore'
group by adr_country`

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM