SELECT t_1.*, t_2.*, t_3.*, t_4.*, t_5.*, t_6.*, t_7.*
FROM t1 t_1
left join t2 t_2 on (1=1)
left Join t3 t_3 on (1=1)
left Join t4 t_4 on (1=1)
left Join t5 t_5 on (1=1)
left Join t6 t_6 on (1=1)
left Join t7 t_7 on (1=1)
select id,
name,
parent_id
from (select * from products
order by parent_id, id) products_sorted,
(select @pv := '2') initialisation
where find_in_set(parent_id, @pv)
and length(@pv := concat(@pv, ',', id))
select Class_type,
count(1) over (partition by case when Class_type = 'HZ' then 'Начальная школа' else Class_type end) cnt,
sum(for_sum) over (partition by case when Class_type = 'HZ' then 'Средняя школа' else Class_type end) sum_
from (
select case when id in (1,2,3,4) then 'Начальная школа'
when id = 5 then 'HZ'
when id in (6,7,8,9) then 'Средняя школа'
when id in (10,11) then 'Старшая школа'
end Class_type,
case when s.ocenka>4 then 1 else 0 end for_sum
from class c ) t
group by Class_type
having Class_type <> 'HZ'
select distinct
FIRST_VALUE(currency) OVER (PARTITION BY currency ORDER BY created_at desc) currency,
FIRST_VALUE(price) OVER (PARTITION BY currency ORDER BY created_at desc) price
from course
SELECT
strftime('%Y', case Dub_date when 'Reg' then reg_date else terminate_date end) AS Date_,
count(case Dub_date when 'Reg' then reg_date end) AS opened_count,
count(case Dub_date when 'Terminate' then terminate_date end) As terminate_count
FROM COMPANIES, (select 'Reg' Dub_date union all select 'Terminate')
group by Date_
having Date_ is not null
select store, bill_number, sum(case product when 'Accessory' then quantity else 0 end) quantity
from table_name
where product in ('Flower', 'Accessory')
group by store, bill_number
having count( distinct product) = 2
select case when id in (1,2,3,4) then 'Начальная школа'
when id in (5,6,7,8,9) then 'Средняя школа'
when id in (10,11) then 'Младшая школа'
end Class_type,
COUNT(*) from class c
group by Class_type
select *
from (SELECT `c`.*, row_number() OVER (partition by `c`.`articleId` order by `c`.`date` DESC ) rn
FROM comments AS `c` WHERE `c`.`articleId` IN (
'44',
'55',
'414',
'555',
'775'
) AND `c`.`approved` ='1' ORDER BY `c`.`date` DESC) t1
where rn <= 5;
SELECT * FROM production as t1
LEFT JOIN production_favorite as t2 ON t1.id = t2.product_id
WHERE t1.brand_id = 145 and t2.name = 'opisanie'
select af_id, sum(coalesce(amount_value,0))*0.1 sum_pair_10
from (
select
arur.af_id,
arur.r_w_uid,
lag(arur.r_w_uid,1,-1) over (partition by arur.af_id order by arur.r_w_uid) r_w_uid_1
,row_number() OVER (partition by arur.af_id order by arur.r_w_uid ) mod 2 pair_flag
from arur, u_af
where u_af.id = arur.af_id ) t1 left join orders o on (o.uid in (t1.r_w_uid,t1.r_w_uid_1) and o.status = 'Completed')
where pair_flag = 0
group by af_id
having count(distinct r_w_uid) > 5
select af_id, sum(coalesce(amount_value,0))*0.1 sum_pair_10
from (SELECT arur.af_id,
arur.r_w_uid,
@row_num := CASE WHEN @row_num_val = af_id THEN @row_num+1
WHEN (@row_num_val := af_id) IS NOT NULL THEN 1
END pair_flag,
@lag_r_w_uid := CASE WHEN (@row_num_val = af_id) and @row_num mod 2 = 1 THEN r_w_uid
else @lag_r_w_uid
END r_w_uid_1
FROM arur, u_af, (SELECT @row_num := null, @row_num_val := null, @lag_r_w_uid := null) AS x
where u_af.id = arur.af_id
ORDER BY af_id, r_w_uid ) t1 left join orders o on (o.uid in (t1.r_w_uid,t1.r_w_uid_1) and o.status = 'Completed')
where pair_flag mod 2 = 0
group by af_id
having count(distinct r_w_uid) > 5