PRAGMA yson.AutoConvert; 
PRAGMA yson.DisableStrict;
$ad_tikets=['product:PROD-BANNER', 'product:PROD-DIRECT', 'product:PROD-METRIKA','product:PROD-PCODE', 
'product:PROD-MODADVERT', 'product:PROD-RMP'];
$day_from_miliseconds=($update_time)->{ 
$format = DateTime::Format("%Y-%m-%d");
RETURN $format(AddTimezone(DateTime::FromMilliseconds($update_time), "Europe/Moscow"));}
;
$week=($date)-> { 
RETURN DateTime::Format("%Y-%m-%d")(Datetime::MakeDate(DateTime::StartOfWeek(CAST($date as Date))));};
$is_list_in_list=($goals_list, $goal_ids) -> {
    RETURN NOT(SetIsDisjoint(ToSet($goals_list), Yson::ConvertToStringList($goal_ids))) ;
};

select 
week as `Неделя`,
count(distinct key) as `Число SPI`,
sum(ydt) as `YandexDowntime`,
sum(money_lost*30/1.18) as `Финансовые потери`
FROM (
select 
$day_from_miliseconds(`created`) as day,
key,
Yson::ConvertToDouble(customFields['dengiVFiskah']) as money_lost,
Yson::ConvertToDouble(customFields['ydt']) as ydt
FROM hahn.`home/startrek/tables/prod/yandex-team/queue/SPI/issues`
WHERE $day_from_miliseconds(`created`)>'2019-11-03' /*'2021-01-01'*/ and
$is_list_in_list($ad_tikets, tags)

)
GROUP BY $week(day) as week
ORDER BY `Неделя` DESC
;