Thanks for making it clear. Please help in re-writing the query to achieve the same results.
Main Topics
Browse All TopicsHi All,
Why cant I use the column alias on the having clause or in the where clause. Am I missing anything or Oracle doesnt support aliases other than order by clause?
If I want to re-write the below query how can I do without re-summing the columns again in the having clause.
Thanks,
**************************
SELECT
FILL_DATE,
TRIM(D.GROUP_NBR) || TRIM(MEM.SUBSCRIBER_NBR) GRP_SUBS,
PHR_NBR#,
RX_NUM,
REFILL,
DRUG#,
SUM(MTUNITS) SUM_MTUNITS,
SUM(COPAY) SUM_COPAY,
SUM(TOTAL_AMOUNT_DUE) SUM_AMTDUE,
SUM(DAYS_SUPPLY) SUM_DAYSUPP,
SUM(PROF_FEE) SUM_PROFFEE,
SUM(ADD_COPAY) SUM_ADDCOPAY
FROM STG.DRUG D
LEFT OUTER JOIN STG.Member MEM
ON TRIM(D.MEMBER) = MEM.CES_ALT_ID
WHERE
D.FILL_DATE BETWEEN MEM.EFF_DT AND MEM.END_DT AND
MEM.VOID_FLAG <> 'V'
GROUP BY
FILL_DATE,
TRIM(D.GROUP_NBR) || TRIM(MEM.SUBSCRIBER_NBR),
PHR_NBR#,
RX_NUM,
REFILL,
DRUG#
HAVING SUM_COPAY <> 0 OR SUM_AMTDUE <> 0 OR SUM_MTUNITS <> 0
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
It looks like they were only in the HAVING.
SELECT
FILL_DATE,
TRIM(D.GROUP_NBR) || TRIM(MEM.SUBSCRIBER_NBR) GRP_SUBS,
PHR_NBR#,
RX_NUM,
REFILL,
DRUG#,
SUM(MTUNITS) SUM_MTUNITS,
SUM(COPAY) SUM_COPAY,
SUM(TOTAL_AMOUNT_DUE) SUM_AMTDUE,
SUM(DAYS_SUPPLY) SUM_DAYSUPP,
SUM(PROF_FEE) SUM_PROFFEE,
SUM(ADD_COPAY) SUM_ADDCOPAY
FROM STG.DRUG D
LEFT OUTER JOIN STG.Member MEM
ON TRIM(D.MEMBER) = MEM.CES_ALT_ID
WHERE
D.FILL_DATE BETWEEN MEM.EFF_DT AND MEM.END_DT AND
MEM.VOID_FLAG <> 'V'
GROUP BY
FILL_DATE,
TRIM(D.GROUP_NBR) || TRIM(MEM.SUBSCRIBER_NBR),
PHR_NBR#,
RX_NUM,
REFILL,
DRUG#
HAVING SUM(COPAY) <> 0 OR SUM(TOTAL_AMOUNT_DUE) <> 0 OR SUM(MTUNITS) <> 0
You can do it as shown below :
select * -- pick only whatever columns you need from inner columns which are existing
from
(
SELECT
FILL_DATE,
TRIM(D.GROUP_NBR) || TRIM(MEM.SUBSCRIBER_NBR) GRP_SUBS,
PHR_NBR#,
RX_NUM,
REFILL,
DRUG#,
SUM(MTUNITS) SUM_MTUNITS,
SUM(COPAY) SUM_COPAY,
SUM(TOTAL_AMOUNT_DUE) SUM_AMTDUE,
SUM(DAYS_SUPPLY) SUM_DAYSUPP,
SUM(PROF_FEE) SUM_PROFFEE,
SUM(ADD_COPAY) SUM_ADDCOPAY
FROM STG.DRUG D
LEFT OUTER JOIN STG.Member MEM
ON TRIM(D.MEMBER) = MEM.CES_ALT_ID
WHERE
D.FILL_DATE BETWEEN MEM.EFF_DT AND MEM.END_DT AND
MEM.VOID_FLAG <> 'V'
GROUP BY
FILL_DATE,
TRIM(D.GROUP_NBR) || TRIM(MEM.SUBSCRIBER_NBR),
PHR_NBR#,
RX_NUM,
REFILL,
DRUG# )
where SUM_COPAY <> 0 OR SUM_AMTDUE <> 0 OR SUM_MTUNITS <> 0;
You can refer to any of the columns inside the inline view which can be
in the select list, where clause, group by , having, order etc.
This is just to show you that we can use inline views to consider the
data returned as a table itself but not real table.
Thanks
Business Accounts
Answer for Membership
by: johnsonePosted on 2007-10-01 at 08:52:36ID: 19991542
No, you cannot use an alias in a where or group by.
The technicality of it is that when the where clause and the group by clause are being executed, the select part of the query has not run and the alias has not been assigned. Since the order by is technically done after the select the aliases can be used.