By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,197 Members | 977 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,197 IT Pros & Developers. It's quick & easy.

SQL to retrieve min value of the combination

P: n/a
Hi,

I have the following table

1001 2 3
1001 3 5
1001 5 7
1002 5 4
1002 6 2

I need to get a result

1001 2 3
1002 6 2

How is it possible ?
Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
question's a little vague, but assuming you are trying to get records for
the minimum values of the first and third column, for all rows in a table,
you could try a subquery like

select *
from x
where (col1, col3) in (
select col1, min(col3)
from x
group by col1
)

but don't try this until you understand why it works ;-)

-- mcs
"Ravindra Harve" <rh****@hotmail.com> wrote in message
news:83**************************@posting.google.c om...
| Hi,
|
| I have the following table
|
| 1001 2 3
| 1001 3 5
| 1001 5 7
| 1002 5 4
| 1002 6 2
|
| I need to get a result
|
| 1001 2 3
| 1002 6 2
|
| How is it possible ?
Jul 19 '05 #2

P: n/a
Use this one , the same but easier
select *
from (
select col1, min(col3)
from x
group by col1
)

"Mark C. Stock" <mcstockX@Xenquery .com> wrote in message
news:D_********************@comcast.com...
question's a little vague, but assuming you are trying to get records for
the minimum values of the first and third column, for all rows in a table,
you could try a subquery like

select *
from x
where (col1, col3) in (
select col1, min(col3)
from x
group by col1
)

but don't try this until you understand why it works ;-)

-- mcs
"Ravindra Harve" <rh****@hotmail.com> wrote in message
news:83**************************@posting.google.c om...
| Hi,
|
| I have the following table
|
| 1001 2 3
| 1001 3 5
| 1001 5 7
| 1002 5 4
| 1002 6 2
|
| I need to get a result
|
| 1001 2 3
| 1002 6 2
|
| How is it possible ?

Jul 19 '05 #3

P: n/a
Julia,

Not the same by any means.

Your select * does absolutely nothing -- the subquery results are simply
passed thru unchanged

Your subquery contains only two columns -- the assumption is that all
columns should be displayed

Try them both on the EMP table with the empno, deptno, and sal in the
resultset and dept and sal in the subquery ("Write a report that lists the
lowest paid employees in each department")

-- mcs

"Julia Sats" <ju********@sympatico.ca> wrote in message
news:iK*********************@news20.bellglobal.com ...
| Use this one , the same but easier
| select *
| from (
| select col1, min(col3)
| from x
| group by col1
| )
|
| "Mark C. Stock" <mcstockX@Xenquery .com> wrote in message
| news:D_********************@comcast.com...
| > question's a little vague, but assuming you are trying to get records
for
| > the minimum values of the first and third column, for all rows in a
table,
| > you could try a subquery like
| >
| > select *
| > from x
| > where (col1, col3) in (
| > select col1, min(col3)
| > from x
| > group by col1
| > )
| >
| > but don't try this until you understand why it works ;-)
| >
| > -- mcs
| >
| >
| > "Ravindra Harve" <rh****@hotmail.com> wrote in message
| > news:83**************************@posting.google.c om...
| > | Hi,
| > |
| > | I have the following table
| > |
| > | 1001 2 3
| > | 1001 3 5
| > | 1001 5 7
| > | 1002 5 4
| > | 1002 6 2
| > |
| > | I need to get a result
| > |
| > | 1001 2 3
| > | 1002 6 2
| > |
| > | How is it possible ?
| >
| >
|
|
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.