使用 Distinct 时仍然得到重复值?

发布于 2024-08-05 18:21:03 字数 1476 浏览 5 评论 0原文

使用SQL Server 2000

如何避免重复值?

查询

SELECT DISTINCT 
    Modification.dbo.Reference.Dates AS DailyDate, 
    tmp_Cardevent2.PERSONID, 
    tmp_Cardevent2.empname, 
    tmp_cardevent2.cardno, 
    tmp_Cardevent2.titlecode, 
    tmp_Cardevent2.titlename, 
    tmp_Cardevent2.departname, 
    CASE 
      WHEN tmp_Cardevent2.CardEventDate = Modification.dbo.Reference.Dates 
        THEN tmp_Cardevent2.CardEventDate 
        ELSE '-----' 
    END AS EMPDATE, 
    CASE 
      WHEN tmp_Cardevent2.CardEventDate = Modification.dbo.Reference.Dates 
        THEN tmp_Cardevent2.Intime 
        ELSE '-----' 
    END AS INTIME
WHEN tmp_Cardevent2.CardEventDate = Modification.dbo.Reference.Dates 
            THEN tmp_Cardevent2.outtime 
            ELSE '-----' 
        END AS outtime 
    FROM tmp_Cardevent2 
    CROSS JOIN  Modification.dbo.Reference
    ORDER BY 
       PERSONID, DAILYDATE DESC

输出:

DailyDates, Personid, empname, cardno, titlecode, titlename, departname, empdate, intime, outtime

12/30/2008 A201 A Cherian 3201 018 Chief Air Traffic Service Assistant Air Traffic Services ----- ----- 

12/30/2008 A201 A Cherian 3201 018 Chief Air Traffic Service Assistant Air Traffic Services 20081230 07:51:31 15:54:38

12/30/2008 A201 A Cherian 3201 018 Chief Air Traffic Service Assistant Air Traffic Services 20081230 07:51:31 15:54:38

我的查询有什么问题,为什么会得到重复的值?如何避免重复值。

请帮忙解决我的问题。

Using SQL Server 2000

How to avoid the duplicate values?

Query

SELECT DISTINCT 
    Modification.dbo.Reference.Dates AS DailyDate, 
    tmp_Cardevent2.PERSONID, 
    tmp_Cardevent2.empname, 
    tmp_cardevent2.cardno, 
    tmp_Cardevent2.titlecode, 
    tmp_Cardevent2.titlename, 
    tmp_Cardevent2.departname, 
    CASE 
      WHEN tmp_Cardevent2.CardEventDate = Modification.dbo.Reference.Dates 
        THEN tmp_Cardevent2.CardEventDate 
        ELSE '-----' 
    END AS EMPDATE, 
    CASE 
      WHEN tmp_Cardevent2.CardEventDate = Modification.dbo.Reference.Dates 
        THEN tmp_Cardevent2.Intime 
        ELSE '-----' 
    END AS INTIME
WHEN tmp_Cardevent2.CardEventDate = Modification.dbo.Reference.Dates 
            THEN tmp_Cardevent2.outtime 
            ELSE '-----' 
        END AS outtime 
    FROM tmp_Cardevent2 
    CROSS JOIN  Modification.dbo.Reference
    ORDER BY 
       PERSONID, DAILYDATE DESC

Output:

DailyDates, Personid, empname, cardno, titlecode, titlename, departname, empdate, intime, outtime

12/30/2008 A201 A Cherian 3201 018 Chief Air Traffic Service Assistant Air Traffic Services ----- ----- 

12/30/2008 A201 A Cherian 3201 018 Chief Air Traffic Service Assistant Air Traffic Services 20081230 07:51:31 15:54:38

12/30/2008 A201 A Cherian 3201 018 Chief Air Traffic Service Assistant Air Traffic Services 20081230 07:51:31 15:54:38

What problem in my query, why am getting duplicate values? How to avoid the duplicate values.

Please can any help to solve my problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

删除→记忆 2024-08-12 18:21:04

这些日期是 DateTime 类型还是 Date 类型?如果它们包含时间,这将是它们不相等的原因。当结果被格式化时,时间就被削减了。

尝试仅选择这些值的日期部分。

Are these dates of type DateTime or just Date? If they include a time, this will be the reason why they are not equal. The time is just cut when the result is formatted.

Try to select only the date part of these values.

情仇皆在手 2024-08-12 18:21:04

首先,您给定的结果集:

DailyDates  Personid  empname    cardno  titlecode  titlename                            departname            empdate   intime    outtime
----------  --------  ---------  ------  ---------  -----------------------------------  --------------------  --------  --------  --------
12/30/2008  A201      A Cherian  3201    018        Chief Air Traffic Service Assistant  Air Traffic Services  -----     -----    
12/30/2008  A201      A Cherian  3201    018        Chief Air Traffic Service Assistant  Air Traffic Services  20081230  07:51:31  15:54:38
12/30/2008  A201      A Cherian  3201    018        Chief Air Traffic Service Assistant  Air Traffic Services  20081230  07:51:31  15:54:38

与给定的查询不匹配,查询中没有“outtime”列:

SELECT DISTINCT 
    Modification.dbo.Reference.Dates AS DailyDate
        ,tmp_Cardevent2.PERSONID
        ,tmp_Cardevent2.empname
        ,tmp_cardevent2.cardno
        ,tmp_Cardevent2.titlecode
        ,tmp_Cardevent2.titlename
        ,tmp_Cardevent2.departname
        ,CASE 
             WHEN tmp_Cardevent2.CardEventDate = Modification.dbo.Reference.Dates THEN tmp_Cardevent2.CardEventDate 
             ELSE '-----'
         END AS EMPDATE
        ,CASE
             WHEN tmp_Cardevent2.CardEventDate = Modification.dbo.Reference.Dates THEN tmp_Cardevent2.Intime
             ELSE '-----' 
         END AS INTIME
    FROM tmp_Cardevent2
        CROSS JOIN Modification.dbo.Reference
    ORDER BY PERSONID, DAILYDATE DESC

不知道表模式以及它们如何相互关联,以及列数据类型:

Modification.dbo.Reference.Dates
tmp_Cardevent2.CardEventDate
tmp_Cardevent2.Intime

这很难了解重复的真正原因是什么。但是,根据问题中的有限信息,日期和/或时间列之一的格式很可能隐藏了值的实际差异。

first off, your given result set:

DailyDates  Personid  empname    cardno  titlecode  titlename                            departname            empdate   intime    outtime
----------  --------  ---------  ------  ---------  -----------------------------------  --------------------  --------  --------  --------
12/30/2008  A201      A Cherian  3201    018        Chief Air Traffic Service Assistant  Air Traffic Services  -----     -----    
12/30/2008  A201      A Cherian  3201    018        Chief Air Traffic Service Assistant  Air Traffic Services  20081230  07:51:31  15:54:38
12/30/2008  A201      A Cherian  3201    018        Chief Air Traffic Service Assistant  Air Traffic Services  20081230  07:51:31  15:54:38

does not match your given query, there is no "outtime" column in the query:

SELECT DISTINCT 
    Modification.dbo.Reference.Dates AS DailyDate
        ,tmp_Cardevent2.PERSONID
        ,tmp_Cardevent2.empname
        ,tmp_cardevent2.cardno
        ,tmp_Cardevent2.titlecode
        ,tmp_Cardevent2.titlename
        ,tmp_Cardevent2.departname
        ,CASE 
             WHEN tmp_Cardevent2.CardEventDate = Modification.dbo.Reference.Dates THEN tmp_Cardevent2.CardEventDate 
             ELSE '-----'
         END AS EMPDATE
        ,CASE
             WHEN tmp_Cardevent2.CardEventDate = Modification.dbo.Reference.Dates THEN tmp_Cardevent2.Intime
             ELSE '-----' 
         END AS INTIME
    FROM tmp_Cardevent2
        CROSS JOIN Modification.dbo.Reference
    ORDER BY PERSONID, DAILYDATE DESC

Without knowing the table schemas and how they relate to each other, as well as the column datatypes of:

Modification.dbo.Reference.Dates
tmp_Cardevent2.CardEventDate
tmp_Cardevent2.Intime

it is hard to understand what is the true cause of the duplicates. However, based on the limited info inthe question, it is most likely that the formatting of one one the date and/or time columns is hiding an actual difference in values.

对你的占有欲 2024-08-12 18:21:04

您可以尝试将另一个不同的选择包装为子查询,例如,

select distinct * from (SELECT DISTINCT 
    Modification.dbo
    ...) q

需要 q 并将名称查询作为伪表。

不太确定为什么第一个不同的不起作用,也许 CASE 混淆了它的优化器?

You could try wrapping another select distinct around that as a subquery, e.g.

select distinct * from (SELECT DISTINCT 
    Modification.dbo
    ...) q

Where q is required and names query as a pseudo-table.

Not really sure why the first distinct isn't working, perhaps the CASE is confusing its optimizer?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文