SELECT
    {date_func}(Date) AS Date,
    {campaign_field} AS CampaignName,
    if(TargetType = 3, 'Networks', 'Search') AS Place,
    multiIf(
      Date < '2017-10-01' AND DeviceType < 4, 'Mobile',
      Date < '2017-10-01', 'Desktop',
      HitDeviceType < 4, 'Mobile',
      'Desktop'
    ) AS Device,
    multiIf(
        match(Options, 'performance'), 'smart_banner',
        ProductType = 'video-creative', 'video_ad',
        ProductType IN ('auto-video-direct', 'video-motion'), 'video_extensions',
        ProductType IN ('media-image', 'media-creative') AND Place = 'Search', 'search_banner',
        ProductType IN ('media-image', 'media-creative') AND Place = 'Networks', 'image_ad',
        match(Options, 'dynamic-banner'), 'dynamic_banner',
        'text_image_ad'
    ) AS BannerType,
    multiIf(
        SelectType IN (27, 45, 67) OR ContextType = 2, 'retargeting',
        ContextType = 3 AND SimDistance in (8, 44, 45, 48, 308, 341, 342, 343, 344, 345, 408, 508, 908, 941, 942, 943, 944, 945, 1108, 1408, 1508, 1608, 1708, 1808, 1908, 2008), 'synonym',
        ContextType = 3, 'broadmatch',
        match(Options, 'navig-source') OR match(Options, 'navig-list-match'), 'navig',
        Place = 'Search', 'phrase_other',
        'phrase'
    ) AS TargetingType,
    SUM(Sign) AS DirectClicks,
    sumIf((Sign * EventCost) * 30 / 1000000 / 1.18, match(AutobudgetOptions, '(^|,)paid-actions(,|$)') = 0) AS DirectCost
FROM click_storage_all
WHERE
    not match(Options, 'mobile-app')
    AND Date >= '{from_date}' AND Date < '{to_date}'
    AND (PlaceID = 542 AND GoodEvent > 0)
    {clids_condition}
    {cids_condition}
    {domains_condition}
    {regions_condition}
GROUP BY
    Date,
    CampaignName,
    Place,
    Device,
    BannerType,
    TargetingType
