简体   繁体   English

选择具有随机ID的不同两列

[英]select distinct two columns with random id

I would like to get distinct record of each Design and type with random id of each record It is not possible to use 我希望获得每个设计的明确记录,并使用每个记录的随机ID输入它是不可能使用的

select distinct Design, Type, ID from table

It will return all values This is structure of my table 它将返回所有值这是我的表的结构

Design | Type | ID
old chair 1
old table 2
old chair 3
new chair 4
new table 5
new table 6
newest chair 7

Possible result 可能的结果

Design | Type | ID
old table 2
old chair 3
new chair 4
new table 6
newest chair 7

If it doesn't matter which one, you can always take the maximum\\minimum one: 如果哪个无关紧要,你可以随时取最大\\最小值:

SELECT design,type,max(ID)
FROM YourTable
GROUP BY design,type

This won't be randomly, it will always take the maximum\\minimum one but it doesn't seems like it matters. 这不会是随机的,它总是需要最大的\\最小值,但似乎并不重要。

Try this one: 试试这个:

select *
from (
select *, row_number() over (partition by Design,Type order by id desc) rowID
from @tab
) x
where rowID = 1

Hope this one helps you : 希望这个可以帮助你:

WITH CTE AS
(
    SELECT Design, Type, ID, ROW_NUMBER() OVER (PARTITION BY Design, 
                                                   Type ORDER BY id DESC) rid
    FROM table
)
SELECT Design, Type, ID FROM CTE WHERE rid = 1 ORDER BY ID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM