Distinct in SQL Server

巧了我就是萌 提交于 2019-12-18 09:14:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!