SQL Case语句问题

时间:2018-06-30 01:24:15

标签: sql google-bigquery

我正在尝试使我认为是对SQL查询的另外两个case语句,但无法弄清楚如何添加这些特定的语句。

我想要的是改善partner_name和Publisher_name的案例声明。现在,如果用户来自自然应用商店搜索,则partner_name和Publisher_name为“”。我想将“”替换为可用于读取的内容,这样就不会有包含空数据的行。我想要的是对上述两个参数的查询中已有的case语句进行改进,以覆盖Internal&Organic。目标是当渠道是自然渠道或内部渠道时,这两个字段将插入渠道名称而不是空白。

源表中的数据始终以UTC时间表示,但是我们使用PST时间。必须将其保留在UTC中(长话短说)。所有项目都是字符串。

这是此示例的输出。

Example Output

这是我当前的SQL

select install_date, campaign_type, channel, campaign_name, platform, partner_name, publisher_name, count (idfa)
from
(
select  
DATE(DATE_ADD(TIMESTAMP(left(timestamp_iso,19)),-7, 'HOUR')) install_date,
case when last_attributed_touch_data_tilde_feature = '' then 'organic' else last_attributed_touch_data_tilde_feature end as  campaign_type,
case when last_attributed_touch_data_tilde_channel = '' then 'organic' else last_attributed_touch_data_tilde_channel end as channel,
case when last_attributed_touch_data_tilde_campaign = '' then 'organic' else last_attributed_touch_data_tilde_campaign end as campaign_name,
case when last_attributed_touch_data_tilde_advertising_partner_name = '' then last_attributed_touch_data_tilde_channel else last_attributed_touch_data_tilde_advertising_partner_name end as partner_name,
case when last_attributed_touch_data_tilde_secondary_publisher = '' then last_attributed_touch_data_tilde_channel else last_attributed_touch_data_tilde_secondary_publisher end as publisher_name,

last_attributed_touch_type,
case when lower(user_data_platform) like 'android%' then 'Android' when user_data_platform like 'IOS%' then 'iOS' else 'Other' end as platform,
user_data_aaid,
user_data_idfa,
user_data_idfv,

case when (user_data_aaid is null or user_data_aaid = '') and (user_data_idfa = '' or user_data_idfa is null ) then user_data_idfv
when (user_data_aaid is null or user_data_aaid = '') then user_data_idfa
else user_data_aaid end as idfa,


FROM table_date_range([app_install_date_], timestamp('2018-06-23'), timestamp('2018-06-25')) --Change timestamp as required
where 1=1
and DATE(DATE_ADD(TIMESTAMP(left(timestamp_iso,19)),-7, 'HOUR')) >= '2018-06-23'
and DATE(DATE_ADD(TIMESTAMP(left(timestamp_iso,19)),-7, 'HOUR')) <= '2018-06-24'
group by 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
  order by 1 desc, 2 desc, 6 desc
  )
  group by 1, 2, 3, 4, 5, 6, 7

我只是不确定在advertisign_partner_name和secondary_publisher_name的声明时如何修改或更改我的案子。我尝试这样做,但它仅在内部通道时才起作用...不是自然通道。我希望它既能处理内部事务又能处理内部事务。

1 个答案:

答案 0 :(得分:0)

  

目标是这两个字段...插入频道名称,而不要为空

捷径是:
代替

SELECT install_date, campaign_type, channel, campaign_name, platform, 
  partner_name, publisher_name, count (idfa)   

使用

SELECT install_date, campaign_type, channel, campaign_name, platform, 
  IF(partner_name = '', channel, partner_name) AS partner_name, 
  IF(publisher_name = '', channel, publisher_name) AS publisher_name,
  count (idfa)