SQL - 比较两列找到真值并使用IF计算中的值

时间:2016-06-15 17:10:22

标签: sql sql-server if-statement

我想比较两列page_entry& exit_page如果两个真值均为1则反弹。

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

这是我提出的,它只显示了真正的价值

select hit_id,visit_num,
    CASE 

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

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

但现在我需要写一个类似

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

3 个答案:

答案 0 :(得分:0)

你正在寻找这样的东西吗? :

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 ;

答案 1 :(得分:0)

CASE WHEN entry_page = 1 AND exit_page = 1 THEN 'True' ELSE 'False' END as 'BounceAnswer'

SUM(CASE WHEN entry_page = 1 AND exit_page = 1 THEN 1 ELSE 0 END) as 'BounceCount'

答案 2 :(得分:0)

选择hit_id,visit_num,     SUM(CASE entry_page         当exit_page那么1         当entry_page那么1         ELSE 0
        END)/ visit_num作为比率 来自[dbo]。[小学] 按hit_id,visit_num,entry_page分组;

这让我非常感谢!!!!!