user2414973 user2414973 - 8 months ago 17
SQL Question

SQL - Compare two columns find the true value and use the value in IF calculation

I am trying to compare two columns page_entry & exit_page if both true value of 1 then its a bounce.

Page_entry | exit_page | Bounce
1 1 true
0 1 false
etc etc ......

this is what I came up with and it only shows me the true values

select hit_id,visit_num,

WHEN entry_page = exit_page
THEN 'True'
ELSE 'False'

END as Bounce
FROM [dbo].[Primary]
WHERE entry_page = exit_page

BUT now I need to write a calculation like

SUM(IF Bouce = TRUE THEN 1 ELSE 0 END)/page_visit


Are you looking for something like this? :

select hit_id,visit_num,
    SUM (CASE entry_page 
        WHEN exit_page THEN 1
        ELSE 0      
        END)/page_visit as ratio
FROM [dbo].[Primary]
group by hit_id,visit_num,page_visit ;