Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to show all columns record on basis of single distinct column.

single column ID
Posted
Comments
kishore sharma 4-Sep-13 8:36am    
not clear,please make it clear
Corporal Agarn 4-Sep-13 11:54am    
Do you mean for a given value (ID) you wish to see related data?
Nelek 4-Sep-13 16:50pm    

1 solution

You're looking for a group by:
SQL
select *
from table
group by field1

Which can occasionally be written with a distinct on statement:
SQL
select distinct on field1 *
from table

On most platforms, however, neither of the above will work because the behavior on the other columns is unspecified. (The first works in MySQL, if that's what you're using.)

You could fetch the distinct fields and stick to picking a single arbitrary row each time.

On some platforms (e.g. PostgreSQL, Oracle, T-SQL) this can be done directly using window functions:
select *
from (
   select *,
          row_number() over (partition by field1) as row_number
   from table
   ) as rows
where row_number = 1
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900