【发布时间】:2021-04-20 07:47:53
【问题描述】:
我正在将下面的 BigQuery 源代码翻译成 MySQL。
问题在于GENERATE_ARRAY,产生了一个可变长度数组,这对于MySQL 来说是不可接受的。
我想我需要使用WITH array AS ( ... ) 制作一个虚拟表,但这似乎并不容易,因为数组的大小不固定。
有什么应对办法吗?
The table used looks like this→
-- This query generates a row for every hour the patient is in the ICU.
-- The hours are based on clock-hours (i.e. 02:00, 03:00).
-- The hour clock starts 24 hours before the first heart rate measurement.
-- Note that the time of the first heart rate measurement is ceilinged to the hour.
-- this query extracts the cohort and every possible hour they were in the ICU
-- this table can be to other tables on ICUSTAY_ID and (ENDTIME - 1 hour,ENDTIME]
-- get first/last measurement time
CREATE TABLE IF NOT EXISTS icustay_hourly(
with all_hours as
(
select
it.stay_id
-- ceiling the intime to the nearest hour by adding 59 minutes then truncating
-- note that we truncate by parsing as string, rather than using DATETIME_TRUNC
-- this is done to enable compatibility with psql
, PARSE_DATETIME(
'%Y-%m-%d %H:00:00',
FORMAT_DATETIME(
'%Y-%m-%d %H:00:00',
DATE_ADD(it.intime_hr, INTERVAL '59' MINUTE)
)) AS endtime
-- create integers for each charttime in hours from admission
-- so 0 is admission time, 1 is one hour after admission, etc, up to ICU disch
-- we allow 24 hours before ICU admission (to grab labs before admit)
, GENERATE_ARRAY(-24, CEIL(TIMESTAMP_DIFF(HOUR, it.outtime_hr, it.intime_hr))) as hrs
from mimic_derived.icustay_times it
)
SELECT stay_id
, CAST(hr AS BIGINT) as hr
, DATE_ADD(endtime, INTERVAL CAST(hr AS BIGINT) HOUR) as endtime
FROM all_hours
CROSS JOIN UNNEST(all_hours.hrs) AS hr
);
【问题讨论】:
-
大部分代码与 MySQL 无关。你应该问一个新的问题。提供样本数据、期望的结果以及您想要实现的逻辑的清晰解释。您现有的代码可以作为参考。
标签: mysql sql google-bigquery