Home » SQL & PL/SQL » SQL & PL/SQL » Filter by Count of particular field
Filter by Count of particular field [message #615359] |
Mon, 02 June 2014 16:45 |
|
rlsublime
Messages: 10 Registered: March 2014
|
Junior Member |
|
|
I have the following query that I need to filter with a count of >2 for a particular field. I am trying to fix the "having count(SUPERVISOR_NAME)>2" as the current query generates an error.
Select ID, first_last, SITENAME,UTILIZATION,REPORTED,OVERALL,monthYear,SUPERVISOR_NAME
from MONTHLY_SUM
where monthYear
in ('OCT-13','NOV-13','DEC-13','JAN-14','FEB-14','MAR-14','APR-14','MAY-14') and SUPERVISOR_NAME is not null
having count(SUPERVISOR_NAME) > 2;
Thanks
|
|
|
|
|
|
Re: Filter by Count of particular field [message #615363 is a reply to message #615362] |
Mon, 02 June 2014 17:05 |
|
rlsublime
Messages: 10 Registered: March 2014
|
Junior Member |
|
|
Select ID, first_last, SITENAME,UTILIZATION,REPORTED,OVERALL,monthYear,SUPERVISOR_NAME
from MONTHLY_SUM
where monthYear
in ('OCT-13','NOV-13','DEC-13','JAN-14','FEB-14','MAR-14','APR-14','MAY-14') and SUPERVISOR_NAME is not null
having count(SUPERVISOR_NAME) > 2
group by ID, first_last, SITENAME,UTILIZATION,REPORTED,OVERALL,monthYear,SUPERVISOR_NAME
|
|
|
|
Re: Filter by Count of particular field [message #615365 is a reply to message #615364] |
Mon, 02 June 2014 17:38 |
|
rlsublime
Messages: 10 Registered: March 2014
|
Junior Member |
|
|
I tried the following code and got a "not a group by expression" error
Select ID, first_last, SITENAME,UTILIZATION,REPORTED,OVERALL,monthYear,SUPERVISOR_NAME
from MONTHLY_SUM
where monthYear
in ('OCT-13','NOV-13','DEC-13','JAN-14','FEB-14','MAR-14','APR-14','MAY-14') and SUPERVISOR_NAME is not null
Group by ID, first_last,SITENAME,UTILIZATION,REPORTED,OVERALL,monthYear,SUPERVISOR_NAME
having count(SUPERVISOR_NAME) > 2;
|
|
|
|
Re: Filter by Count of particular field [message #615368 is a reply to message #615366] |
Mon, 02 June 2014 17:53 |
John Watson
Messages: 8931 Registered: January 2010 Location: Global Village
|
Senior Member |
|
|
Sorry, I misread your post. You cannot apply HAVING to a column on which you are grouping. Logically impossible. Look at this:12c orclz>
12c orclz> select deptno,count(ename) from emp group by deptno having count(ename) > 4;
DEPTNO COUNT(ENAME)
---------- ------------
30 6
20 5
12c orclz>
|
|
|
Re: Filter by Count of particular field [message #615369 is a reply to message #615365] |
Mon, 02 June 2014 21:01 |
|
rlsublime
Messages: 10 Registered: March 2014
|
Junior Member |
|
|
I am trying to combine the following two queries. The first filters by supervisor name and the second pulls in the remaining data. I am not sure how to combine the two to only display the filtered results.
Select SUPERVISOR_FIRST_LAST
from PROD_SUM
where monthYear in ('OCT-13','NOV-13','DEC-13','JAN-14','FEB-14','MAR-14','APR-14','MAY-14')
and SUPERVISOR_FIRST_LAST is not null
group by SUPERVISOR_SID,SUPERVISOR_FIRST_LAST
having count(SUPERVISOR_FIRST_LAST)>3
Select id, first_last, SITENAME,UTILIZATION,REPORTED,OVERALL, monthYear
from PROD_SUM
where monthYear
in ('OCT-13','NOV-13','DEC-13','JAN-14','FEB-14','MAR-14','APR-14','MAY-14')
|
|
|
|
|
|
|
|
Re: Filter by Count of particular field [message #615378 is a reply to message #615375] |
Tue, 03 June 2014 00:26 |
oralover2006
Messages: 144 Registered: January 2010 Location: India
|
Senior Member |
|
|
BlackSwan wrote on Tue, 03 June 2014 10:32>
does TRAIN mean to simply write your query for you?
If you can't learn by example, perhaps you are unqualified to be trained.
1) no
2) absolutely
what I wrote I mentioned it to OP.
He should try given link and other examples to learn and understand how it works, if any problem, raise question.
|
|
|
Re: Filter by Count of particular field [message #615480 is a reply to message #615378] |
Wed, 04 June 2014 09:02 |
|
live4learn
Messages: 41 Registered: September 2013 Location: Bangalore, India
|
Member |
|
|
Try this :
Select ID, first_last, SITENAME,UTILIZATION,REPORTED,OVERALL,monthYear,SUPERVISOR_NAME
from (
Select count(1) cnt,ID, first_last, SITENAME,UTILIZATION,REPORTED,OVERALL,monthYear,SUPERVISOR_NAME
from MONTHLY_SUM
where monthYear
in ('OCT-13','NOV-13','DEC-13','JAN-14','FEB-14','MAR-14','APR-14','MAY-14') and SUPERVISOR_NAME is not null
Group by ID, first_last,SITENAME,UTILIZATION,REPORTED,OVERALL,monthYear,SUPERVISOR_NAME
) where cnt >2;
I am assuming your group by is working fine as per your expectation!
|
|
|
|
Re: Filter by Count of particular field [message #615489 is a reply to message #615487] |
Wed, 04 June 2014 12:17 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
@Ed, Even I was a part of the y2k/RR combat. However, there are still legacy applications which are used to such design flaws. And developers are bound to violate the rules. Now, what to do? Poor developer.
|
|
|
Goto Forum:
Current Time: Thu Apr 25 05:33:51 CDT 2024
|