Solved

Using column alias in where clause of select

Posted on 2008-10-13
4
2,567 Views
Last Modified: 2013-12-19
I have the following query(database is oracle):

SELECT TO_CHAR(TO_DATE('04.08.2008:10:34:24','DD.MM.YYYY:HH24:MI:SS'),'DD.MM.YYYY:HH24:MI:SS') "createddate",
       TO_CHAR(createddate,'DD.MM.YYYY:HH24:MI:SS') "dateCreated",
       trunc(86400*(createddate-TO_DATE('04.08.2008:10:34:24','DD.MM.YYYY:HH24:MI:SS')))-60*(trunc((86400*(createddate-TO_DATE('04.08.2008:10:34:24','DD.MM.YYYY:HH24:MI:SS')))/60)) "Sec",
       trunc((86400*(createddate-TO_DATE('04.08.2008:10:34:24','DD.MM.YYYY:HH24:MI:SS')))/60)-60*(trunc(((86400*(createddate-TO_DATE('04.08.2008:10:34:24','DD.MM.YYYY:HH24:MI:SS')))/60)/60)) "Min",
       trunc(((86400*(createddate-TO_DATE('04.08.2008:10:34:24','DD.MM.YYYY:HH24:MI:SS')))/60)/60)-24*(trunc((((86400*(createddate-TO_DATE('04.08.2008:10:34:24','DD.MM.YYYY:HH24:MI:SS')))/60)/60)/24)) "Hrs",
       trunc((((86400*(createddate-TO_DATE('04.08.2008:10:34:24','DD.MM.YYYY:HH24:MI:SS')))/60)/60)/24) "Days"
FROM table

which returns a column named "Days" with the difference in days between the created date in the table column and a date that i specified above.  I know that its not possible to use an alias in the where clause, so how can I get around it, basically i want to do something like this:

SELECT TO_CHAR(TO_DATE('04.08.2008:10:34:24','DD.MM.YYYY:HH24:MI:SS'),'DD.MM.YYYY:HH24:MI:SS') "createddate",
       TO_CHAR(createddate,'DD.MM.YYYY:HH24:MI:SS') "dateCreated",
       trunc(86400*(createddate-TO_DATE('04.08.2008:10:34:24','DD.MM.YYYY:HH24:MI:SS')))-60*(trunc((86400*(createddate-TO_DATE('04.08.2008:10:34:24','DD.MM.YYYY:HH24:MI:SS')))/60)) "Sec",
       trunc((86400*(createddate-TO_DATE('04.08.2008:10:34:24','DD.MM.YYYY:HH24:MI:SS')))/60)-60*(trunc(((86400*(createddate-TO_DATE('04.08.2008:10:34:24','DD.MM.YYYY:HH24:MI:SS')))/60)/60)) "Min",
       trunc(((86400*(createddate-TO_DATE('04.08.2008:10:34:24','DD.MM.YYYY:HH24:MI:SS')))/60)/60)-24*(trunc((((86400*(createddate-TO_DATE('04.08.2008:10:34:24','DD.MM.YYYY:HH24:MI:SS')))/60)/60)/24)) "Hrs",
       trunc((((86400*(createddate-TO_DATE('04.08.2008:10:34:24','DD.MM.YYYY:HH24:MI:SS')))/60)/60)/24) "Days"
FROM table where "Days" > 0
0
Comment
Question by:imrama
  • 2
4 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22704303
you cannot use the column alias directly in the Where clause.
you have to either repeat the expression, or use a subquery ...
0
 
LVL 42

Expert Comment

by:dqmq
ID: 22704327
Use an in-line view:

SELECT * FROM
(
SELECT TO_CHAR(TO_DATE('04.08.2008:10:34:24','DD.MM.YYYY:HH24:MI:SS'),'DD.MM.YYYY:HH24:MI:SS') "createddate",
       TO_CHAR(createddate,'DD.MM.YYYY:HH24:MI:SS') "dateCreated",
       trunc(86400*(createddate-TO_DATE('04.08.2008:10:34:24','DD.MM.YYYY:HH24:MI:SS')))-60*(trunc((86400*(createddate-TO_DATE('04.08.2008:10:34:24','DD.MM.YYYY:HH24:MI:SS')))/60)) "Sec",
       trunc((86400*(createddate-TO_DATE('04.08.2008:10:34:24','DD.MM.YYYY:HH24:MI:SS')))/60)-60*(trunc(((86400*(createddate-TO_DATE('04.08.2008:10:34:24','DD.MM.YYYY:HH24:MI:SS')))/60)/60)) "Min",
       trunc(((86400*(createddate-TO_DATE('04.08.2008:10:34:24','DD.MM.YYYY:HH24:MI:SS')))/60)/60)-24*(trunc((((86400*(createddate-TO_DATE('04.08.2008:10:34:24','DD.MM.YYYY:HH24:MI:SS')))/60)/60)/24)) "Hrs",
       trunc((((86400*(createddate-TO_DATE('04.08.2008:10:34:24','DD.MM.YYYY:HH24:MI:SS')))/60)/60)/24) "Days"
FROM table
) V

where "Days" > 0
0
 

Accepted Solution

by:
imrama earned 0 total points
ID: 22704360
i figured out just do this:

trunc((((86400*(createddate-TO_DATE('04.08.2008:10:34:24','DD.MM.YYYY:HH24:MI:SS')))/60)/60)/24) > 0
0
 
LVL 42

Expert Comment

by:dqmq
ID: 22704507
Which is what Angelll meant by "repeating the expression".  In line view also works--it's a matter of style.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to return more than 1 value from a store procedure 21 37
Oracle 12c database link between pdb not working 20 48
Oracle sql query 7 49
constraint check 2 19
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

867 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now