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 > LIKE problem

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-04-04, 02:22
actionant actionant is offline
Registered User
 
Join Date: Jan 2004
Location: South Africa
Posts: 178
Question LIKE problem

Is it possible to use LIKE in HAVING instead of WHERE?
Reply With Quote
  #2 (permalink)  
Old 02-04-04, 02:56
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,629
Oracle's SQL allows it ... For example,
Code:
select p.zone, count(*)
from customer p
group by p.zone
having p.zone like '2%';

    ZONE   COUNT(*)
---------- -------------
         2            5503
Reply With Quote
  #3 (permalink)  
Old 02-04-04, 03:20
actionant actionant is offline
Registered User
 
Join Date: Jan 2004
Location: South Africa
Posts: 178
Quote:
Originally posted by Littlefoot
Oracle's SQL allows it ... For example,
Code:
select p.zone, count(*)
from customer p
group by p.zone
having p.zone like '2%';

    ZONE   COUNT(*)
---------- -------------
         2            5503
thanks i actually got it to work using:
'*Health*'
instead of
'%Health%'
Reply With Quote
  #4 (permalink)  
Old 02-04-04, 05:18
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
What is the SQL statement you are using this with? Since HAVING is meant for use on aggregate functions it is unusual to find it being used with a character string.
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #5 (permalink)  
Old 02-04-04, 05:28
actionant actionant is offline
Registered User
 
Join Date: Jan 2004
Location: South Africa
Posts: 178
Quote:
Originally posted by andrewst
What is the SQL statement you are using this with? Since HAVING is meant for use on aggregate functions it is unusual to find it being used with a character string.
SELECT Count(MetricEval.AlternateIn) AS CountOfAlternateIn FROM (MetricEval INNER JOIN (Targets INNER JOIN Employee ON Targets.EmpName = Employee.EmpName) ON MetricEval.EmpName = Employee.EmpName) INNER JOIN Metrics ON (Targets.Measure = Metrics.Measure) AND (MetricEval.Measure = Metrics.Measure) HAVING ((MetricEval.Measure Like '*House*') AND (MetricEval.Month = #" & EndOfMonth & "#) AND (Employee.Supervisor = '" & supName & "') AND ((MetricEval.AlternateIn = 'G') OR (MetricEval.AlternateIn = 'E')));
Reply With Quote
  #6 (permalink)  
Old 02-04-04, 05:41
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
That could be modified to:

SELECT Count(MetricEval.AlternateIn) AS CountOfAlternateIn FROM (MetricEval INNER JOIN (Targets INNER JOIN Employee ON Targets.EmpName = Employee.EmpName) ON MetricEval.EmpName = Employee.EmpName) INNER JOIN Metrics ON (Targets.Measure = Metrics.Measure) AND (MetricEval.Measure = Metrics.Measure) WHERE ((MetricEval.Measure Like '*House*') AND (MetricEval.Month = #" & EndOfMonth & "#) AND (Employee.Supervisor = '" & supName & "') AND ((MetricEval.AlternateIn = 'G') OR (MetricEval.AlternateIn = 'E')));

This may not matter with your DBMS, but I know that in Oracle the WHERE clause can perform much better than the HAVING, because WHERE is evaluated on the rows before they are grouped and counted, HAVING afterwards.
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #7 (permalink)  
Old 02-04-04, 05:51
actionant actionant is offline
Registered User
 
Join Date: Jan 2004
Location: South Africa
Posts: 178
Quote:
Originally posted by andrewst
That could be modified to:

SELECT Count(MetricEval.AlternateIn) AS CountOfAlternateIn FROM (MetricEval INNER JOIN (Targets INNER JOIN Employee ON Targets.EmpName = Employee.EmpName) ON MetricEval.EmpName = Employee.EmpName) INNER JOIN Metrics ON (Targets.Measure = Metrics.Measure) AND (MetricEval.Measure = Metrics.Measure) WHERE ((MetricEval.Measure Like '*House*') AND (MetricEval.Month = #" & EndOfMonth & "#) AND (Employee.Supervisor = '" & supName & "') AND ((MetricEval.AlternateIn = 'G') OR (MetricEval.AlternateIn = 'E')));

This may not matter with your DBMS, but I know that in Oracle the WHERE clause can perform much better than the HAVING, because WHERE is evaluated on the rows before they are grouped and counted, HAVING afterwards.
thanks
i am using Access
Reply With Quote
  #8 (permalink)  
Old 02-04-04, 06:00
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Quote:
Originally posted by actionant
thanks
i am using Access
Actually I'm surprised it even works. Since HAVING applies to the data after it has been grouped, all those columns in your HAVING clause don't even exist at that point, since they weren't selected and used in a GROUP BY. Oracle rejects such a query altogether:

SQL> select count(*) from emp
2 having deptno=10;
having deptno=10
*
ERROR at line 2:

ORA-00979: not a GROUP BY expression

SQL> select count(*) from emp
2 where deptno=10;

COUNT(*)
----------
3
__________________
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