获取2个值之间的所有整数/日期

时间:2016-12-23 20:32:30

标签: sql google-bigquery

我有下表:

col val1 val2
A   1    6
B   44   46
C   2    5

我怎样才能得到以下输出:

col val
A   1
A   2
A   3
A   4
A   5
A   6
B   44
B   45
B   46
C   2
C   3
C   4
C   5

这是val1和val2之间的col和val的组合?

2 个答案:

答案 0 :(得分:3)

#standardSql
WITH YourTable AS (
  SELECT 'A' AS col, 1 AS val1,  6 AS val2 UNION ALL
  SELECT 'B' AS col, 44 AS val1, 46 AS val2 UNION ALL
  SELECT 'C' AS col, 2 AS val1,  5 AS val2 
)
SELECT col, val
FROM YourTable, UNNEST(GENERATE_ARRAY(val1, val2)) AS val
ORDER BY col, val

答案 1 :(得分:0)

我使用了一个静态公共表来保存连接数。你可以自己生成一个。此外,我必须模仿你的桌子,但你可以用一个简单的电话代替它。

#standardSql
select col,n.i from (
select * from
(select 'A' as col,   1 as val1,    6 as val2) union all 
(select 'B' as col,   44 as val1,    46 as val2) union all
(select 'C' as col,   2 as val1,    5 as val2)) d
join `fh-bigquery.public_dump.numbers_255` n on n.i>=val1 and n.i<=val2
order by 1,2