Solved

Need help with nested SQL for a stored procedure

Posted on 2013-10-25
4
201 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

832 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