简体   繁体   中英

Combine two SQL queries into one query for the same table

I have a table called COMPUTED_DATA . It contains 3 columns: data, cluster, and last_execution.

There is job which runs every 2 weeks which inserts multiple data for a cluster and its last_execution time.

My requirement is to get the data for a cluster for its most recent last_execution time. 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

data = SELECT data FROM COMPUTED_DATA WHERE cluster=1204 AND
last_execution={last_execution}

This query uses that last_execution to get the data.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM