Link to home
Start Free TrialLog in
Avatar of Aditya U
Aditya U

asked on

What if there is a CASE statement with data from different columns

CASE
             WHEN a.money = 1 THEN c.cashamount
             ELSE c.time
          END
             AS amount,
Avatar of Raghavendra Hullur
Raghavendra Hullur
Flag of India image

Hi,
If the data types for those columns are same, then it can be used.
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

You can use different columns values.  Why are you thinking you cannot?
Avatar of Aditya U

ASKER

I must have not clearly mentioned the question above. What if I have to use that column name in a group by statement??
I want to use that amount column in a group by statement . To avoid ambiguity I have to prefix with a table name , but as you can see case stmnt includes data from two tables 'a' and 'c'. So I am confused which tablename to use as prefix??
Can you explain more?
Do you want to use the case statement output for grouping? If yes, then you can do so using an outer select statement on top of the one used for retrieving the values.
CASE
             WHEN a.money = 1 THEN c.cashamount
             ELSE c.time
          END
             AS amount,

Yes, I want to use value of 'amount' in my group by statement. When I mention amount directly it is giving me a 0ra-00918 error.
https://www.essentialsql.com/using-conditional-logic-in-sql-with-case-expression/


I refered to this link which stated that you can alias a column alias for a case in GROUP BY. The entire CASE statemnt has to be repeated??!!!
Yes, that's true.
If you don't want to do that, you can write an outer select statement and use the case statement output for grouping.
Can you please elaborate your answer with the above sample code?
ASKER CERTIFIED SOLUTION
Avatar of Raghavendra Hullur
Raghavendra Hullur
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial