我想删除AUTHORS_CONVERSATION
列中的重复行。
我的代码是:
SELECT AUTHORS.AUTHORS_PHOTO,
AUTHORS.AUTHORS_ID,
AUTHORS.AUTHORS_NAME,
AUTHORS_MESSAGES.AUTHORS_RECEIVER_ID,
AUTHORS_MESSAGES.AUTHORS_SENDER_ID,
AUTHORS_MESSAGES.MESSAGE_CONTENT,
AUTHORS_MESSAGES.MESSAGE_DATE,
AUTHORS_MESSAGES.CONVERSATION_ID,
AUTHORS_MESSAGES.MESSAGE_STATUS
FROM AUTHORS_MESSAGES
INNER JOIN AUTHORS
ON AUTHORS_MESSAGES.AUTHORS_SENDER_ID = AUTHORS.AUTHORS_ID
OR AUTHORS_MESSAGES.AUTHORS_RECEIVER_ID = AUTHORS.AUTHORS_ID
WHERE AUTHORS.AUTHORS_ID = 1555
ORDER BY MESSAGE_DATE DESC;
结果示例:
AUTHORS_ID, AUTHORS_CONVERSATION
1555, 1
AUTHORS_ID, AUTHORS_CONVERSATION
1555, 1
AUTHORS_ID, AUTHORS_CONVERSATION
1555, 1
AUTHORS_ID, AUTHORS_CONVERSATION
1555, 4
应该结果:
AUTHORS_ID, AUTHORS_CONVERSATION
1555, 1
AUTHORS_ID, AUTHORS_CONVERSATION
1555, 4
如果重复,我如何仅按 DATE 显示最后结果排序?谢谢!
答案 0 :(得分:0)
我目前无法尝试此查询......但请尝试让我知道
SELECT AUTHORS.AUTHORS_PHOTO,
AUTHORS.AUTHORS_ID,
AUTHORS.AUTHORS_NAME,
AUTHORS_MESSAGES.AUTHORS_RECEIVER_ID,
AUTHORS_MESSAGES.AUTHORS_SENDER_ID,
AUTHORS_MESSAGES.MESSAGE_CONTENT,
AUTHORS_MESSAGES.MESSAGE_DATE,
AUTHORS_MESSAGES.CONVERSATION_ID,
AUTHORS_MESSAGES.MESSAGE_STATUS
FROM (SELECT AUTHORS.AUTHORS_PHOTO,
AUTHORS.AUTHORS_ID,
AUTHORS.AUTHORS_NAME,
AUTHORS_MESSAGES.AUTHORS_RECEIVER_ID,
AUTHORS_MESSAGES.AUTHORS_SENDER_ID,
AUTHORS_MESSAGES.MESSAGE_CONTENT,
AUTHORS_MESSAGES.MESSAGE_DATE,
AUTHORS_MESSAGES.CONVERSATION_ID,
AUTHORS_MESSAGES.MESSAGE_STATUS ,ROW_NUMBER () OVER (PARTITION BY AUTHORS_CONVERSATION ORDER BY MESSAGE_DATE DESC) AS RowNumber FROM AUTHORS_MESSAGES
INNER JOIN AUTHORS
ON AUTHORS_MESSAGES.AUTHORS_SENDER_ID = AUTHORS.AUTHORS_ID
OR AUTHORS_MESSAGES.AUTHORS_RECEIVER_ID = AUTHORS.AUTHORS_ID
WHERE AUTHORS.AUTHORS_ID = 1555
ORDER BY MESSAGE_DATE DESC) AS a
WHERE a.RowNumber = 1
答案 1 :(得分:0)
你试过按ID分组吗?
示例:
WHERE AUTHORS.AUTHORS_ID = 1555, GROUP BY AUTHORS_ID;
如果你只想要的话,为什么你要在DATE之前订购 AUTHORS_ID和AUTHORS_CONVERSATION?
答案 2 :(得分:0)
; WITH CTE AS(
SELECT
AUTHORS.AUTHORS_PHOTO,
AUTHORS.AUTHORS_ID,
AUTHORS.AUTHORS_NAME,
AUTHORS_MESSAGES.AUTHORS_RECEIVER_ID,
AUTHORS_MESSAGES.AUTHORS_SENDER_ID,
AUTHORS_MESSAGES.MESSAGE_CONTENT,
AUTHORS_MESSAGES.MESSAGE_DATE,
AUTHORS_MESSAGES.CONVERSATION_ID,
AUTHORS_MESSAGES.MESSAGE_STATUS,
ROW_NUMBER() OVER (PARTITION BY AUTHORS_ID, CONVERSATION_ID ORDER BY MESSAGE_DATE DESC) AS RN
FROM AUTHORS_MESSAGES
INNER JOIN AUTHORS
ON AUTHORS_MESSAGES.AUTHORS_SENDER_ID = AUTHORS.AUTHORS_ID
OR AUTHORS_MESSAGES.AUTHORS_RECEIVER_ID = AUTHORS.AUTHORS_ID
WHERE AUTHORS.AUTHORS_ID = 1555
ORDER BY MESSAGE_DATE DESC;
) SELECT * FROM CTE
WHERE RN = 1
假设您需要按AUTHORS_ID,CONVERSATION_ID排序重复行,并按MESSAGE_DATE排序。如果不同,请修改 PARTITOIN和ORDER部分
答案 3 :(得分:0)
如果您试图避免某些x,y,z列的重复,请在这些列上保留一个组。