473,388 Members | 1,499 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,388 software developers and data experts.

Returning the newest rows

I have a query set up that returns the data that I would like, but I
would only like the latest data for each vehicle number. The query I
have set up is

SELECT TOP 100 PERCENT dbo.vwEvents.EventName,
dbo.luSessionAll.SessionName, dbo.luOuting.OutingNumber,
dbo.luVehicle.VehicleName, dbo.luOuting.OutingID,
dbo.tblOutings.OutingStartTime,dbo.tblSessions.Ses sionDate,dbo.tblSessions.SessionStartTime
FROM dbo.vwSessions INNER JOIN dbo.vwEvents ON
dbo.vwSessions.Event = dbo.vwEvents.EventID
INNER JOIN
dbo.luSessionAll ON dbo.vwEvents.EventID =
dbo.luSessionAll.Event INNER JOIN
dbo.luOuting ON dbo.luSessionAll.SessionID =
dbo.luOuting.SessionID INNER JOIN
dbo.luVehicle ON dbo.luSessionAll.Vehicle =
dbo.luVehicle.VehicleID INNER JOIN
dbo.tblOutings ON dbo.luOuting.OutingID =
dbo.tblOutings.OutingID INNER JOIN
dbo.tblSessions ON dbo.tblOutings.[Session] =
dbo.tblSessions.SessionID
GROUP BY dbo.vwEvents.EventName, dbo.luSessionAll.SessionName,
dbo.luOuting.OutingNumber, dbo.luVehicle.VehicleName,
dbo.luOuting.OutingID, dbo.tblOutings.OutingStartTime,
dbo.tblSessions.SessionStartTime, dbo.tblSessions.SessionDate
ORDER BY dbo.luVehicle.VehicleName, dbo.tblSessions.SessionDate,
dbo.tblSessions.SessionStartTime, dbo.tblOutings.OutingStartTime

this returns all the outings. I would like the outing that has, in
order of importance, the latest session date, latest session time and
latest outing start time. Outing start time can sometimes be <<Null>>
but the other two always have values. How would I go about doing this?
thanks in advance for any help

Jun 30 '06 #1
2 1089
(pl*******@gmail.com) writes:
I have a query set up that returns the data that I would like, but I
would only like the latest data for each vehicle number. The query I
have set up is

SELECT TOP 100 PERCENT dbo.vwEvents.EventName,
....
ORDER BY dbo.luVehicle.VehicleName, dbo.tblSessions.SessionDate,
dbo.tblSessions.SessionStartTime, dbo.tblOutings.OutingStartTime


I don't have the time to look into the problem as such, but I feel
obliged to point out that the above looks dubious.

TOP 100 PERCENT does not make any sense at all, that is just white noice,
so I suggest that you remove.

At this point, I am not surprised if you say that this in fact a view
definition, and you need the TOP 100 for the ORDER BY to be permitted.
Well, it is still white noise. In SQL 2000 a SELECT from the view is
very like to return rows in the order set up by the ORDER BY clause, but
that is just mere chance. In SQL 2005 this is far likely and more than one
has been bitten by this.

The only way to get an ordered result from a query is to include an ORDER
BY clause in the query itself. You cannot use views to encapsulate order.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jun 30 '06 #2
(pl*******@gmail.com) writes:
I have a query set up that returns the data that I would like, but I
would only like the latest data for each vehicle number. The query I
have set up is
...
this returns all the outings. I would like the outing that has, in
order of importance, the latest session date, latest session time and
latest outing start time. Outing start time can sometimes be <<Null>>
but the other two always have values. How would I go about doing this?
thanks in advance for any help


Without know what is what in the tables, I will have to guess a bit.
Maybe this is what you are looking for. And if it's not, maybe it's
enough to get you going:

SELECT E.EventName, SA.SessionName, O.OutingNumber,
V.VehicleName, O.OutingID, Os.OutingStartTime,
S.SessionDate,S.SessionStartTime
FROM dbo.vwSessions Ss
JOIN dbo.vwEvents E ON Ss.Event = E.EventID
JOIN dbo.luSessionAll SA ON E.EventID = SA.Event
JOIN dbo.luOuting O ON SA.SessionID = O.SessionID
JOIN dbo.luVehicle V ON SA.Vehicle = V.VehicleID
JOIN dbo.tblOutings Os ON O.OutingID =
(SELECT TOP 1 Os.OutingID
FROM dbo.tblOutings Os1
JOIN dbo.tblSesseions S1 ON
Os.[Session] = S.SessionID
ORDER BY S1.SessionDate DESC,
S1.SessionSessionStartTime DESC,
Os1.OutingStartTime DESC)
JOIN dbo.tblSessions S ON Os.[Session] = S.SessionID
ORDER BY V.VehicleName, S.SessionDate, S.SessionStartTime,
Os.OutingStartTime

I replaced the table/view names with alias to make the queries easier
to read.

I also did away with the GROUP BY that did not seem to serve any
purporse. Maybe it's a DISTINCT you need. Then again, if you need a
DISTINCT this is an indication that the query is lacking a condition
somewhere.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jun 30 '06 #3

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

Similar topics

1
by: Ira | last post by:
I'm on a hosted Linux server and I have an Outer Join query that's returning on one less row than I expect. There are two rows very similar and I expect them both returned. There are similar...
9
by: Rowland Hills | last post by:
I have a table which is returning inconsistent results when I query it! In query analyzer: If I do "SELECT * FROM TABLE_NAME" I get no rows returned. If I do "SELECT COL1, COL2 FROM...
4
by: Daisy | last post by:
Let's say I've got a forum, where users can be moderators of each forum. Tables look like this: USER -------- user_key name FORUM
0
by: Ryan J. Allen | last post by:
Forgive me if this is the wrong group to post to. I have a table and the relevant bits look like this: CREATE TABLE "records" ( "id" SERIAL, "number" integer NOT NULL, "timestamp" timestamp...
0
by: Maart_newbie | last post by:
Hi all, I've got a question about returning the value of a pk-column to a DataTable after inserting a row (via a data-adapter) using MySql5. Here is the SQL and code concerned: ...
13
by: Karl Groves | last post by:
I'm missing something very obvious, but it is getting late and I've stared at it too long. TIA for responses I am writing a basic function (listed at the bottom of this post) that returns...
0
by: Alex | last post by:
Hi, I am a complete beginner with Oracle 9i and XML within it, not XML alone though. I am trying to do a regular SELECT statement using the SQL Scratchpad within the Oracle Enterprise Manager and...
3
by: codefragment | last post by:
Hi I have a chunky bit of sql that I will want to call from a number of places. It will return a few thousand rows. Whats the best way of structuring this? 1) I initially thought of using...
1
by: mbewers1 | last post by:
Hi there, I'm having a strange problem whereby I can execute a select query in Oracle SQL Developer that rerturns some rows but, when this same query is placed into C#, I get no results. I've...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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...

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.