dbTalk Databases Forums  

T-SQL for the following?

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss T-SQL for the following? in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Mark Probert
 
Posts: n/a

Default T-SQL for the following? - 02-21-2007 , 06:21 PM







Hi ..

I am geeting myself into many knots around how to do thsi extraction.

There is a table that contains the following:

aID bID cID dID
--------------------------
6 9 28 1
6 2 28 2
6 11 28 3
6 1 27 5
6 9 27 5
6 11 27 5

I am trying to get a result of:

aID bID cID dID
--------------------------
6 9 28 1
6 2 28 2
6 11 28 3
6 1 27 5

So, something like "the set off all values over bID which have the
greatest cID". Any ideas?

Many thanks, in advance,

--
-mark.

Reply With Quote
  #2  
Old   
KenJ
 
Posts: n/a

Default Re: T-SQL for the following? - 02-21-2007 , 09:33 PM






Hi Mark,

This query gets the result you were looking for. I don't know if it
works for cases that weren't represented by the numbers in your
example.

SELECT
aID
, min(bID) AS bID
, max(cID) AS cID
, dID
FROM
@tbl
GROUP BY
aID
, dID
ORDER BY
dID

Thanks,

Ken



On Feb 21, 6:21 pm, Mark Probert <probe... (AT) gmail (DOT) com> wrote:
Quote:
Hi ..

I am geeting myself into many knots around how to do thsi extraction.

There is a table that contains the following:

aID bID cID dID
--------------------------
6 9 28 1
6 2 28 2
6 11 28 3
6 1 27 5
6 9 27 5
6 11 27 5

I am trying to get a result of:

aID bID cID dID
--------------------------
6 9 28 1
6 2 28 2
6 11 28 3
6 1 27 5

So, something like "the set off all values over bID which have the
greatest cID". Any ideas?

Many thanks, in advance,

--
-mark.



Reply With Quote
  #3  
Old   
Ed Murphy
 
Posts: n/a

Default Re: T-SQL for the following? - 02-22-2007 , 11:01 AM



Mark Probert wrote:

Quote:
There is a table that contains the following:

aID bID cID dID
--------------------------
6 9 28 1
6 2 28 2
6 11 28 3
6 1 27 5
6 9 27 5
6 11 27 5

I am trying to get a result of:

aID bID cID dID
--------------------------
6 9 28 1
6 2 28 2
6 11 28 3
6 1 27 5

So, something like "the set off all values over bID which have the
greatest cID". Any ideas?
select t.aID, t.bID, t.cID, t.dID
from the_table t
join (
select bID, max(cID) max_cID
from the_table
group by bID
) s on t.bID = s.bID and t.cID = s.max_cID


Reply With Quote
  #4  
Old   
Mark Probert
 
Posts: n/a

Default Re: T-SQL for the following? - 02-22-2007 , 01:17 PM




Hi, Ed..

You wrote:

Quote:
select t.aID, t.bID, t.cID, t.dID
from the_table t
join (
select bID, max(cID) max_cID
from the_table
group by bID
) s on t.bID = s.bID and t.cID = s.max_cID
Many thanks.
I have some study to do on that "join" syntax ;-)
--

-mark.


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.