473,654 Members | 3,253 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 1173
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
21732
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 @Test DROP COLUMN Col2
2
5469
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 doing it for want a program that produces logging of what it does.
2
11940
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 like array arrayListOfTablesToProcess = { "orders", "phone",
18
4596
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 an end-user in an organization and the database, through the exclusive use of stored procedures which are authored by the organization or by software developers. All development work at the application software level may thereby be conducted...
2
18700
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 sequentially call the *.sql files? i.e. call schemaVersionCheck.sql call addFieldToLoanTable.sql call updateLoanTrigger.sql
1
1725
by: TOM GUGGER | last post by:
OMNI GROUP tgugger@aimexec.com T-SQL/ CONTRACT TO PERM/ ATLANTA
0
932
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 script to be generated of all that TSQL statements Like and then i want this TSQL scripts to be read by a window Service which is installed locally. 2. After the above step i want my Windows Service to send this script to the web service 3. I had...
0
1336
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: Movies, Theaters and a MovieProgram. Now I'm
0
1339
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 of the report. I am using an SSIS package to call the script. I think that's the way to go because I need it to run on Friday's at 3:00. Then I'll have a scheduled event to fire it off via sFTP from my server.(Although Im opent to ANY Suggestions) ...
1
1692
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 Customer can have many Orders and many Addresses. Order can only have one Address. Customer will have a column called CustomerNum of type NVARCHAR(3). • Develop a SQL function that provides a unique, incremented CustomerNum in the form “001”, “002”,...
0
8294
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8816
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8709
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8596
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7309
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1924
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1597
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.