简体   繁体   中英

is there a optimized/better way to write this query?

I have below query and wanted to know if this can be re-written in a better way?

SELECT COL1, COL2 FROM TABLE1 WHERE ID = 1 and COL4 = 1415 AND COL3 IN 
(SELECT MAX(COL3) FROM TABLE1 WHERE PRI = ID = 1 and COL4 = 1415);

The question arises from the fact that filters ID and Col4 in where clause of subquery are same as the filters in the main query.

You can use:

    SELECT COL1, COL2,MAX(COL3) as mx FROM TABLE1 WHERE ID = 1 
     and COL4 = 1415  having  mx=MAX(COL3);

This will avoid the extra sub-query.

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-2025 STACKOOM.COM