select distinct entity_id, first_value(text) over (partition by entity_id order by language_id) fv, lang_id
from tableA b
cross join
(/*select 1 lang_id union all*/
select 2 lang_id union all
select 3 lang_id union all
select 4 lang_id) lang
order by entity_id, lang_id
with prepod as ( select 'a' name, STR_TO_DATE('2013-02-11', '%Y-%m-%d') date_r union all
select 'aa' name, STR_TO_DATE('2010-09-01', '%Y-%m-%d') date_r union all
select 'b' name, STR_TO_DATE('1968-02-11', '%Y-%m-%d') date_r union all
select 'bb' name, STR_TO_DATE('1969-01-21', '%Y-%m-%d') date_r union all
select 'c' name, STR_TO_DATE('1980-02-11', '%Y-%m-%d') date_r union all
select 'd' name, STR_TO_DATE('1989-02-11', '%Y-%m-%d') date_r union all
select 'z' name, null date_r -- препод, у которого нет данных др.
)
select inters.title, count(prepod.name) cnt
from
(
select null begin, 1969 end, 'ранее 1970' title, 5 ord union all
select 1970 begin, 1979 end, '1970 - 1979' title, 4 ord union all
select 1980 begin, 1989 end, '1980 - 1989' title, 3 ord union all
select 1990 begin, 1999 end, '1990 - 1999' title, 2 ord union all
select 2000 begin, null end, 'от 2000' title, 1 ord union all
select null begin, null end, 'нет данных' title, 6 ord -- запись для тех, у кого нет данных др.
) inters -- таблица с интервалами
left join prepod on inters.begin <= year(prepod.date_r) and inters.end >= year(prepod.date_r)
or inters.begin is null and inters.end >= year(prepod.date_r)
or inters.begin <= year(prepod.date_r) and inters.end is null
or prepod.date_r is null and inters.begin is null and inters.end is null
group by inters.title, inters.ord
order by inters.ord, inters.title
select y, m, d, sum(hosts_count) hosts_count, sum(leads_count) leads_count, sum(leads_amount) leads_amount
from ( select
max(case when a.attr_type = 1 then a.value_int end) y,
max(case when a.attr_type = 2 then a.value_int end) m,
max(case when a.attr_type = 3 then a.value_int end) d,
sum(case when a.attr_type = 0 and a.value_int = 0 then i.count end) hosts_count,
sum(case when a.attr_type = 0 and a.value_int = 1 then i.count end) leads_count,
sum(case when a.attr_type = 0 and a.value_int = 1 then i.amount end) leads_amount
from stat_item i
inner join stat_item_attrs a on
a.item_id = i.id and (
(a.attr_type = 0 and a.value_int in (0,1))
or
a.attr_type in (1, 2,3)
)
group by i.id) t1
group by y,m,d
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 COUNT(ed.*) > COUNT(case when ed.dfemployee is null then 1 end)
SELECT * FROM `table`
WHERE 1 = 1
and `column1` LIKE '%word1%'
and `column1` LIKE '%word2%'
and `column1` LIKE '%word3%'
and `column1` LIKE '%word4%'
and `column1` LIKE '%word5%'
and `column1` LIKE '%word5%'
select t1.Class_type, t1.cnt, t2.Result_
from (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(*) cnt
from class c
group by Class_type) t1,
(select case when id in (1,2,3,4,5) then 'Начальная школа'
when id in (6,7,8,9) then 'Средняя школа'
when id in (10,11) then 'Старшая школа'
end Class_type, sum(case when s.ocenka>4 then 1 end as) Result_
from class c
group by Class_type) t2
where t1.Class_type = t2.Class_type
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'
Вот этот кусок отключает уже заполненные
where not exists (select 1 from tableA where entity_id = f1 and language_id = f2 and text is not null)
но может сработать и ON DUPLICATE KEY UPDATE text = coalesce(text,fv)
проверять чесно лень =)