Как сделать дело в HANA
У меня есть следующий запрос в Postgres, который дает диапазон метров и количество транспортных средств, пройденных между диапазоном
SELECT (CASE WHEN meter >= 0 AND meter< 10000 THEN '0-10'
WHEN meter >= 10000 AND meter < 20000 THEN '10-20'
WHEN meter >= 20000 AND meter <30000 THEN '20-30'
WHEN meter >= 30000 AND meter < 40000 THEN '30-40'
WHEN meter >= 40000 AND meter < 50000 THEN '40-50'
WHEN meter >= 50000 AND meter < 60000 THEN '50-60'
WHEN meter >= 60000 AND meter <70000 THEN '60-70'
WHEN meter >= 70000 AND meter < 80000 THEN '70-80'
WHEN meter >= 80000 AND meter < 100000 THEN '80-90'
WHEN meter >=100000 THEN 'above 100'
END) as range,count(*) as vehicle_count
from (
SELECT extract (day from time) as day, place,vehicle_id,sum(meter_two_points) as meter
FROM public.datatable where time >= '2015-09-05 00:00:00' and time <= '2015-09-05 23:00:00' and place=10
group by day,vehicle_id,place
order by day,vehicle_id
) as A group by range order by range limit 10
Теперь я хочу сделать тот же запрос для той же таблицы в HANA. Так я пытался с
SELECT (CASE WHEN meter >= 0 AND meter< 10000 THEN '0-10'
WHEN meter >= 10000 AND meter < 20000 THEN '10-20'
WHEN meter >= 20000 AND meter <30000 THEN '20-30'
WHEN meter >= 30000 AND meter < 40000 THEN '30-40'
WHEN meter >= 40000 AND meter < 50000 THEN '40-50'
WHEN meter >= 50000 AND meter < 60000 THEN '50-60'
WHEN meter >= 60000 AND meter <70000 THEN '60-70'
WHEN meter >= 70000 AND meter < 80000 THEN '70-80'
WHEN meter >= 80000 AND meter < 100000 THEN '80-90'
WHEN meter >=100000 THEN 'above 100'
END) as range,count(*) as vehicle_count
from (
But it throws with the error `invalid column name range`
SELECT to_integer(to_varchar(time,'DD')) as day, place,vehicle_id,sum(meter_two_points) as meter
FROM public.datatable where time >= '2015-09-05 00:00:00' and time <= '2015-09-05 23:00:00' and place=10
group by day,vehicle_id,place
order by day,vehicle_id
) as A group by range order by range limit 10
2 ответа
Решение
Вы можете изменить это:
SELECT range,
COUNT(*) AS vehicle_count
FROM (
SELECT (CASE WHEN meter >= 0 AND meter< 10000 THEN '0-10'
WHEN meter >= 10000 AND meter < 20000 THEN '10-20'
WHEN meter >= 20000 AND meter <30000 THEN '20-30'
WHEN meter >= 30000 AND meter < 40000 THEN '30-40'
WHEN meter >= 40000 AND meter < 50000 THEN '40-50'
WHEN meter >= 50000 AND meter < 60000 THEN '50-60'
WHEN meter >= 60000 AND meter <70000 THEN '60-70'
WHEN meter >= 70000 AND meter < 80000 THEN '70-80'
WHEN meter >= 80000 AND meter < 100000 THEN '80-90'
WHEN meter >=100000 THEN 'above 100'
END) AS range
FROM (
SELECT to_integer(to_varchar(time,'DDMMYYYY')) AS day,
--You should considering using other way to truncate date from time,
--not convert too much,
--at least you could use only to_varchar is enough
place,
vehicle_id,
SUM(meter_two_points) AS meter
FROM public.datatable
WHERE time >= '2015-09-05 00:00:00'
AND time <= '2015-09-05 23:00:00'
AND place=10
GROUP BY to_integer(to_varchar(time,'DDMMYYYY')),
vehicle_id,
place
)
) AS a
GROUP BY range
ORDER BY range
LIMIT 10; --why limit 10 here while you only have 10 ranges?
И как порядок операций SQL, я продолжаю задаваться вопросом, может ли PostgreSQL GROUP BY
после SELECT
так что group by может сгруппироваться по столбцу range в вашем первом запросе.
Вот пример CASE в предложениях SELECT, взятый из статьи «Declaraciones CASE en consultas OPEN SQL usando ABAP 7.4» на ABAP.ar:
DATA: ls_vbak TYPE vbak,
ld_vbeln LIKE vbak-vbeln.
PARAMETERS: p_vbeln like vbak-vbeln.
CONSTANTS: lc_name1(5) TYPE c VALUE 'name1',
lc_name2(5) TYPE c VALUE 'name2',
lc_name3(5) TYPE c VALUE 'name3'.
ld_vbeln = p_vbeln.
SELECT vbeln, vbtyp,
CASE
WHEN auart = 'ZAMA' THEN @lc_name1
WHEN auart = 'ZACR' THEN @lc_name2
ELSE @lc_name3
END AS ernam
FROM vbak
WHERE vbeln = @ld_vbeln
INTO CORRESPONDING FIELDS of @ls_vbak.
ENDSELECT.
SELECT vbeln, vbtyp,
CASE
WHEN auart = 'ZAMA' THEN @lc_name1
WHEN auart = 'ZACR' THEN @lc_name2
ELSE @lc_name3
END AS ernam
FROM vbak
WHERE vbeln = @ld_vbeln
INTO CORRESPONDING FIELDS of @ls_vbak.
ENDSELECT