Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I get a query to return a "1" value in place of text

Here’s the full code:

WITH Part1 AS 
(
    SELECT 
        session_id, occurred_at, activity_name
    FROM      
        Activity_Data_Table$
    WHERE   
        (occurred_at > CURRENT_TIMESTAMP - 31) 
        AND (activity_name = 'create')
    GROUP BY 
        session_id, occurred_at, activity_name
), 
Part2 AS 
(
    SELECT 
        session_id, occurred_at, activity_name
    FROM      
        Activity_Data_Table$ AS Activity_Data_Table$_1
    WHERE   
        (occurred_at > CURRENT_TIMESTAMP - 31) 
        AND (activity_name = 'Resolve')
    GROUP BY 
        session_id, occurred_at, activity_name
)
SELECT 
    Part1_1.occurred_at, Part1_1.activity_name, 
    Part2_1.activity_name
FROM   
    Part1 AS Part1_1 
LEFT OUTER JOIN 
    Part2 AS Part2_1 ON Part1_1.session_id = Part2_1.session_id

Here is what I get:

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

It returns 3 columns:

Date, activity 1, activity 2
           

What I want, and it can be in additional columns or substitute: in the 2 activity columns, if there is a non-null value (create or resolve) then I want to insert a ‘1’ otherwise 0 or null. Tried countless ways of inserting count and it’s just not working, but I’m sure someone with better skills this is 2 seconds.

Thanks

>Solution :

There’s no need to query the table twice and join the results.

SELECT 
    session_id,
    occurred_at,
    MAX(CASE WHEN activity_name = 'create'  THEN 1 ELSE 0 END) AS has_create,
    MAX(CASE WHEN activity_name = 'Resolve' THEN 1 ELSE 0 END) AS has_resolve
FROM
    Activity_Data_Table$
WHERE   
        occurred_at   >  CURRENT_TIMESTAMP - 31
    AND activity_name IN ('create', 'Resolve')
GROUP BY
    session_id,
    occurred_at

This will only return sessions that have at least one or other activity. If you want to allow sessions with neither activity, just remove the AND activity_name IN ('create', 'Resolve')

EDIT:

I just realised I don’t know if the two activities happen at the same time. If they do, great, the above query works. If they can be different, something like this may help…

SELECT 
    session_id,
    MIN(occurred_at)   AS occured_at_first,
    MAX(occurred_at)   AS occured_at_last,
    MAX(CASE WHEN activity_name = 'create'  THEN occurred_at END) AS occured_at_create,
    MAX(CASE WHEN activity_name = 'Resolve' THEN occurred_at END) AS occured_at_resolve,        
    MAX(CASE WHEN activity_name = 'create'  THEN  1  ELSE  0 END) AS has_create,
    MAX(CASE WHEN activity_name = 'Resolve' THEN  1  ELSE  0 END) AS has_resolve
FROM
    Activity_Data_Table$
WHERE   
    occurred_at   >  CURRENT_TIMESTAMP - 31
GROUP BY
    session_id

It depends on what you need/want.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading