SELECT DISTINCT events.id, events.name as event_name, events.date_start, events.date_end, events.country_id, events.description, events.day_1, events.day_2, events.month_1, events.month_2, events.year_1, events.year_2, events.is_present, links.links, images.images, countries.countries, countries.country_ids, country_to_cluster.year_start FROM events
LEFT JOIN (SELECT countries.id, GROUP_CONCAT(countries.id) as country_ids, event_to_country.country_id as country_id, event_to_country.event_id, GROUP_CONCAT(countries.name) as countries FROM countries LEFT JOIN event_to_country ON countries.id = country_id GROUP BY event_to_country.event_id) as countries ON events.id = countries.event_id
LEFT JOIN (SELECT event_id, GROUP_CONCAT(link) as links FROM links GROUP BY event_id) AS links ON events.id = links.event_id
LEFT JOIN (SELECT event_id, GROUP_CONCAT(name) as images FROM images GROUP BY event_id) AS images ON events.id = images.event_id
LEFT JOIN keywords ON events.id = keywords.event_id
LEFT JOIN country_to_cluster ON country_to_cluster.country_id = events.country_id AND events.year_1 >= coalesce(country_to_cluster.year_start,events.year_1) AND events.year_1 <= coalesce(country_to_cluster.year_end,events.year_1)
WHERE (country_to_cluster.cluster_id = :cluster_id)
ORDER BY events.year_1 ASC
LIMIT 30
SELECT n.id, n.text
FROM news AS n
JOIN news_tags AS nt ON nt.news_id = n.id
JOIN tags AS t1 ON t1.id = nt.tag_id AND t1.name in ("животные", "кроты" /*перечисляете свои теги*/)
group by n.id, n.text
having count(distinct t1.id) = 2 /*кол-во перечисленных тегов*/
SELECT product_id, count(distinct field_id) cnt_f, count(distinct value_id) cnt_v
from product
where ( (field_id = 'f1' and value_id in ('v1', 'v2'))
or (field_id = 'f2' and value_id in ('v3', 'v4'))
)
group by product_id
having count(distinct field_id) = 2 and count(distinct value_id) = 2
SELECT
wp_posts.ID,
wp_posts.post_title,
wc_product_meta_lookup.minPrice,
wc_product_meta_lookup.maxPrice
FROM
wp_posts
LEFT JOIN (
SELECT
product_id,
(min_price * GREATEST(1, coalesce(t2.meta_value*80,1), coalesce(t3.meta_value*80),1)) as minPrice,
(max_price * GREATEST(1, coalesce(t2.meta_value*80,1), coalesce(t3.meta_value*80),1)) as maxPrice
FROM
wp_wc_product_meta_lookup as t1
LEFT JOIN wp_postmeta as t2 ON t1.product_id = t2.post_id
AND t2.meta_key = 'convert_to_rub'
AND t2.meta_value = 1
JOIN wp_term_relationships tr ON (t1.product_id = tr.object_id AND tr.term_taxonomy_id IN ( 83, 85 ) )
LEFT JOIN wp_termmeta as t3 ON tr.term_taxonomy_id = t3.term_id
AND t3.meta_key = 'convert_to_rub'
AND t3.meta_value = 1
) wc_product_meta_lookup ON (
wp_posts.ID = wc_product_meta_lookup.product_id
)
WHERE
1 = 1
AND wp_posts.post_type = 'product'
AND wc_product_meta_lookup.minPrice > 0
--GROUP BY wp_posts.ID
ORDER BY wp_posts.post_name ASC
select t1.*
from table t1
join (select nomer_zakaza, max(data_obnovleniya) max_DT, min(data_obnovleniya) min_DT
from table
group by nomer_zakaza) t2
on (t1.nomer_zakaza = t2.nomer_zakaza and t1.data_obnovleniya in (max_DT, min_DT))
order by t1.nomer_zakaza, t1.data_obnovleniya
select p.*
from periods p
join (
select subject_id, group_id, min(start) min_start, max(start) max_start, max(case when "your date" between start and finish then start end) mid_start
from periods
group by subject_id, group_id
having mid_start is not null /*если хотите чтобы данные выводились только те у которых "your date" попал в период */
) temp_T on ( p.subject_id = temp_T.subject_id
and p.group_id = temp_T.group_id
and p.start in (min_start, coalesce(mid_start,min_start), max_start))
select distinct
first_value(message) over (partition by LEAST(sender_id, recipient_id),GREATEST(sender_id, recipient_id)
order by id desc)
from table
select message
from table
where id in (select max(id) from table group by LEAST(sender_id, recipient_id),GREATEST(sender_id, recipient_id))
select s.Name
, sum(nvl(o.Amount, 0)) as Quantity
from Sellers as s,
Orders as o
where s.id = o.Salesperson_id
and o.Order_date is not null
and o.Order_date > to_date('20091231','YYYYMMDD') /*если это date*/
group by s.id, s.Name
having count(o.Salesperson_id) > 1
order by sum(nvl(o.Amount, 0)) desc
offset 0 rows fetch next 1 rows only
SELECT d.dfname,
COUNT(ed.*) edu,
COUNT(case when ed.dfemployee is null then 1 end) notedu,
COUNT(em.*) AS vse
FROM tdepartment d
left join temployee em on (em.dfdepartment = d.dfobj)
left join teducation ed on ( ed.dfemployee = em.dfobj)
group by d.dfname
having edu > notedu
select case when status in ('ACTIVE', 'ONLINE') then 1
when status in ('DISCHARGED', 'WARN', 'ABNORMAL') then 2
end status_type,
Count(*) cnt
from sensor_instances
where groupid = 18
group by status_type
select
case when t1.attr_type = 1 and t1.value_int = 20 then t1.value_int end y,
case when t1.attr_type = 2 and t1.value_int = 4 then t1.value_int end m,
case when t1.attr_type = 3 and t1.value_int = 4 then t1.value_int end d,
case when t1.attr_type = 4 and t1.value_string = 'google' then t1.value_string end utm_source,
k.value_int kind_int,
i.count cnt
from stat_item_attrs k
left join stat_item i on i.id = k.item_id
join stat_item_attrs t1 on t1.item_id = k.item_id and t1.attr_type in (1/*y*/,2/*m*/,3/*d*/4/*utm_source*/) and (t1.value_int in (20, 4 /*20-04-04*/) or t1.value_string = 'google')
where
k.attr_type = 0/*kind*/ and k.value_int in(0,1)/*hosts,leads*/
SELECT source,
AVG(w.lenght) ag,
sum(SUM(h.price)) over (partition by h.source) s
FROM help h left JOIN watch w ON h.user_id=w.user_id and h.film_id=w.film_id
group by source
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)