473,546 Members | 2,205 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help with complex(?) querie

Hi

I'm having some problem getting the following result.

I have 2 tables

Drivers
--------
DriverID int (PK)
DriverName varchar(50)

Runs
-------
RunID int (PK)
DriverID int (FK)
Speed float
ET float

Example data:

Drivers:
DriverID DriverName
-------------------
1 Nisse
2 Ken
3 Dan

Runs:
RunID DriverID Speed ET
-----------------------------
1 3 143.2 5.99
2 2 33.22 10.39
3 3 139.3 6.34
4 1 213.1 5.23
5 1 211.2 5.32
What i wan't is to get every drivers best ET sorted like this

DriverID DriverName RunID Speed ET
-------------------------------------
1 Nisse 4 213.1 5.23
3 Dan 1 143.2 5.99
2 Ken 2 33.22 10.39
Hope this is possible.

Thanks
Patric

Jul 23 '05 #1
3 1236
On 30 Mar 2005 11:38:12 -0800, Patric wrote:
Hi

I'm having some problem getting the following result.

I have 2 tables

Drivers
--------
DriverID int (PK)
DriverName varchar(50)

Runs
-------
RunID int (PK)
DriverID int (FK)
Speed float
ET float

Example data:

Drivers:
DriverID DriverName
-------------------
1 Nisse
2 Ken
3 Dan

Runs:
RunID DriverID Speed ET
-----------------------------
1 3 143.2 5.99
2 2 33.22 10.39
3 3 139.3 6.34
4 1 213.1 5.23
5 1 211.2 5.32
What i wan't is to get every drivers best ET sorted like this

DriverID DriverName RunID Speed ET
-------------------------------------
1 Nisse 4 213.1 5.23
3 Dan 1 143.2 5.99
2 Ken 2 33.22 10.39
Hope this is possible.

Thanks
Patric


Hi Patric,

Try if this works:

SELECT d.DriverID, d.DriverName, r.RunID, r.Speed
FROM Runs AS r
INNER JOIN Drivers AS d
ON d.DriverID = r.DriverID
WHERE NOT EXISTS
(SELECT *
FROM Runs AS r2
WHERE r2.DriverID = r.DriverID
AND r2.Speed < r.Speed)

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #2
Patric (pa****@webst8. com) writes:
Drivers
--------
DriverID int (PK)
DriverName varchar(50)

Runs
-------
RunID int (PK)
DriverID int (FK)
Speed float
ET float

Example data:

Drivers:
DriverID DriverName
-------------------
1 Nisse
2 Ken
3 Dan

Runs:
RunID DriverID Speed ET
-----------------------------
1 3 143.2 5.99
2 2 33.22 10.39
3 3 139.3 6.34
4 1 213.1 5.23
5 1 211.2 5.32
What i wan't is to get every drivers best ET sorted like this

DriverID DriverName RunID Speed ET
-------------------------------------
1 Nisse 4 213.1 5.23
3 Dan 1 143.2 5.99
2 Ken 2 33.22 10.39


SELECT r.DriverID, d.DriverNme, r.RunID, r.Speed, r.ET
FROM Runs r
JOIN Drivers d ON r.DriverID = d.DriverID
JOIN (SELECT DriverID, maxspeed = MAX(Speed)
FROM Runs
GROUP BY DriverID) AS m ON m.DriverID = r.DriverID
AND m.maxspeed = r.Speed

Notes:

1) The query in parentheses is a *derived table*. It is sort of a
temp table within the query, but the table is not necessarily
materialized, and the optimizer may recast computation order
as long the result is not affected. This is a very powerful
concept.

2) If there are two runs with the same max speed for the same driver,
both runs will be displayed. You did not specify any rules to
break ties.

3) Had you provided table definitions and sample data with CREATE TABLE
statements and INSERT statements, the query above would have been
tested. Now the query is untested.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #3
Thanks for your help. I think it solved my problem. I'll try to make a
better description with create teble and inserts next time.

/ Patric

Jul 23 '05 #4

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

Similar topics

0
1007
by: Rob Yelvington | last post by:
I need some help with a query. I have two tables in one data base that both have a SSN field. The ssn field in one table contains slashes and the ssn field in the other does not. I know how to use CONCAT() and RIGHT(),MID(), and LEFT() on one of the ssn fields to obtain a result with digits only. But, I need to obtain a result set that...
1
1390
by: shunah | last post by:
hello. In an app I'm building, there are two different kinds of memos, family memos and individual memos. Right now I have a single form that I using in four slightly different ways: 1. add a family memo, 2. edit a family memo, 3. add an individual memo and 4. edit an individual memo. I'm having trouble doing all of this even...
0
1000
by: Betsy | last post by:
I have a Worrd document that is merged to a query in access (both 2003). If I put a parameter into the querie that requires user input such as the document won't open because it says all the fields are empty rather than displaying an input box asking for the last name. I've done this a hundred times ever since access 97. What's the...
1
1135
by: DS | last post by:
Any reason why a Query would stop working? If I run the Query without the Criteria froma form it works. When I run it from a form with the Criteria it doesn't work. Any know why? Thanks. DS
20
3038
by: Stewart Graefner | last post by:
Here is a chunk of code that works for an individual record. It evaluates dates and checks or unchecks boxes as it goes along. It may not be pretty but it works. What my problem is that I need it to evaluate all the records(200+) in my db and change those which need changing. Having to do it individually would defeat the purpose of developing...
3
1563
by: avandenbroeck | last post by:
The following code does not select the appropiate fields, it select everything with in a zip code ( as an example)and so on. I have tried to modify but I am a rookie and everything I tried has failed. Help please $query = "Select xxx, xxx, xxx, xxx, xxx, xxx, xxx From `list_data` where (keyword like '$keyword%' and zip='$zip' or key2...
48
4203
by: Jimmy | last post by:
thanks to everyone that helped, unfortunately the code samples people gave me don't work. here is what i have so far: <% Dim oConn, oRS, randNum Randomize() randNum = (CInt(1000 * Rnd) + 1) * -1 Set oConn=Server.CreateObject("ADODB.Connection") Set oRS=Server.CreateObject("ADODB.recordset") oConn.Provider="Microsoft.Jet.OLEDB.4.0"...
1
1304
by: diatel1 | last post by:
I have a database with tables and forms. One of those table is the Inventory table, where I have fields like "product" "stock #" "buy date" "paid date" "paid yes/no", etc. I would like to have a form or a querie where I can ask for example:"Inventory paid from 00/00/0000 to 00/00/0000"; "Inventory owe from 00/00/0000 to 00/00/0000". In the same...
4
1142
by: dhpacheco | last post by:
Hello I’m doing an application using VS 2005, c# for management a DB, but I new using the language, neither have experience in programming. Please could someone tell me if this is correct? I include a datase in my project and create variables tableadapter for each table of the dataset. Now I need operate the DB, using insertions is easy ‘cause...
0
7507
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7947
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...
1
7461
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6030
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...
0
5080
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3492
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...
0
3472
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1922
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 we have to send another system
1
1046
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.