Solved

SQL Syntax for selecting a group of 'last' records

Posted on 2015-01-20
15
141 Views
Last Modified: 2015-02-01
Ok.
I have a table, tblPieces that has a PieceID and many other detail about each chunk of iron (eventually 10K or so chunks)
I have tblFlowLineReadings, that has PieceID, and JobID in it as foreign keys, along with data about a particular reading
(eventually millions of readings)
I have a form that let's the users choose a subset of PieceIDs to put on a report.
Those choices get knocked into a table, tempOnReport.

Now, for each PieceID in tempOnReport, I want to retrieve the latest row in tblFlowLineReadings for that piece.
Getting ALL the rows in tblFlowLineReadings would be simple enough
Select * from tblFlowLineReadings where PieceID in (Select PieceID from tempOnReport)

But getting ONLY the latest row for each PieceID, that's a bit tricky...
AND
Eventually I'll need to make this same result set come out for an ASP.Net page, so anything Access specific, or SQL Server naughty, won't cut the mustard.

The definition of 'latest' can be the largest JobID in tblFlowLineReadings for each PieceID in tempOnReport.

Thanks,
Nick67
0
Comment
Question by:Nick67
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 2
  • +1
15 Comments
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 40560807
This may seem "SQL naughty", but it's really just a T-SQL statement, so your ASP.Net should be happy with it.

Select * from tblFlowLineReadings T Inner Join
(Select PieceID, Max(JobID) as MaxJobID from tblFlowLineReadings group by PieceID) M
on T.PieceID = M.PieceID and T.JobID = M.MaxJobID

Open in new window

0
 
LVL 32

Assisted Solution

by:Daniel Wilson
Daniel Wilson earned 100 total points
ID: 40560810
Oops, looks like I misread your spec.  Trying again:

Select * from tblFlowLineReadings T Inner Join
(Select PieceID, Max(JobID) as MaxJobID from tempOnReport group by PieceID) M
on T.PieceID = M.PieceID and T.JobID = M.MaxJobID

Open in new window

0
 
LVL 11

Assisted Solution

by:John_Vidmar
John_Vidmar earned 100 total points
ID: 40560832
SELECT	*
FROM	tblPieces		x
JOIN	tblFlowLineReadings	y	ON	x.PieceID = y.PieceID
JOIN	(	SELECT	b.PieceID
		,	JobID = MAX(b.JobID)
		FROM	tempOnReport		a
		JOIN	tblFlowLineReadings	b	ON	a.PieceID = b.PieceID
		GROUP
		BY	b.PieceID
	)	z	ON	y.PieceID = z.PieceID
			AND	y.JobID = z.JobID

Open in new window

0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 26

Author Comment

by:Nick67
ID: 40561027
Ah,
It was the max and group by that I needed to recall -- because DMax() is not going to fly and is a performance pig.
But, the max, group by, and two IN clauses should do it.  And it makes sense to me looking at it in the query designer: a criteria for PieceID and a criteria for JobID

SELECT *
FROM tblFlowLineReadings
WHERE JobID IN
    (SELECT MAX(JobID) as LastJob
    FROM tblFlowLineReadings
    GROUP BY  tblFlowLineReadings.PieceID)
And PieceID IN
    (SELECT PieceID
    FROM tempOnReport)

but, I take it that JOINs are more efficient than INs, or at this point (SQL Server 2008 R2) the query optimizer will sort the two things out to be the same in the end?
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40561068
>>" I take it that JOINs are more efficient than INs"

Whilst any generalizations are dangerous, I would say YES. Use joins in preference to INs.
0
 
LVL 49

Assisted Solution

by:PortletPaul
PortletPaul earned 300 total points
ID: 40561091
more on joins -v- INs
>>"Getting ALL the rows in tblFlowLineReadings would be simple enough
Select * from tblFlowLineReadings where PieceID in (Select PieceID from tempOnReport)"


I would suggest instead:
Select FLR.* from tempOnReport R inner join tblFlowLineReadings FLR on R.PieceID = FLR.PieceID

Assumes you have index on FLR.PieceID and possibly R.PieceID also

--------
Regarding getting the "latest" record an alternative to MAX() and GROUP BY is to use ROW_NUMBER()
like this

select * from (

Select FLR.*
, ROW_NUMBER() OVER(PARTITION BY PieceID ORDER BY JobID DESC) AS RN
from tempOnReport R
inner join tblFlowLineReadings FLR on R.PieceID = FLR.PieceID
) as derived
where RN = 1
0
 
LVL 26

Author Comment

by:Nick67
ID: 40561119
I'll have to leave it for this evening, but I keep getting a Cartesian result (35 rows at present in tempOnReport -- query returns 1225 rows 35*35)
ID: 40561027 gives the right result, but how do I twist that around into JOINS and not get a Cartesian result?

Assumes you have index on FLR.PieceID and possibly R.PieceID also
Foreign keys and primary keys :)  Good assumption!
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40561311
>>"ID: 40561027 gives the right result, but how do I twist that around into JOINS and not get a Cartesian result?"

I didn't see that id, where was it?  (hint: I'm  being sarcastic)

On this side of the browser we work without the ability to see your data; this is why we frequently ask for "sample data" and "expected results"; because then we can build and unit test a solution.
0
 
LVL 26

Author Comment

by:Nick67
ID: 40562189
@PortletPaul
ROLFLMAO
http://blog.experts-exchange.com/ee-blog/2014-mve-winners/
You aren't the only one on that list! :D  Congratulations! by the way.

I am far more often on the other side of the browser, too
My New Year's Resolution was to actually ask some questions.

At the moment, this is all being built, so there's either a paucity of data (tblFlowlineReadings only has 35 rows in it and one JobID) or a surfeit (jobID has 85K) and the data is in SQL Server.

When I try this in Access
SELECT FLR.*, *
FROM [Select Max(jobID) as maxJob from tblFlowLineReadings Group by PieceID]. AS M
INNER JOIN ([Select PieceId from tempOnReport]. AS R
INNER JOIN [Select * from tblFlowLineReadings]. AS FLR
ON R.PieceId = FLR.PieceID)
ON M.maxJob = FLR.JobID;

I get a Cartesian result, so clearly I am doing something wrong -- but I can't see it.

Here's a modified data set spit out in an .mdb
There are two tables and two queries.
The one query, with the IN clause, represents the desired result.
The query with 2 JOINS gives the unwanted Cartesian result
INtoJOIN.mdb
0
 
LVL 26

Accepted Solution

by:
Nick67 earned 0 total points
ID: 40562375
Damn.
Looks like I needed PieceID in the 'Max' table and an additional JOIN back to the tempOnReport on PieceID.
This works.
I'll need to check that it works on a larger set of data

SELECT FLR.*, *
FROM [SELECT PieceID,Max(jobID) AS maxJob FROM tblFlowLineReadings GROUP BY PieceID ]. AS M
INNER JOIN ([SELECT PieceId FROM tempOnReport]. AS R
INNER JOIN [SELECT * FROM tblFlowLineReadings ]. AS FLR
ON R.PieceId = FLR.PieceID)
ON (M.PieceID = R.PieceId) AND (M.maxJob = FLR.JobID);
0
 
LVL 49

Assisted Solution

by:PortletPaul
PortletPaul earned 300 total points
ID: 40563464
*sheepishy*

oh! I didn't recognize you (probably because you don't ask questions). apologies.
you will definitely know what this side of the browser looks like.

---

it's not necessary to use subqueries for aliases R or FLR and I know Access loves parentheses more than I do, but I see where you are going. Looks about right, my variant (may need more parentheses).

SELECT
      FLR.*
    , *
FROM [SELECT PieceID,Max(jobID) AS maxJob FROM tblFlowLineReadings GROUP BY PieceID] AS M
      INNER JOIN tempOnReport AS R
      INNER JOIN tblFlowLineReadings AS FLR ON R.PieceId = FLR.PieceID ON M.PieceID = R.PieceId
      AND M.maxJob = FLR.JobI

Open in new window

0
 
LVL 26

Author Comment

by:Nick67
ID: 40563492
Access loves parentheses alright.
And it loves [ ]
And it loves ;
when you are aliasing
:(
To the point where it would just hose-bag the saved query every time I used it.
So, I wound up saving SELECT PieceID,Max(jobID) AS maxJob FROM tblFlowLineReadings GROUP BY PieceID
as qryMaxJob and throwing qryMaxJob  into the query editor mix as a table and then not aliasing anything after that.
Then it did what I wanted and remained stable.

I had forgotten how bitchy the Query Editor is when you start with table aliases :(
The one flaw in an otherwise superlative bit of UI.

SSMS will be a lot more civilized when it comes time (in the next day or two) to create the sproc for the ASP.Net page.
It's an interesting project.
I've got an Access front-end, and SQL Server backend and an ASP.Net 'picture window' for the clients to purely view the data.  Knocking it all into shape on the fly will definitely tax the grey matter.
I'll definitely have to remember to put the SQL query syntax as the primary zone.
I built my first UDF on Monday, but the suggestions I received was to use a cursor.
Hence the 'SQL Server naughty'

*sheepishy*
No Biggie.
I've got a very good memory for the printed word.
And if you've played in the MS Access TA in any substantive way -- and you have -- I'll have seen your name and poked at your bio.


Thanks,
Nick67
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40563502
I only poke tentatively in the Access topic, frequently tripping over those parentheses and other niggly bits that are unique to it.

I did use Access way back. In fact I bought the very first version that didn't even allow joins (what a piece of nonsense that one was) but it improved a lot very quickly thankfully.
0
 
LVL 26

Author Comment

by:Nick67
ID: 40573298
80K points is 'poking tentatively?' :)
Thanks for the help!
0
 
LVL 26

Author Closing Comment

by:Nick67
ID: 40582259
Thanks to all for the help.
You all pointed me in the right direction with the Max(), and I got that bit sorted.
You helped me convert the INs to JOINs and I got syntax worked out that will play in both Access and SQL Server.

Thanks again!
Nick67
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question