To continue reading for free, register below or login
To read more you must become a member of SearchOracle.com
');
// -->

The difficulty with this type of problem is that it requires
returning detail rows for a situation which is easily solved with HAVING.
However, to use HAVING we have to use GROUP BY, and then the detail
rows are aggregated. The way to approach this problem is to break it
down into components. The first component of the solution does involve
the HAVING clause.
select Acct
from Weekly
group
by Acct
having count(*) >= 3
Note that COUNT(DISTINCT WeekEnding) also works. Whether we use this
or the simpler COUNT(*) depends on whether the combination of WeekEnding
and Acct is unique. Counting distinct values may be necessary if, for
example, there are other columns you didn't mention, such as customer.
In that case, we might have one row per customer per account per week ending
date, so COUNT(DISTINCT WeekEnding) is necessary if the GROUP BY is only
on the account.
Now that we know which accounts qualify, we can build the other component,
which is the selection of all detail rows for the accounts which qualify.
This can be accomplished in two ways. The first method uses an IN subquery:
select WeekEnding
, Amt
, Acct
from Weekly
where Acct in
( select Acct
from Weekly
group
by Acct
having count(*) >= 3 )
The second method uses the same subquery but as a derived table
in the FROM clause:
select WeekEnding
, Amt
, Acct
from Weekly
inner
join ( select Acct
from Weekly
group
by Acct
having count(*) >= 3 ) as ok_accts
on ok_accts.Acct = Weekly.Acct
What is the difference between these two solutions? As far as execution
goes, they should perform the same. But the derived table has an advantage.
Suppose that if, instead of one column, the HAVING condition needed to be based
on a GROUP BY involving two columns. What if you wanted details for all customer
accounts that occurred at least 3 times?
The first solution would be:
select WeekEnding
, Amt
, Acct
, Customer
from Weekly
where ( Acct, Customer ) in
( select Acct
, Customer
from Weekly
group
by Acct
, Customer
having count(*) >= 3 )
This solution now uses what is called a row constructor, and this
is perfectly valid SQL. The problem with row constructors is that not every database
engine supports them. Yet.
Now imagine what the query involving the join to the derived table would look like.
|