sql server 没有子查询的最大值计数

发布于 2024-07-26 19:25:40 字数 1225 浏览 5 评论 0原文

我想编写一个 T-SQL 查询,它不仅返回最大值,而且返回具有最大值的行数。 一定有比我想出的更好的方法

 --wrong way 
 select LibraryBranchId, max(daysCheckedOut), count(daysCheckedOut) 
 from books group by LibraryBranchId

 LibraryBranchId   Expr1      Expr2
 ----------------------------------
 1                 100       398503  (WRONG!)
 2                 75         94303  (WRONG!)
 3                 120       103950  (WRONG!)

我可以通过 INNER JOINing 子查询正确地做到这一点,但这似乎很浪费

 --right way, but seems WAY too long
 select LibraryBranchId,max(daysCheckedOut),count(daysCheckedOut)
 from books inner join 
   ( select LibraryBranchId, max(daysCheckedOut) as maxDaysCheckedOut
     from books group by LibraryBranchId ) as maxbooks 
 on books.LibraryBranchId=maxbooks.LibraryBranchId
 where daysCheckedOut=maxDaysCheckedOut
 group by LibraryBranchId 

 LibraryBranchId   Expr1      Expr2
 ----------------------------------
 1                 100           17  (RIGHT!)
 2                 75            11  (RIGHT!)
 3                 120            2  (RIGHT!)

所以有没有一种方法像查询#1一样简单,但返回正确的结果,如查询#1 2?

MS SQL Server 2000

编辑:在我第一次尝试输入此内容时,我错过了上面两个重要的 GROUP BY,我已添加它们 编辑:假装凯德·鲁克斯写的版本就是我写的

I want to write a T-SQL query which returns not only the maximum value, but the number of rows having the maximum value. There must be a better way than what I have come up with

 --wrong way 
 select LibraryBranchId, max(daysCheckedOut), count(daysCheckedOut) 
 from books group by LibraryBranchId

 LibraryBranchId   Expr1      Expr2
 ----------------------------------
 1                 100       398503  (WRONG!)
 2                 75         94303  (WRONG!)
 3                 120       103950  (WRONG!)

I can do this correctly by INNER JOINing a subquery but it seems wasteful

 --right way, but seems WAY too long
 select LibraryBranchId,max(daysCheckedOut),count(daysCheckedOut)
 from books inner join 
   ( select LibraryBranchId, max(daysCheckedOut) as maxDaysCheckedOut
     from books group by LibraryBranchId ) as maxbooks 
 on books.LibraryBranchId=maxbooks.LibraryBranchId
 where daysCheckedOut=maxDaysCheckedOut
 group by LibraryBranchId 

 LibraryBranchId   Expr1      Expr2
 ----------------------------------
 1                 100           17  (RIGHT!)
 2                 75            11  (RIGHT!)
 3                 120            2  (RIGHT!)

So is there a way that is as simple as query #1, but returns the correct result as in query #2?

MS SQL Server 2000

EDIT: I missed two important GROUP BYs above on my first try at entering this, I have added them
EDIT: Pretend that the version that Cade Roux wrote is what I wrote

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

青春如此纠结 2024-08-02 19:25:40

我认为这是正确的:

SELECT maxbooks.LibraryBranchId, maxbooks.maxDaysCheckedOut, count(*)
FROM books
INNER JOIN (
    SELECT LibraryBranchId, max(daysCheckedOut) AS maxDaysCheckedOut
    FROM books
    GROUP BY LibraryBranchId
) AS maxbooks
    ON books.LibraryBranchId = maxbooks.LibraryBranchId
    AND books.daysCheckedOut = maxbooks.maxDaysCheckedOut
GROUP BY maxbooks.LibraryBranchId, maxbooks.maxDaysCheckedOut

我认为没有更简单的方法 - 从概念上讲,它是两个集合的交集。 关于分支的元组集合和满足该条件的元组集合。

I think that's right:

SELECT maxbooks.LibraryBranchId, maxbooks.maxDaysCheckedOut, count(*)
FROM books
INNER JOIN (
    SELECT LibraryBranchId, max(daysCheckedOut) AS maxDaysCheckedOut
    FROM books
    GROUP BY LibraryBranchId
) AS maxbooks
    ON books.LibraryBranchId = maxbooks.LibraryBranchId
    AND books.daysCheckedOut = maxbooks.maxDaysCheckedOut
GROUP BY maxbooks.LibraryBranchId, maxbooks.maxDaysCheckedOut

I don't think there's a simpler way - conceptually, it's the intersection of two sets. The set of tuples about the branches and the set of tupes satisfying that.

童话里做英雄 2024-08-02 19:25:40

最简单的方法是获取您的 LibraryBranchId 并选择您的签出日期
然后无论您在何处编写代码,都以编程方式对它们进行计数,并以编程方式获取最大天数。

The easiest way to do this is to get your LibraryBranchId's and select your dayscheckedout
then count them programmatically wherever you write the code and get the max dayscheckedout programmatically also.

尸血腥色 2024-08-02 19:25:40

这个怎么样?

select LibraryBranchId, MAX(daysCheckedOut), count(daysCheckedOut)
from books B
where daysCheckedOut = (select MAX(daysCheckedOut) from books where LibraryBranchID = B.LibraryBranchID)
group by LibraryBranchId 

How about this?

select LibraryBranchId, MAX(daysCheckedOut), count(daysCheckedOut)
from books B
where daysCheckedOut = (select MAX(daysCheckedOut) from books where LibraryBranchID = B.LibraryBranchID)
group by LibraryBranchId 
沉鱼一梦 2024-08-02 19:25:40

另一种“浪费”的方式:

select LibraryBranchId, avg(daysCheckedOut) as maxDaysCheckedOut, count(*)
from
(
    select LibraryBranchId, daysCheckedOut
    from books b1
    where not exists 
    (
        select *
        from books b2
        where b2.LibraryBranchId = b1.LibraryBranchId
        and b2.daysCheckedOut > b1.daysCheckedOut
     )
) t
group by LibraryBranchId

Another "wasteful" way:

select LibraryBranchId, avg(daysCheckedOut) as maxDaysCheckedOut, count(*)
from
(
    select LibraryBranchId, daysCheckedOut
    from books b1
    where not exists 
    (
        select *
        from books b2
        where b2.LibraryBranchId = b1.LibraryBranchId
        and b2.daysCheckedOut > b1.daysCheckedOut
     )
) t
group by LibraryBranchId
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文