Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
226 views
in Technique[技术] by (71.8m points)

sql - select distinct and autoincrement field in select query

I have a table Product with

ProductNo ProductDetail UniqueiD(Primarykey)
L1234      ProductA        1
L1234      ProductB        2
L1234      ProductC        3
M1234      ProductD        4
M1234      ProductE        5

So i need a select query that will display distinct product no with ids for displaying in p-listbox. say

Name  code
L1234  1
M1234  2

How do i achieve this? Thanks


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

One method is:

select distinct name, dense_rank() over (order by name)
from product;

That said, I would probably use group by:

select name, row_number() over (order by name) as code
from product
group by name;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...