Christian Graf
Christian Graf

Reputation: 406

Retrieving a single column in a subquery having a "having" condition

Well, I've got a subquery that contains an "having" condition. So I have to add the parameters in the having condition in the select clause, right?

So I've got the subquery:

SELECT sfd.id, sfd.StartTime, sfd.EndTime, sd.StartTime 
FROM ssp.SessionFloatData sfd, ssp.SessionData_daily sd 
WHERE Name = 'nsrserverhost' AND Parameter = 'storagenode' 
AND sd.id = sfd.id 
HAVING (sfd.StartTime <= sd.StartTime AND sfd.EndTime >= sd.StartTime)

but in fact I'm only interested in the the first column: I just want to retrieve the id.

The excerpt of my whole statement is

WHERE sfd.id IN (SELECT sfd.id, sfd.StartTime, sfd.EndTime, sd.StartTime 
...
                HAVING (sfd.StartTime <= sd.StartTime AND sfd.EndTime >= sd.StartTime))

What obiously fails as four columns are returned :( What can I do?

Update: Selecting just sfd.id results in an error: ERROR 1054 (42S22): Unknown column 'sfd.StartTime' in 'having clause' Changing the 'having'

Upvotes: 0

Views: 34

Answers (1)

ericpap
ericpap

Reputation: 2937

I don't understand what you try to do. The HAVING clause is only used when in conjuction with GROUP BY clause. In your case I suposse you could do this:

SELECT sfd.id
FROM ssp.SessionFloatData sfd, ssp.SessionData_daily sd 
WHERE Name = 'nsrserverhost' AND Parameter = 'storagenode' 
AND sfd.StartTime <= sd.StartTime AND sfd.EndTime >= sd.StartTime
AND sd.id = sfd.id 
GROUP BY sfd.id

So what was on HAVING is passed to WHERE clause. Is this what you want?

Upvotes: 1

Related Questions