问题
I am executing the following query,
Select distinct
a.cr_id,
Case
When ca.ca_vote = 'Approve' and ca.ca_title='MANAGER' Then ca.ca_email
When ca.ca_vote = 'Reject' Then ''
When ca.ca_vote = 'Pending' Then ''
When ca.ca_vote = 'IN PROCESS' Then ''
End as ca_email
from
credit a
inner join credit_approvals ca on ca.c_id=a.cr_id
where
a.cr_cs_date between Convert(varchar(20),'11/16/2011',101) and dateadd(day,1,convert (varchar(20),'11/16/2011',101))
order by
a.cr_id
Despite distinct
for cr_id
, it is still displaying the duplicate values. Please let me know how to handle this, so that I could able to display only distinct records.
回答1:
Distinct
is applied to all columns, not the one which is immediately after Distinct
.
If you have several different ca_email
for a cr_id
, you will see them all.
If you don't want that, you have to come up with a rule to decide what record among the duplicates must stay.
来源:https://stackoverflow.com/questions/8173137/distinct-in-sql-server