只计算指定值中的一个

0mkxixxg  于 2021-08-09  发布在  Java
关注(0)|答案(3)|浏览(335)

我真希望有人能帮我。
我在我的列中做了两个count函数,都是由列中有特定文本的位置指定的(请参见下面的代码)。在我的会议计数(event\u type='meeting\u start')中,我发现这可以在同一个会议中弹出多次。我只需要计算一次,每个会议事件id,找不到一个方法来做到这一点。

select I.CRM_INSTANCE_NAME,
       C.CUSTOMER_NAME,
       U.USER_NAME,
       ltrim(TO_CHAR(U.CREATED_DATE ,'mm-yyyy'),'0') AS USER_ACTIVATION_DATE,
       ltrim(TO_CHAR(U.LAST_UPDATED_DATE ,'dd-mm-yyyy'),'0') AS LAST_ACTIVITY_DATE,
       COUNT(E.EVENT_TYPE) AS MEETING,
       COUNT(F.EVENT_TYPE) AS FINALISED

       FROM SBX_USERS U
        LEFT JOIN SBX_CUSTOMERS C on U.CUSTOMER_ID = C.CUSTOMER_ID
        LEFT JOIN SBX_CRM_INSTANCES I on C.CRM_INSTANCE_ID = I.CRM_INSTANCE_ID
        LEFT JOIN SBX_MEETING M on U.USER_ID = M.USER_ID
        LEFT JOIN SBX_EVENTS E ON SOURCE_ID  = M.MEETING_ID
        LEFT JOIN (SELECT * FROM SBX_EVENTS WHERE EVENT_TYPE = 'meeting_start' AND EVENT_CREATION_DATE >=add_months(sysdate, -12)) E on E.SOURCE_ID = M.MEETING_ID
        LEFT JOIN (SELECT * FROM SBX_EVENTS WHERE EVENT_TYPE = 'finalize'AND EVENT_CREATION_DATE >=add_months(sysdate, -12)) F on F.SOURCE_ID = M.MEETING_ID

where U.ENABLED_FLAG IN ('Y','E')

group by  I.CRM_INSTANCE_NAME, C.CUSTOMER_NAME, U.USER_NAME, ltrim(TO_CHAR(U.CREATED_DATE ,'mm-yyyy'),'0'), ltrim(TO_CHAR(U.LAST_UPDATED_DATE ,'dd-mm-yyyy'),'0')
order by  I.CRM_INSTANCE_NAME;

以上是我当时正在使用的代码,提前感谢您的帮助。
当做
博士

im9ewurl

im9ewurl1#

您不需要来自sbx\u事件的两个额外的外部连接,您可以使用条件聚合,例如:

select I.CRM_INSTANCE_NAME,
       C.CUSTOMER_NAME,
       U.USER_NAME,
       ltrim(TO_CHAR(U.CREATED_DATE ,'mm-yyyy'),'0') AS USER_ACTIVATION_DATE,
       ltrim(TO_CHAR(U.LAST_UPDATED_DATE ,'dd-mm-yyyy'),'0') AS LAST_ACTIVITY_DATE,
       COUNT(DISTINCT CASE WHEN E.EVENT_TYPE = 'meeting_start' AND eVENT_CREATION_DATE >=add_months(sysdate, -12) THEN 1 END) AS MEETING,
       COUNT(CASE WHEN E.EVENT_TYPE = 'finalize' AND eVENT_CREATION_DATE >=add_months(sysdate, -12) THEN 1 END) AS FINALISED
       FROM SBX_USERS U
        LEFT JOIN SBX_CUSTOMERS C on U.CUSTOMER_ID = C.CUSTOMER_ID
        LEFT JOIN SBX_CRM_INSTANCES I on C.CRM_INSTANCE_ID = I.CRM_INSTANCE_ID
        LEFT JOIN SBX_MEETING M on U.USER_ID = M.USER_ID
        LEFT JOIN SBX_EVENTS E ON SOURCE_ID  = M.MEETING_ID
where U.ENABLED_FLAG IN ('Y','E')
group by  I.CRM_INSTANCE_NAME, C.CUSTOMER_NAME, U.USER_NAME, ltrim(TO_CHAR(U.CREATED_DATE ,'mm-yyyy'),'0'), ltrim(TO_CHAR(U.LAST_UPDATED_DATE ,'dd-mm-yyyy'),'0')
order by  I.CRM_INSTANCE_NAME;

n、 你有两张table化名为 e ,所以我猜你在数中指的是哪一个。
另外,如果您只关心meeting\u start和finalize事件\u类型,则可以从sbx\u events表中添加该类型作为连接条件以及日期限制。

cngwdvgl

cngwdvgl2#

你需要一个分组在你的子选择中,你称之为“表e”,就像这样:

select I.CRM_INSTANCE_NAME,
       C.CUSTOMER_NAME,
       U.USER_NAME,
       ltrim(TO_CHAR(U.CREATED_DATE ,'mm-yyyy'),'0') AS USER_ACTIVATION_DATE,
       ltrim(TO_CHAR(U.LAST_UPDATED_DATE ,'dd-mm-yyyy'),'0') AS LAST_ACTIVITY_DATE,
       COUNT(E.MEETING_NO) AS MEETING,
       COUNT(F.EVENT_TYPE) AS FINALISED

       FROM SBX_USERS U
        LEFT JOIN SBX_CUSTOMERS C on U.CUSTOMER_ID = C.CUSTOMER_ID
        LEFT JOIN SBX_CRM_INSTANCES I on C.CRM_INSTANCE_ID = I.CRM_INSTANCE_ID
        LEFT JOIN SBX_MEETING M on U.USER_ID = M.USER_ID
        LEFT JOIN SBX_EVENTS E ON SOURCE_ID  = M.MEETING_ID
        LEFT JOIN (SELECT EVENT_TYPE,SOURCE_ID,count(*) MEETING_NO FROM SBX_EVENTS WHERE EVENT_TYPE = 'meeting_start' AND EVENT_CREATION_DATE >=add_months(sysdate, -12) GROUP BY EVENT_TYPE,SOURCE_ID) E on E.SOURCE_ID = M.MEETING_ID
        LEFT JOIN (SELECT * FROM SBX_EVENTS WHERE EVENT_TYPE = 'finalize'AND EVENT_CREATION_DATE >=add_months(sysdate, -12)) F on F.SOURCE_ID = M.MEETING_ID

where U.ENABLED_FLAG IN ('Y','E')

group by  I.CRM_INSTANCE_NAME, C.CUSTOMER_NAME, U.USER_NAME, ltrim(TO_CHAR(U.CREATED_DATE ,'mm-yyyy'),'0'), ltrim(TO_CHAR(U.LAST_UPDATED_DATE ,'dd-mm-yyyy'),'0')
order by  I.CRM_INSTANCE_NAME;
bjg7j2ky

bjg7j2ky3#

本质上,我认为你想要 COUNT(DISTINCT ...) .
但是,您的查询不是有效的sql:您有两个别名 E :连接的逻辑 sbx_events 是有问题的。我怀疑您实际上希望在该表上有一个连接(在日期上有一个连接条件),在 COUNT(DISTINCT) .
那就是:

select 
    i.crm_instance_name,
    c.customer_name,
    u.user_name,
    ltrim(to_char(u.created_date ,'mm-yyyy'),'0') as user_activation_date,
    ltrim(to_char(u.last_updated_date ,'dd-mm-yyyy'),'0') as last_activity_date,
    count(distinct case when event_type = 'meeting_start'  then m.meeting_id end) as meeting,
    count(distinct case when event_type = 'finalize'  then m.meeting_id end) as finalized
from sbx_users u
left join sbx_customers c 
    on u.customer_id = c.customer_id
left join sbx_crm_instances i 
    on c.crm_instance_id = i.crm_instance_id
left join sbx_meeting m 
    on u.user_id = m.user_id
left join sbx_events e 
    on source_id  = m.meeting_id
    and  e.event_creation_date >= add_months(sysdate, -12))
where u.enabled_flag in ('y','e')
group by  
    i.crm_instance_name, 
    c.customer_name, 
    u.user_name, 
    ltrim(to_char(u.created_date ,'mm-yyyy'),'0'), 
    ltrim(to_char(u.last_updated_date ,'dd-mm-yyyy'),'0')
order by  i.crm_instance_name;

相关问题