473,406 Members | 2,956 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,406 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 10876
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.