我正在尝试执行查询以选择属于另一个表中的开始和结束列定义的范围内的所有行。例如,使用伪代码,如果我有这些(非常小的)表:
ranges:
group_id = c("a", "b", "c", "d"),
start = c(1, 7, 2, 25),
end = c(5, 23, 7, 29)
positions:
position = 100 random numbers
annotation = 100 random strings
我想做一个返回类似的查询:
group_id position annotation
a 2 adfkjdas
a 3 sdlfkjasl;kdfj
b 9 sdlfkdj
c 5 wwlekrj
d 27 zxcvzx
使用MariaDB / MySQL,BETWEEN查询将在范围内逐行操作,因此这将起作用:
SELECT
ranges.group_id as group_id,
positions.position as position,
positions.annotation as annotation
FROM
(SELECT * FROM my_ranges) AS ranges, positions
WHERE
positions.position BETWEEN ranges.start AND ranges.end
也就是说,查询就好像WHERE子句实际上是由“OR”连接的一系列WHERE子句,一个用于范围表的每一行(例如,BETWEEN 1和5 OR ORWEEN 7和23 OR BETWEEN 2 AND 7或25和29之间。)
似乎BETWEEN运算符在presto中的行为不同,因此相同的查询不会返回任何结果。
在现实中,我的范围表有大约20,000个我想查询的范围,所以通过编写OR语句来加入它们似乎太过分了......
这里的任何人都可以建议一种方法来修改此查询(或我的一般方法!)以与Presto一起使用吗?
(在评论时添加):对于比伪代码更多的SQL,我想
use tables like this:
CREATE TABLE IF NOT EXISTS `ranges` (
`group_id` char,
`start` int(3),
`end` int(3)
);
INSERT INTO `ranges` (`group_id`, `start`, `end`) VALUES
('a', '2', '5'),
('b', '7', '23'),
('c', '2', '7'),
('d', '25', '29');
CREATE TABLE IF NOT EXISTS `positions` (
`position` int(3),
`annotation` varchar(20)
);
INSERT INTO `positions` (`position`, `annotation`) VALUES
('2', 'adfkjdas'),
('3', 'sdlfkjasl;kdfj'),
('5', 'wwlekrj'),
('9', 'sdlfkdj'),
('27', 'zxcvzx');
运行这样的查询:
SELECT
group_id,
position,
annotation
FROM
ranges, positions
WHERE
positions.position BETWEEN ranges.start AND ranges.end
答案 0 :(得分:2)
以下对我有用。我必须解决end
是保留字的事实:
CREATE EXTERNAL TABLE IF NOT EXISTS ranges (
group_id string,
start_value int,
end_value int
)
ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.OpenCSVSerde'
LOCATION 's3://my-bucket/ranges/';
CREATE EXTERNAL TABLE IF NOT EXISTS positions (
position int,
annotation string
)
ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.OpenCSVSerde'
LOCATION 's3://my-bucket/positions/';
SELECT
group_id,
position,
annotation
FROM
ranges, positions
WHERE
positions.position BETWEEN ranges.start_value AND ranges.end_value;
ranges
和positions
目录包含CSV文件:
a,2,5
b,7,23
c,2,7
d,25,29
和
2,adfkjdas
3,sdlfkjaslkdfj
5,wwlekrj
9,sdlfkdj
27,zxcvzx