select 具有随机 ID 的不同两列
select distinct two columns with random id
我想获得每个设计和类型的不同记录,每个记录的随机 ID
无法使用
select distinct Design, Type, ID from table
它将return所有值
这是我的 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
可能的结果
Design | Type | ID
old table 2
old chair 3
new chair 4
new table 6
newest chair 7
如果哪一个无关紧要,您可以随时选择 maximum\minimum 个:
SELECT design,type,max(ID)
FROM YourTable
GROUP BY design,type
这不会是随机的,它总是会取 maximum\minimum 个,但这似乎并不重要。
试试这个:
select *
from (
select *, row_number() over (partition by Design,Type order by id desc) rowID
from @tab
) x
where rowID = 1
希望这对你有帮助:
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
我想获得每个设计和类型的不同记录,每个记录的随机 ID 无法使用
select distinct Design, Type, ID from table
它将return所有值 这是我的 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
可能的结果
Design | Type | ID
old table 2
old chair 3
new chair 4
new table 6
newest chair 7
如果哪一个无关紧要,您可以随时选择 maximum\minimum 个:
SELECT design,type,max(ID)
FROM YourTable
GROUP BY design,type
这不会是随机的,它总是会取 maximum\minimum 个,但这似乎并不重要。
试试这个:
select *
from (
select *, row_number() over (partition by Design,Type order by id desc) rowID
from @tab
) x
where rowID = 1
希望这对你有帮助:
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