If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > New column in where clause

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-24-03, 14:06
kieranodwyer kieranodwyer is offline
Registered User
 
Join Date: Feb 2003
Posts: 21
New column in where clause

I have written the following query and it works perfectly....

SELECT d .ACCOUNT_CODE, p.LAST_NAME, p.FIRST_NAME, p.DEPARTMENT, COUNT(*) AS NoOfCalls, AVG(d .DURATION) AS AverageLength, MAX(d .DURATION) AS MaxCall, SUM(d .DURATION) AS TotalMins, SUM(CASE WHEN d .duration < 1 THEN 0.04 ELSE (0.04 + ((d .duration - 1) * 0.04)) END) AS Cost

FROM DETAIL d INNER JOIN PHONE_EMP_MAST p

ON d .ACCOUNT_CODE = p.ACCOUNT_CODE

GROUP BY d .ACCOUNT_CODE, p.LAST_NAME, p.FIRST_NAME, p.DEPARTMENT


I want to put in a clause so that it will show only the records where average length is greater than 5.... how can you refer to this column in the where clause?

When i put in a line saying where averagelength > '5', I get an error saying "Invalid column name"....
Reply With Quote
  #2 (permalink)  
Old 02-24-03, 14:17
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Re: New column in where clause

Quote:
Originally posted by kieranodwyer
I have written the following query and it works perfectly....

SELECT d .ACCOUNT_CODE, p.LAST_NAME, p.FIRST_NAME, p.DEPARTMENT, COUNT(*) AS NoOfCalls, AVG(d .DURATION) AS AverageLength, MAX(d .DURATION) AS MaxCall, SUM(d .DURATION) AS TotalMins, SUM(CASE WHEN d .duration < 1 THEN 0.04 ELSE (0.04 + ((d .duration - 1) * 0.04)) END) AS Cost

FROM DETAIL d INNER JOIN PHONE_EMP_MAST p

ON d .ACCOUNT_CODE = p.ACCOUNT_CODE

GROUP BY d .ACCOUNT_CODE, p.LAST_NAME, p.FIRST_NAME, p.DEPARTMENT


I want to put in a clause so that it will show only the records where average length is greater than 5.... how can you refer to this column in the where clause?

When i put in a line saying where averagelength > '5', I get an error saying "Invalid column name"....
You need to use "HAVING AVG(d.DURATION) > 5"

Also, no quotes wanted round the 5!
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #3 (permalink)  
Old 02-24-03, 14:35
kieranodwyer kieranodwyer is offline
Registered User
 
Join Date: Feb 2003
Posts: 21
I put that in:

SELECT d .ACCOUNT_CODE, p.LAST_NAME, p.FIRST_NAME, p.DEPARTMENT, COUNT(*) AS NoOfCalls, AVG(d .DURATION) AS AverageLength,
MAX(d .DURATION) AS MaxCall, SUM(d .DURATION) AS TotalMins, SUM(CASE WHEN d .duration < 1 THEN 0.04 ELSE (0.04 + ((d .duration - 1) * 0.04))
END) AS Cost

FROM DETAIL d INNER JOIN
PHONE_EMP_MAST p ON d .ACCOUNT_CODE = p.ACCOUNT_CODE

HAVING AVG(d .DURATION) > 5

GROUP BY d .ACCOUNT_CODE, p.LAST_NAME, p.FIRST_NAME, p.DEPARTMENT


but it still isn't working... it says there is an error near the keyword GROUP... (I'm using SQL server if that makes any difference...
Reply With Quote
  #4 (permalink)  
Old 02-24-03, 15:20
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
I don't know SQL Server, but I'd try moving the HAVING clause to AFTER the GROUP BY clause.
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #5 (permalink)  
Old 02-24-03, 16:00
kieranodwyer kieranodwyer is offline
Registered User
 
Join Date: Feb 2003
Posts: 21
Thanks Tony... putting it at the end of the statement made it work!
Reply With Quote
  #6 (permalink)  
Old 02-24-03, 17:32
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Quote:
Originally posted by kieranodwyer
Thanks Tony... putting it at the end of the statement made it work!
It makes sense - you have to GROUP the data before you can find the groups HAVING a certain property...
... having said that, Oracle actually doesn't mind which way round you do it.
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On