我有一个看起来像这样的临时表:
RequestID | CreatedDate | HistoryStatus
CF-0000001 | 8/26/2009 1:07:01 PM | For Review
CF-0000001 | 8/26/2009 1:07:01 PM | Completed
CF-0000112 | 8/26/2009 1:07:01 PM | For Review
CF-0000113 | 8/26/2009 1:07:01 PM | For Review
CF-0000114 | 8/26/2009 1:07:01 PM | Completed
CF-0000115 | 8/26/2009 1:07:01 PM | Completed
我希望桌子看到最后的结果是这样的:
RequestID | CreatedDate | HistoryStatus
CF-0000001 | 8/26/2009 1:07:01 PM | Completed
CF-0000112 | 8/26/2009 1:07:01 PM | For Review
CF-0000113 | 8/26/2009 1:07:01 PM | For Review
CF-0000114 | 8/26/2009 1:07:01 PM | Completed
CF-0000115 | 8/26/2009 1:07:01 PM | Completed
即。应删除重复的CF-0000001
。
如果有多个重复行且仍然返回不重复的行,我该如何返回或者我应该只选择一行?
答案 0 :(得分:11)
如果要根据RequestID和CreatedDate显示重复行之一并显示最新的HistoryStatus,请尝试此操作。
with t as (select row_number()over(partition by RequestID,CreatedDate order by RequestID) as rnum,* from tbltmp)
Select RequestID,CreatedDate,HistoryStatus from t a where rnum in (SELECT Max(rnum) FROM t GROUP BY RequestID,CreatedDate having t.RequestID=a.RequestID)
或者如果您只想选择CreatedDate中的一个重复行并显示最新的HistoryStatus,请尝试以下查询。
with t as (select row_number()over(partition by CreatedDate order by RequestID) as rnum,* from tbltmp)
Select RequestID,CreatedDate,HistoryStatus from t where rnum = (SELECT Max(rnum) FROM t)
或者,如果您想选择仅考虑请求ID的重复行之一并显示最新的HistoryStatus,请使用下面的查询
with t as (select row_number()over(partition by RequestID order by RequestID) as rnum,* from tbltmp)
Select RequestID,CreatedDate,HistoryStatus from t a where rnum in (SELECT Max(rnum) FROM t GROUP BY RequestID,CreatedDate having t.RequestID=a.RequestID)
我在sql server 2005中编写的所有上述查询。
答案 1 :(得分:5)
从标题我猜你每个独特的行只需要一个结果?如果是这种情况,请查看GROUP BY子句(或SELECT DISTINCT)。
答案 2 :(得分:4)
select t.*
from (
select RequestID, max(CreatedDate) as MaxCreatedDate
from table1
group by RequestID
) tm
inner join table1 t on tm.RequestID = t.RequestID and tm.MaxCreatedDate = t.CreatedDate
答案 3 :(得分:3)
如果查询中存在一对多关系,则可能会在一侧出现重复的行。
假设以下
TABLE TEAM
ID TEAM_NAME
0 BULLS
1 LAKERS
TABLE PLAYER
ID TEAM_ID PLAYER_NAME
0 0 JORDAN
1 0 PIPPEN
然后执行类似
的查询SELECT
TEAM.TEAM_NAME,
PLAYER.PLAYER_NAME
FROM TEAM
INNER JOIN PLAYER
你会得到
TEAM_NAME PLAYER_NAME
BULLS JORDAN
BULLS PIPPEN
所以你将拥有重复的TEAM NAME。即使使用DISTINCT子句,您的结果集也将包含重复的TEAM NAME
因此,如果您不想在查询中使用重复的TEAM_NAME,请执行以下操作
SELECT ID, TEAM_NAME FROM TEAM
对于遇到的每个团队ID执行
SELECT PLAYER_NAME FROM PLAYER WHERE TEAM_ID = <PUT_TEAM_ID_RIGHT_HERE>
所以这样你就不会在一边获得重复参考
的问候,
答案 4 :(得分:0)
如果这是一个SQL问题,并且我理解你在问什么,(它并不完全清楚),只需在查询中添加distinct
Select Distinct * From TempTable
答案 5 :(得分:0)
select * from temptable
where rnum --unique key
in
(
SELECT RNUM --unique key
FROM temptable
WHERE ( HistoryStatus
) IN (SELECT HistoryStatus
FROM temptable
GROUP BY
HistoryStatus
HAVING COUNT(*) <= 1));
我还没有测试过这段代码。我使用了类似的代码,它的工作原理。 语法在Oracle中。
答案 6 :(得分:0)
使用命名空间和子查询您可以这样做:
declare @data table (RequestID varchar(20), CreatedDate datetime, HistoryStatus varchar(20))
insert into @data values ('CF-0000001','8/26/2009 1:07:01 PM','For Review');
insert into @data values ('CF-0000001','8/26/2009 1:07:01 PM','Completed');
insert into @data values ('CF-0000112','8/26/2009 1:07:01 PM','For Review');
insert into @data values ('CF-0000113','8/26/2009 1:07:01 PM','For Review');
insert into @data values ('CF-0000114','8/26/2009 1:07:01 PM','Completed');
insert into @data values ('CF-0000115','8/26/2009 1:07:01 PM','Completed');
select d1.RequestID,d1.CreatedDate,d1.HistoryStatus
from @data d1
where d1.HistoryStatus = 'Completed'
union all
select d2.RequestID,d2.CreatedDate,d2.HistoryStatus
from @data d2
where d2.HistoryStatus = 'For Review'
and d2.RequestID not in (
select RequestID
from @data
where HistoryStatus = 'Completed'
and CreatedDate = d2.CreatedDate
)
以上查询返回
CF-0000001, 2009-08-26 13:07:01.000, Completed
CF-0000114, 2009-08-26 13:07:01.000, Completed
CF-0000115, 2009-08-26 13:07:01.000, Completed
CF-0000112, 2009-08-26 13:07:01.000, For Review
CF-0000113, 2009-08-26 13:07:01.000, For Review
答案 7 :(得分:-2)
尝试使用select distinct x。* from ( 您的查询 )
感谢。
答案 8 :(得分:-3)
要从两行的重复列中仅获取一个不同的记录,您可以使用由oracle自身维护的“rowid”列作为主键,因此首先尝试
"select rowid,RequestID,CreatedDate,HistoryStatus from temptable;"
然后你可以通过在SELECT语句中使用它的'rowid'列的值来获取第二行。