[Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 261
  • Last Modified:

Simplify a SQL query

I am currently using 2 Queries to get to the value i am looking for and  am wondering if i could do this in one.

My table has 2 Col's of intrest lets called them ColA and ColB

ColA        ColB
10001        50
10008        50
10014        50
10002        51
10009        51
10015        51
10003        52
10009        52

and so on, i have the value of ColA lets say 10002 and i need to find the largest value
in my table for ColA where ColB = the same as for the row of 10002 which is 51 in the
sample case.

Right now i query for Colb based on 10002 and then in a second query i select max of
colA where colB = the value i got infirst query.

Any ideas to do this in one query would be great
  • 2
2 Solutions
Please show us the query you are using now.
Is this what you're doing now:
select ColA,ColB from myTable where ColB=(select ColB from myTable where ColA=1002)?
select max(ColA)
from myTable
where ColB = (select top 1 ColB from myTable where ColA = 10002 )
Select max(ColA)  from YourTable
where ColB =
(Select ColB from  YourTable where ColA= 10002)

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now