Solved

Need help with nested SQL for a stored procedure

Posted on 2013-10-25
4
206 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
[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
  • 2
4 Comments
 
LVL 66

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 41

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 66

Expert Comment

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

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

623 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