在/ conditional时计算不同的列大小写

时间:2015-10-21 16:55:56

标签: postgresql count conditional distinct

我正在尝试计算列中不同数量的ID,这样可以正常工作。

COUNT(DISTINCT messages.id) AS link_created

但是当我尝试使用条件计数时,我得到一个语法错误,添加一个案例的正确语法是什么时候或者其他一些条件只计算messages.link_label所在的不同消息id?

COUNT(DISTINCT messages.id CASE WHEN messages.link_label IS NOT NULL 1 END) AS link_created

我的完整查询如下所示。

@customers = Customer.select("customers.*,
                                  COUNT(DISTINCT recipient_lists.id) messages_sent,
                                  COUNT(DISTINCT messages.id CASE WHEN messages.link_label IS NOT NULL 1 END) AS link_created,
                                  COALESCE(SUM(video_activities.video_watched_count),0) AS watched_count,
                                  COALESCE(SUM(video_activities.response_count),0) AS response_count,
                                  COALESCE(SUM(video_activities.email_opened_count),0) AS email_opened_count,
                                  COALESCE(SUM(CASE WHEN video_activities.video_watched_at IS NOT NULL THEN 1 ELSE 0 END),0) AS unique_watches,
                                  COALESCE(SUM(CASE WHEN video_activities.email_opened_at IS NOT NULL THEN 1 ELSE 0 END),0) AS unique_opens,
                                  COALESCE(SUM(CASE WHEN video_activities.response_count > 0 THEN 1 ELSE 0 END),0) AS unique_responses,
                                  customers.updated_at AS last_login,
                                  SUBSTRING( email from POSITION( '@' in email) + 1 for length(email)) AS company")
                          .joins("LEFT JOIN messages ON customers.id = messages.customer_id
                                  LEFT JOIN recipient_lists ON messages.id = recipient_lists.message_id AND messages.link_label is NULL               
                                  LEFT JOIN video_activities ON messages.id = video_activities.message_id")
                          .group("customers.id")

1 个答案:

答案 0 :(得分:9)

试试这个:

$(menuWrapper.selector+","+ menuTrigger.selector+","+menuIcon.selector+","+ contentWrapper.selector).removeClass();