简体   繁体   English

SQL子查询-选择具有最大JobValue的JobID

[英]SQL Sub-query — Select JobID with a maximum JobValue

This seems like an easy thing, but I'm drawing a blank. 这似乎是一件容易的事,但我正在空白。

Select * from 
....
inner join 
  (
   select JobsID, Value from Jobs where Value **is the highest**
  ) as MaxJob on MaxJob.CustID = A.CustID

inner join
  (
   select other information based upon MaxJob.JobID
  ) as OtherStuff

Is there a nice way to have that first subquery give me the Job ID of the job with the maximum Value? 有什么好方法让第一个子查询为我提供具有最大值的作业的作业ID?

Thanks... this seems easy and I'm sure I'm overlooking something very elementary. 谢谢...这似乎很简单,我敢肯定我忽略了一些非常基本的内容。 One of those days... 那些日子中的一天...

Edit: Due to this question being a bit ambiguous, I've written up a much more detailed question here (since this question was answered correctly). 编辑:由于这个问题是有点暧昧,我已经写了一个更详细的问题在这里 (因为这个问题被回答正确)。

If you want the single JobId with the highest Value: 如果您希望单个JobId具有最高的价值:

SELECT JobId FROM Jobs WHERE Value = SELECT MAX(Value) FROM Jobs

But, that may give you multiple JobIds if thay all have the same Value. 但是,如果它们都具有相同的值,则可能会给您多个JobId。 So, assuming you don't want that, I'd probably do: 因此,假设您不希望这样做,我可能会这样做:

SELECT MAX(JobId) as JobId FROM Jobs WHERE Value = SELECT MAX(Value) FROM Jobs
Select top 1 JobId, Value from Jobs order by Value desc

这可能会导致性能比max(Value)差,但是代码更少

Yes, you're overlooking something: the excellent new language features in SQL Server 2005 and later! 是的,您忽略了一些东西:SQL Server 2005和更高版本中出色的新语言功能! Particularly the analytic functions like row_number() and the very cool CROSS APPLY join. 特别是像row_number()这样的分析函数和非常酷的CROSS APPLY联接在一起。

row_number solves the first question (choosing "the highest" something for a given other thing): row_number解决了第一个问题(为给定的其他东西选择“最高”的东西):

with Ranked(a,b,c,d,rk) as (
  select T1.a,T2.b,T2.c,T2.d,
    row_number() over (
      partition by T1.a
      order by T2.x desc
    )
  from T1 join T2 on someCondition
)
  select a,b,c,d
  from Ranked
  where rk = 1;

CROSS APPLY solves the second question - creating a table source "based on MaxJob.JobID": CROSS APPLY解决了第二个问题-“基于MaxJob.JobID”创建表源:

select *
from tableOrJoin
cross apply (
  select stuff
  from elsewhere
  where something = tableOrJoin.JobID
) as A

In other words, it allows you to have a "correlated join" by using a column value from the left hand table source in the definition of the right-hand table source. 换句话说,它允许您在右侧表源的定义中使用左侧表源中的列值进行“关联联接”。

If you have a specific question, please give more specific information. 如果您有特定问题,请提供更多具体信息。 I suspect you may be able to use both of these new features in your solution. 我怀疑您可能可以在解决方案中同时使用这两个新功能。

select max(JobId), value from jobs where...

编辑:我不好我读错了问题,这可能会工作

select jobid, max(value) from jobs....

SELECT j.JOBID, MAX(Value) OVER(order by j.Value PARTITION by j.JOBID) as max_val, s.foobar FROM Jobs j INNER JOIN SomeOtherTable s ON (s.jobid = j.jobid) WHERE booya = win 从作业j INNER JOIN SomeOtherTable s ON(s.jobid = j.jobid)中选择j.JOBID,MAX(Value)OVER(按j.Value PARTITION由j.JOBID排序)作为max_val,s.foobar

I suspect that might make no sense, cause I don't know your tables :D 我怀疑这可能毫无意义,因为我不知道您的表:D

But LEARN THE POWER OF OVER AND PARTITION. 但是,学习重叠和分区的能力。 LEARN IT, LOVE IT. 学习,爱它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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