Effective way in PRESTO to Result output with Boolean values

会有一股神秘感。 提交于 2021-01-28 10:25:47

问题


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:

  1. Is the above select statement is effective way in PRESTO.?
  2. If not what are the other options.
  3. 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.
  4. With CASE I cant able to use COUNT how can i add a rule if i want to see COUNT is greater than 2. (Count(*) > 0) = True else False.
  5. Can we use SubQuery in CASE? 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!