问题
Im trying to create a output of boolen values on back of some conditions.
OutPut:
EX: I have 3 rules/conditions from different tables which are not related to each other.
Rule 1:
Select USER_NAME, ID from session_user where age > 25
Rule 2:
Select USER_NAME, ID from current_user where plan = 'gold'
Rule 3:
Select USER_NAME, ID from customer where group_name='managers'
My OutPut Should be:
USER_NAME | ID | Rule 1 | Rule 2 | Rule 3
user1 1 true false true
user2 2 false true true
user3 3 true true true
If user1
is passing rule one the value should be true
for him in the output, if he passed rule 2
then true
else false
.
Solution:
SELECT dp.USER_NAME,
dp.ID,
CASE
WHEN dp.sex='F' THEN 'True'
ELSE 'False'
END AS Rule_1,
CASE
WHEN dp.sex='M' THEN 'True'
ELSE 'False'
END AS Rule_2,
CASE
WHEN dp.sex not in ('M','F') THEN 'True'
ELSE 'False'
END AS Rule_3
FROM user_details dp where dp.Organisation='007';
Question:
- Is the above select statement is effective way in PRESTO.?
- If not what are the other options.
- Im new to PRESTO so its difficult to use join in select, How do i form similar output when i join multiple tables? any doc links will be helpful to understand further.
- With
CASE
I cant able to useCOUNT
how can i add a rule if i want to seeCOUNT
is greater than 2. (Count(*) > 0) = True else False. - Can we use
SubQuery
inCASE
? Will it affect performance?
回答1:
PrestoDB supports boolean values, so no CASE
expression is necessary:
SELECT dp.USER_NAME, dp.ID,
(dp.sex = 'F') AS Rule_1,
(dp.sex = 'M') AS Rule_2,
(dp.sex not in ('M','F')) AS Rule_3
FROM user_details dp
WHERE dp.Organisation = '007';
I don't see what the rest of your questions have to do with your query. However, StackOverflow questions are limited to a single question. So feel free to ask another question . . . but PrestoDB (like all databases) supports subqueries in CASE
expressions.
来源:https://stackoverflow.com/questions/62873767/effective-way-in-presto-to-result-output-with-boolean-values