Jump to content

SQL error: aggregate function alias cannot be used in the WHERE clause


Go to solution Solved by Mike Cantrell,

Recommended Posts

I have a condition defined in my SQL connector configuration that selects the min and max timestamps from my data.  The goal is to produce a condition that has a single capsule that spans my entire data set.  The SQL query looks something like this:

 

SELECT 
	MIN(WO.RUNCOMPLETE) AS FirstSampleTime, 
	MAX(WO.RUNCOMPLETE) AS LastSampleTime 
FROM 
	...Joined tables...
WHERE 
	...Some conditions... 	

The resulting condition in Seeq workbench reports an error when compiling this:
 

SELECT 
	MIN(WO.RUNCOMPLETE) AS FirstSampleTime, 
	MAX(WO.RUNCOMPLETE) AS LastSampleTime 
FROM 
	...Joined tables...
WHERE 
	LASTSAMPLETIME IS NOT NULL 
	AND FIRSTSAMPLETIME IS NOT NULL 
	...Some conditions... 	
ORDER BY FIRSTSAMPLETIME DESC 
LIMIT 1

It seems Seeq is inserting WHERE conditions on LastSampleTime and FirstSampleTime and this is causing an error.  

Question 1:  Why is Seeq inserting the WHERE conditions? 
Question 2: Is there a workaround for this?  The data I'm using tends to be looked at in its entirety.  So, having a condition with a single capsule that spans the time for all samples is very handy.  

Link to comment
Share on other sites

Thanks.  I solved this by putting my query inside a WITH like so: 
 

WITH Temp AS ( 
  SELECT 
      MIN(WO.RUNCOMPLETE) AS FirstSampleTime, 
      MAX(WO.RUNCOMPLETE) AS LastSampleTime 
  FROM 
      ...Joined tables...
  WHERE 
      ...Some conditions...
)
SELECT FirstSampleTime, LastSampleTime FROM Temp

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...