Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need help with nested SQL for a stored procedure

Posted on 2013-10-25
4
Medium Priority
?
209 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 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

721 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