473,387 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

SQL to retrieve min value of the combination

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
3 10875
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: forums_mp | last post by:
I've got an STL class (see below) with two functions to store and retrieve data - msg structs. The "Store" function when called will copy the received message (depending on which message) into...
1
by: Eugfene | last post by:
I have the following function in a html file: function selectEdit(fileID, processCode, processID, fileName,fileDesc) { document.forms.recordID.value = fileID; document.forms.processor.value =...
5
by: Roy Gourgi | last post by:
Hi, I am used to working in Visual FoxPro and I would like to be able to create a database and store and retrieve information from it. What is the simplest way to do it and what should I be...
4
by: marco.nl | last post by:
it fails to retrieve newly intruduced data by the user. i tried this ... AfxGetApp()->WriteProfileString("Settings", "email", EDIT1); UpdateData(FALSE); and this.....
4
by: jay | last post by:
I am using the dataset object to add a row to a sql server database in vb.net code, as follows: dim drow as DataRow dim cmdBld as new SqlCommandBuilder(mySqlDataAdapter) ds.tables(0).NewRow()...
2
by: Frank | last post by:
Can I do this? I add a session var in C# and ultimatly want to pass it into a vbscript client side activeX control. This is what I have so far but get " Object Required:'name2' " error. Can...
4
by: kang jia | last post by:
hi i am doing mailinglist currently. the code in my first page is like this : : <html> <head> <link rel="stylesheet" type="text/css" href="gallery.css" /> <script language="JavaScript"> ...
21
by: giandeo | last post by:
Hello Experts. Is it possible to retrieve the value from a populated pull down menu from a database and then use that value to access the same database to get the related fields. Example: ...
2
by: Cramer | last post by:
Using ASP.NET 3.5... As far as I know, any time we store a value in application or session state, it is stored as a humble 'object' type rather than it's "real" type. For example, if we want...
1
by: divya | last post by:
Hello, I used the below code to move selected items between two listboxes Left and Right:- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestPopup.aspx.cs" Inherits="TestPopup" %> ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.