[英]Combine two SQL queries into one query for the same table
I have a table called COMPUTED_DATA .我有一个名为COMPUTED_DATA的表。 It contains 3 columns: data, cluster, and last_execution.
它包含 3 列:数据、集群和 last_execution。
There is job which runs every 2 weeks which inserts multiple data for a cluster and its last_execution time.有一个作业每 2 周运行一次,它为集群插入多个数据及其 last_execution 时间。
My requirement is to get the data for a cluster for its most recent last_execution time.我的要求是获取集群最近 last_execution 时间的数据。 Currently I have written query like this in my code
目前我已经在我的代码中写了这样的查询
last_execution = SELECT distinct(last_execution) FROM COMPUTED_DATA
WHERE cluster=1204 AND ORDER BY last_execution DESC limit 1
The above query gets me the most recent last_execution上面的查询让我得到了最近的 last_execution
data = SELECT data FROM COMPUTED_DATA WHERE cluster=1204 AND
last_execution={last_execution}
This query uses that last_execution to get the data.此查询使用 last_execution 来获取数据。
My question is can this be combined into just 1 query.我的问题是这可以组合成一个查询。 I am running this in my spark cluster so each SQL query is very time expensive.
我在我的 spark 集群中运行它,所以每个 SQL 查询都非常耗时。 Hence I want to combine this into one query.
因此,我想将其合并到一个查询中。 Please help.
请帮忙。
EDIT: the second query where I am getting data from returns multiple rows.编辑:我从中获取数据的第二个查询返回多行。 Hence I cannot use limit on the second query because that count is unknown.
因此我不能对第二个查询使用限制,因为该计数是未知的。
Yes it can是的,它可以
SELECT
data
FROM
COMPUTED_DATA
WHERE
cluster = 1204 and last_execution=(SELECT distinct(last_execution) FROM COMPUTED_DATA
WHERE cluster=1204 AND ORDER BY last_execution DESC limit 1)
This isn't the most beautiful way to write this, but you get idea how to use subquery in where clause.这不是写这个的最漂亮的方式,但你知道如何在 where 子句中使用子查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.