не группирует по entities.id. Почему?
SELECT `date`, COUNT(*) AS cnt, 'tot_user' as user FROM `l_sites` GROUP BY `date`
union all
SELECT `date`, COUNT(*) AS cnt, 'tot_user2' as user FROM `l_users` GROUP BY `date`
union all
SELECT `date`, COUNT(*) AS cnt, 'tot_user3' as user FROM `l_leads` GROUP BY `date`
SELECT case when CHARACTER_LENGTH(TEXT) - CHARACTER_LENGTH(REPLACE(TEXT, '"', '')) = 2
then SUBSTRING(TEXT FROM LOCATE('"', TEXT)+1 FOR CHARACTER_LENGTH(TEXT) - LOCATE('"', TEXT)-1)
when CHARACTER_LENGTH(TEXT) - CHARACTER_LENGTH(REPLACE(TEXT, '"', '')) = 3
then SUBSTRING(TEXT FROM LOCATE('"', TEXT,LOCATE('"', TEXT)+1)+1 FOR CHARACTER_LENGTH(TEXT)-LOCATE('"', TEXT,LOCATE('"', TEXT)+1)-1 )
when CHARACTER_LENGTH(TEXT) - CHARACTER_LENGTH(REPLACE(TEXT, '"', '')) = 4
then SUBSTRING(TEXT FROM LOCATE('"', TEXT,LOCATE('"', TEXT)+1)+1 FOR CHARACTER_LENGTH(TEXT)-LOCATE('"', TEXT, LOCATE('"', TEXT,LOCATE('"', TEXT)+1)+1)-2)
else null
end
FROM mytable
select case when CHARACTER_LENGTH(TEXT) - CHARACTER_LENGTH(REPLACE(TEXT, '"', '')) = 2
then SUBSTRING_INDEX(SUBSTRING_INDEX(TEXT, '"', 2), '"', -1)
when CHARACTER_LENGTH(TEXT) - CHARACTER_LENGTH(REPLACE(TEXT, '"', '')) > 2
then SUBSTRING_INDEX(SUBSTRING_INDEX(TEXT, '"', 3), '"', -1)
else null
end
FROM mytable
SELECT T.YEAR,
SUM (CASE WHEN T.W = 1 THEN 1 ELSE 0 END) AS MEN,
SUM (CASE WHEN T.W = 2 THEN 1 ELSE 0 END) AS WOMEN
FROM (SELECT (YEAR(CURRENT_DATE) - YEAR(F.DR)) DIV 10 * 10 AS YEAR, F.W
FROM DB_PERSON AS P LEFT JOIN DB_PERSON_FIO AS F ON P.ID = F.ID_PERSON
WHERE P.STAT > 0) AS T
GROUP BY T.YEAR
ORDER BY T.YEAR;
SELECT T.YEAR,
SUM (CASE WHEN T.W = 1 THEN 1 ELSE 0 END) AS MEN,
SUM (CASE WHEN T.W = 2 THEN 1 ELSE 0 END) AS WOMEN
FROM (SELECT (DATE_FORMAT(FROM_DAYS(TO_DAYS(CURRENT_DATE) - TO_DAYS(F.DR)), '%Y') + 0) DIV 10 * 10 AS YEAR,
F.W
FROM DB_PERSON AS P LEFT JOIN DB_PERSON_FIO AS F ON P.ID = F.ID_PERSON
WHERE P.STAT > 0) AS T
GROUP BY T.YEAR
ORDER BY T.YEAR;