SQL Max 按查询分组帮助
我有一个简单的问题。如何在一个查询中选择我需要的两个值?目前我正在这样做,效果很好,但显然在一个应该解决问题的时候运行了两个查询。我尝试了 MAX(columnA) 和 GROUP BY ColumnB,但返回多行。我只想返回一行。
DECLARE @biID bigint
, @dtThreshold DateTime
SELECT @biID = MAX(biID)
FROM tbPricingCalculationCount WITH (NOLOCK)
SELECT @dtThreshold = dtDateTime
FROM tbPricingCalculationCount WITH (NOLOCK)
WHERE biID = @biID
我希望在一个查询中正确设置这两个变量。我怎样才能做到这一点?
谢谢, 〜ck
I have a quick question. How do I select the two values I need in one query? Currently I'm doing this, which works fine, but it's obviously running two queries when one should do the trick. I tried MAX(columnA) and GROUP BY ColumnB, but that returns multiple row. I only want one row returned.
DECLARE @biID bigint
, @dtThreshold DateTime
SELECT @biID = MAX(biID)
FROM tbPricingCalculationCount WITH (NOLOCK)
SELECT @dtThreshold = dtDateTime
FROM tbPricingCalculationCount WITH (NOLOCK)
WHERE biID = @biID
I would like both those variables to be set correctly in one query. How can I do that?
Thanks,
~ck
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你能不这样做吗?
can you not just do this?
怎么样:
如果您不在其他地方使用 biID,您甚至可以将其修剪为:
How about:
If you're not using the biID elsewhere, you can even trim it to:
怎么样
How about
这将返回具有最大 biID 的行的 dtDateTime:
如果多个行共享相同的“最大”biID,则需要使用 TOP 将结果限制为 1:
This returns dtDateTime for the row with the largest biID:
If more than one row shares the same "largest" biID, then you need to limit the results to one using TOP: