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,
WHEN a.money = 1 THEN c.cashamount
ELSE c.time
END
AS amount,
You can use different columns values. Why are you thinking you cannot?
ASKER
I must have not clearly mentioned the question above. What if I have to use that column name in a group by statement??
ASKER
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.
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.
ASKER
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.
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.
ASKER
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??!!!
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.
If you don't want to do that, you can write an outer select statement and use the case statement output for grouping.
ASKER
Can you please elaborate your answer with the above sample code?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If the data types for those columns are same, then it can be used.