使用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
我的查询中有什么问题,为什么会出现重复值?如何避免重复值。
请帮忙解决我的问题。
答案 0 :(得分:3)
这些日期是DateTime还是Date?如果他们包含时间,这将是他们不平等的原因。结果格式化后,时间就会被切断。
尝试仅选择这些值的日期部分。
答案 1 :(得分:1)
首先,您的给定结果集:
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
很难理解重复的真正原因是什么。但是,根据问题中的有限信息,一个日期和/或时间列的格式很可能隐藏了值的实际差异。
答案 2 :(得分:0)
您可以尝试将另一个选择不同的包围作为子查询,例如
select distinct * from (SELECT DISTINCT
Modification.dbo
...) q
其中q是必需的,并将查询命名为伪表。
不确定为什么第一个不同的东西不起作用,也许CASE混淆了它的优化器?