Solved

SQL Syntax for selecting a group of 'last' records

Posted on 2015-01-20
15
123 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
  • 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
 
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 48

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 48

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 48

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 48

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 48

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now