?
Solved

Oracle Column Alias

Posted on 2007-10-01
6
Medium Priority
?
22,195 Views
Last Modified: 2013-12-19
Hi 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
0
Comment
Question by:cutie_smily
  • 3
  • 2
6 Comments
 
LVL 35

Accepted Solution

by:
johnsone earned 900 total points
ID: 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.
0
 

Author Comment

by:cutie_smily
ID: 19991573
Thanks for making it clear. Please help in re-writing the query to achieve the same results.
0
 
LVL 35

Expert Comment

by:johnsone
ID: 19991831
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:cutie_smily
ID: 19992170
Thanks. My question was how can we achieve this without summing the columns twice.
0
 
LVL 35

Assisted Solution

by:johnsone
johnsone earned 900 total points
ID: 19993097
You are not summing the columns twice.  It only sums the columns once.  The having is a check after the fact.
0
 
LVL 28

Assisted Solution

by:Naveen Kumar
Naveen Kumar earned 300 total points
ID: 19994502
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
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question