比较两个日期并避免重复

时间:2015-04-29 14:18:56

标签: sql oracle date-arithmetic

我正在使用HP ALM,我尝试执行此查询:

SELECT  ONE.ID, ONE.Num_QC_EDF, ONE.Statut, TWO.Proprietaire, ONE.ChangeDate
FROM
(SELECT B.BG_BUG_ID as ID, B.BG_USER_15 as Num_QC_EDF, AP.AP_NEW_VALUE as Statut, BG_RESPONSIBLE as Proprietaire, AL.AU_TIME as ChangeDate
FROM  BUG B
INNER JOIN AUDIT_LOG AL
ON AL.AU_ENTITY_ID = B.BG_BUG_ID
INNER JOIN AUDIT_PROPERTIES AP
ON AL.AU_ACTION_ID = AP.AP_ACTION_ID
AND AL.AU_ENTITY_TYPE = 'BUG'
AND AL.AU_ACTION_ID= AP.AP_ACTION_ID
AND AP_FIELD_NAME= 'BG_STATUS')ONE,
(SELECT B.BG_BUG_ID as ID,B.BG_USER_15 as Num_QC_EDF, AP.AP_NEW_VALUE as Proprietaire , BG_STATUS as Statut , AL.AU_TIME as ChangeDate
FROM  BUG B
INNER JOIN AUDIT_LOG AL
ON AL.AU_ENTITY_ID = B.BG_BUG_ID
INNER JOIN AUDIT_PROPERTIES AP
ON AL.AU_ACTION_ID = AP.AP_ACTION_ID
AND AL.AU_ENTITY_TYPE = 'BUG'
AND AL.AU_ACTION_ID= AP.AP_ACTION_ID
AND AP.AP_FIELD_NAME = 'BG_RESPONSIBLE'
AND B.BG_STATUS = 'TU leger OK')TWO,
BUG B
WHERE
ONE.ID= TWO.ID AND ONE.Num_QC_EDF= TWO.Num_QC_EDF AND ONE.STATUT= TWO.STATUT and ONE.Proprietaire= TWO.Proprietaire and TWO.ChangeDate <= ONE.ChangeDate
Order by BG_BUG_ID, Num_QC_EDF, Proprietaire, ChangeDate

我想比较位于第二个子查询中的ChangeDate和第一个子查询中的一个必须是&lt; =,同样整个查询必须不返回其结果中的重复行(PS:distinct在嵌套查询中不起作用)

我正在寻求帮助。

1 个答案:

答案 0 :(得分:0)

首先,您添加了BUG B,它不会与其他两个子查询表ONETWO联接。所以,你得到了很多重复。

在不知道您的表格结构的情况下,无法更改您的查询。

如果您不想更改查询并仍希望获取唯一记录,则可以执行以下操作:

Select DISTINCT AA.* FROM (*YOUR ENTIRE QUERY*) AA; 

不推荐以上解决方案,您需要将BUG B正确加入其他两个表格。