English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Hi Friends,

I have the below problem in writing an sql query.

Assume we have a table with two columns.the pattern being

col1 col2

X 1
X 2
Y 3
Y 4
Z 5
Z 6
This is the pattern there are n no of rows having same value in col1 and different value in col2.I need to write a query which feach me the rows having least value in col2(comparision between the two records)

The required solution is

col3 col4
X 1
Y 3
Z 5

2007-12-06 00:59:08 · 3 answers · asked by Gogo 1 in Computers & Internet Programming & Design

3 answers

hi ok here is what you need

select col1, min(col2) from table
group by col1

this will result in displaying only those entries from col1 that have the minimum cal2 value.

Note: you would want to make the col2 an integer data type. I assume you know that. Also if you want it be sorted by col1 use the "Order by" clause.

2007-12-06 02:04:21 · answer #1 · answered by warlock_handler 2 · 0 0

Hi,
Suppose ur table name is tab1 then write the following SQL query

Select col1, min(col2) from tab1 group by col1

2007-12-10 00:15:14 · answer #2 · answered by iqbal 4 · 0 0

select col1,min(col2) from table_name group by col1

2007-12-06 09:53:18 · answer #3 · answered by natesh p 1 · 0 0

fedest.com, questions and answers