gpt4 book ai didi

sql - 忽略特定列中的相同值

转载 作者:行者123 更新时间:2023-12-04 05:29:08 26 4
gpt4 key购买 nike

我有这个查询可以正常工作。但是,查询在 [id2] 列下返回​​相同的 int 值时遇到问题。我需要帮助仅获取值(value)的一个实例而忽略另一个。基本上捕获一个唯一的[id2]。任何帮助将不胜感激。另外,如果您需要澄清,请告诉我。

SELECT [id],[id1]
,[id2]
,[name]
,[date]
,[user]
FROM [MyDatabase.table]
Where [date]>= dateadd(day,datediff(day,0,getdate()),-5)
ORDER BY [cid]

编辑 1:
id1       id2    name        time                user
6466 171477 item1 2012-10-10 07:08:48.000 user1
6469 171477 item1 2012-10-10 07:11:01.000 user1
6468 171477 item1 2012-10-10 07:10:37.000 user1
6465 171477 item1 2012-10-10 07:07:43.000 user1
6464 171477 item1 2012-10-10 07:06:58.000 user1
6467 171477 item1 2012-10-10 07:09:35.000 user1
6474 173026 item2 2012-10-10 10:20:21.000 user2
6478 173297 item3 2012-10-10 11:31:55.000 user3
6472 175445 item4 2012-10-10 07:18:17.000 user1
6460 175977 item5 2012-10-08 07:42:39.000 user4
6473 176253 item6 2012-10-10 10:18:21.000 user2
6471 176253 item6 2012-10-10 10:15:03.000 user2
6470 176253 item6 2012-10-10 10:14:34.000 user2

应该是:
id1     id2    name        time                user
6466 171477 item1 2012-10-10 07:08:48.000 user1
6474 173026 item2 2012-10-10 10:20:21.000 user2
6478 173297 item3 2012-10-10 11:31:55.000 user3
6472 175445 item4 2012-10-10 07:18:17.000 user1
6460 175977 item5 2012-10-08 07:42:39.000 user4
6473 176253 item6 2012-10-10 10:18:21.000 user2

最佳答案

;WITH tbl_online AS (
SELECT [id],[r_id]
,[cid]
,[name]
,[date]
,[user] ,ROW_NUMBER () OVER (
PARTITION BY [cid] ORDER BY [cid] ) AS Rnum
FROM [MyDatabase.table]
Where [date]>= dateadd(day,datediff(day,0,getdate()),-5) )
SELECT * FROM tbl_online WHERE Rnum !>1
ORDER BY [cid]

关于sql - 忽略特定列中的相同值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12844796/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com