473,503 Members | 1,657 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

T-SQL for the following?


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.
Feb 22 '07 #1
3 1166
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...@gmail.comwrote:
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.

Feb 22 '07 #2
Mark Probert wrote:
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
Feb 22 '07 #3

Hi, Ed..

You wrote:

|>
|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.
Feb 22 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
21699
by: Stephen Miller | last post by:
Is it possible to ALTER a temporary table in TSQL (SQL2000)? The following TSQL reports a syntax error at the ALTER TABLE line: DECLARE @Test TABLE( NOT NULL , NOT NULL ) ALTER TABLE...
2
5460
by: Steve | last post by:
Hi; I'm brand spanking new to sqlserver ( nice so far ). I need to make a simple data change across a list of tables. Basically replace an old date with a new date. However, the people I am...
2
11928
by: Steve | last post by:
Hi; I have been writing a lot of short tsql scripts to fix a lot of tiny database issues. I was wondering if a could make an array of strings in tsql that I could process in a loop, something...
18
4571
by: mountain man | last post by:
Greetings to all database professionals and laymen, Let us make a bold assumption that we have developed a software tool for the SQL Server environment which simply acts as an interface between...
2
18689
by: dynoweb | last post by:
I have several *.sql files with schema/data changes to be applied to our current database. Is there a way to create a TSQL script that could be run from the SQL Query Analyzer that would...
1
1720
by: TOM GUGGER | last post by:
OMNI GROUP tgugger@aimexec.com T-SQL/ CONTRACT TO PERM/ ATLANTA
0
921
by: kdilip41 | last post by:
Hi All, I would like to whether the following details is possible :: 1. 'How Many TSQL Statements Is Executed within a particular time frame say 8 hours and at the end of 8 hours i want a...
0
1330
by: BenCoo | last post by:
Hello everyone, I'm programming in VB.NET 2005 and I habe not much experience with database programming. I'm in search for some sql for the following : I have 3 database tables:...
0
1332
by: dharper | last post by:
Hi! I'm new to sql 2005 and need to run an automated report that is a fairly simple fixed width ragged right report. Problem is I need a header in the report that has static text, plus the rowcount...
1
1687
by: jabeek | last post by:
I had to write a valid TSQL query that does all the following in a transaction and will rollback if any system error is thrown. • Create the following tables Customer, Order and Address where...
0
7198
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
7319
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
5570
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,...
1
4998
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1498
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
373
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.