简体   繁体   English

不同的子句在SQL Server 2005中不起作用

[英]Distinct Clause not working in SQL Server 2005

I have so many records having duplicate taskid assigned to multiple person, but i want to show distinct records means only one taskid in output in SQL 我有很多记录,其中重复的taskid分配给了多个人,但是我想显示不同的记录,这意味着SQL输出中只有一个taskid

Below is my query not working give me solution 下面是我的查询无法正常工作给我解决方案

SELECT DISTINCT 
    taskid, taskname, person, userid, dept, date, status, monitor, 
    comments, monitor_comments, respondtime, assignedby, 
    reassigncomment, priority,date_complete, followup_date, re_status
FROM
    task
WHERE     
    (status IS NULL)

in your case, result is distinct but not of your desire because you need only distinct task id then you should use this: 在您的情况下,结果是不同的,但不是您想要的,因为您只需要不同的任务ID,则应使用以下命令:

SELECT DISTINCT taskid
FROM         task
WHERE     (status IS NULL) 

then result would be distinct task ids. 那么结果将是不同的任务ID。

First, if you have a column called taskid in a table called task , I think it should be unique -- unless it is somehow a slowly changing dimension. 首先,如果在名为task的表中有一个名为taskid的列,我认为它应该是唯一的-除非它是一个缓慢变化的维度。

If it is not unique, then you are begging the question: which row do you want? 如果不是唯一的,那么您会提出一个问题:您要哪一行?

In any case, SQL Server 2005 have a function called row_number() that can solve your problem: 无论如何,SQL Server 2005都有一个名为row_number()的函数可以解决您的问题:

select t.*
from (select t.*, row_number() over (partition by taskid order by taskid) as seqnum
      from task
     ) t
where seqnum = 1;

This will return one arbitrary row for each taskid . 这将为每个taskid返回一个任意行。 If you have a way of preferring one row over another, then adjust the order by clause. 如果您有优先于一行的方式,请调整order by子句。

i have added a column priority in which the value of that column is 1 of same TASKID and other will be 0 so i can find 我添加了列优先级,其中该列的值是相同TASKID的1,其他将是0,所以我可以找到

SELECT DISTINCT taskid, taskname, person, userid, dept, date, status, monitor, comments, monitor_comments, respondtime, assignedby, reassigncomment, priority,date_complete, followup_date, re_status FROM task WHERE SELECT DISTINCT taskid,taskname,person,userid,dept,date,status,monitor,comment,monitor_comments,responsetime,assignedby,reassigncomment,priority,date_complete,followup_date,re_status从任务WHERE
(status IS NULL) and (priority='1') (状态为NULL)和(优先级='1')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM