Solved

Need help with nested SQL for a stored procedure

Posted on 2013-10-25
4
202 Views
Last Modified: 2013-10-25
I have 2 tables I need to include in this query.

1. Batch (each record is a batch)
2. Session (could have multiple sessions per batch)

The problem I'm having is that I need to only return batches that the most recent 'EndTime' in the session table for the batch is older than 10 minutes.

Here's how I tried to do it, but I'm getting this error:
An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.

    
    SELECT  b.* ,
    FROM    dbo.batchtable b,
	dbo.sessiontable st
    WHERE   b.BatchID = st.BatchID
	AND MAX(st.EndTime) < DATEADD(mi,-10,GETDATE())      
                 AND b.JobName = 'JOB1' --
    ORDER BY b.Priority , b.BatchID

Open in new window


I'm guessing this will be a simple solution for the experts. I don't work in SQL enough to remember all the syntax and how things need to be grouped.

Any help is appreciated.
0
Comment
Question by:jasonkrueger
  • 2
4 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 39600988
For starters
Change the reference to sessiontable to a subquery that does the MAX.
Any time you want to filter based on an aggregate like MAX, that's done in HAVING and not WHERE
SELECT b.* 
FROM  dbo.batchtable b
	JOIN (
		SELECT id, MAX(EndTime)
		FROM dbo.sessiontable 
		GROUP BY id
		HAVING MAX(EndTime) < DATEADD(mi,-10,GETDATE()) ) st ON b.BatchID = st.BatchID
WHERE b.JobName = 'JOB1'
ORDER BY b.Priority, b.BatchID

Open in new window

0
 
LVL 40

Expert Comment

by:Sharath
ID: 39601003
try this.
    SELECT  *
    FROM    dbo.batchtable b,
	dbo.sessiontable st
    WHERE   b.BatchID = st.BatchID
	AND st.EndTime = (SELECT MAX(st1.EndTime) FROM sessiontable st1 WHERE st1.EndTime > DATEADD(mi,-10,GETDATE())
	AND st.BatchID = st1.BatchID)
	AND b.JobName = 'JOB1' --
    ORDER BY b.Priority , b.BatchID  

Open in new window

0
 

Author Closing Comment

by:jasonkrueger
ID: 39601374
Thanks Jim! worked like a charm
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39601387
Thanks for the grade.  Good luck with your project.  -Jim
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

808 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