如何选择最高总值的对象组

发布于 2025-01-25 21:16:26 字数 1857 浏览 2 评论 0原文

我有三张表格:

对象(numId is primary键

object_category价格1
永久202
借用303
借用10

primary键

is em
colname
numId

借用

(numobj是 emere键/主键参考对象表, collection_name is em efiral键 rector collection表)

numobjcollection_name
1collection_alpha
2collection_betha
3我需要做的查询收集

是:“哪些集合(不包括永久性)的借入对象总成本最高?”

我目前的尝试(无效):

SELECT ColName FROM Colecao
WHERE (
   SELECT MAX((SUM(Price)) FROM Objects AS Num
   JOIN Borrowed ON NumObj = Objects.NumId
   JOIN Collection ON ColName = Collection_name
   WHERE Num > COUNT(NumId) FROM Objects 
   WHERE Object_category = "permanent"
);

它返回消息:“语法错误”

I have three tables like this:

Objects (NumId is Primary key)

NumIdObject_categoryPrice
1permanent20
2borrowed30
3borrowed10

Collection (ColName is Primary Key)

ColName
collection_alpha
collection_betha

Borrowed

(NumObj is Foreign Key/Primary Key referring Object table, and Collection_name is Foreign Key referring Collection table)

NumObjCollection_name
1collection_alpha
2collection_betha
3collection betha

The query I need to do is: "Which collections (excluding the permanent) have the highest total cost of borrowed objects?"

my current attempt (that doesn't work):

SELECT ColName FROM Colecao
WHERE (
   SELECT MAX((SUM(Price)) FROM Objects AS Num
   JOIN Borrowed ON NumObj = Objects.NumId
   JOIN Collection ON ColName = Collection_name
   WHERE Num > COUNT(NumId) FROM Objects 
   WHERE Object_category = "permanent"
);

It returns message: "syntax error"

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

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

发布评论

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

评论(1

兲鉂ぱ嘚淚 2025-02-01 21:16:26

“哪些集合[...]的总成本最高?”

select
    b.Collection_name,
    sum(o.price) Worth
from
    Objects o
    inner join Borrowed b on b.NumObj = o.NumId
where
    o.Object_category = 'borrowed'
group by
    b.Collection_name
order by
    sum(o.price) desc

为您列出所有按借入的对象的总体价值下降的收集列表。您可以添加限制1以显示列表的顶部项目。

但是,想象一下,有两个收藏恰好具有相同的总价值。他们将 都具有“借入对象的最高总成本” ,因此应退还两者。上面具有限制1的查询将省略其中之一。

在这种内等级= 1。

select
    *
from
    (
    select
        *,
        rank() over (order by Worth desc) rnk
    from
        (
        select
            b.Collection_name,
            sum(o.price) Worth
        from
            Objects o
            inner join Borrowed b on b.NumObj = o.NumId
        where
            o.Object_category = 'borrowed'
        group by
            b.Collection_name
        ) collections
    ) collections_ranked
where
    rnk = 1
order by
    Collection_name

或者,我们可以设置 cte 并表达了类似的东西:

with
    collections as (
        select
            b.Collection_name,
            sum(o.price) Worth
        from
            Objects o
            inner join Borrowed b on b.NumObj = o.NumId
        where
            o.Object_category = 'borrowed'
        group by
            b.Collection_name
    )
select
    Collection_name,
    Worth
from
    collections
where
    Worth = (select max(worth) from collections)
order by
    Collection_name

"Which collections [...] have the highest total cost of borrowed objects?"

select
    b.Collection_name,
    sum(o.price) Worth
from
    Objects o
    inner join Borrowed b on b.NumObj = o.NumId
where
    o.Object_category = 'borrowed'
group by
    b.Collection_name
order by
    sum(o.price) desc

gives you a list of all collections ordered descending by the overall worth of the borrowed objects. You could add limit 1 to show the top item of the list.

But imagine there are two collections that happen to have the same total worth. They would both have "the highest total cost of borrowed objects", so both should be returned. The above query with limit 1 would omit one of them.

In this case we could rank the collections according to their worth, and return all rows with rank = 1.

select
    *
from
    (
    select
        *,
        rank() over (order by Worth desc) rnk
    from
        (
        select
            b.Collection_name,
            sum(o.price) Worth
        from
            Objects o
            inner join Borrowed b on b.NumObj = o.NumId
        where
            o.Object_category = 'borrowed'
        group by
            b.Collection_name
        ) collections
    ) collections_ranked
where
    rnk = 1
order by
    Collection_name

Alternatively, we could set up a CTE and express the same thing like this:

with
    collections as (
        select
            b.Collection_name,
            sum(o.price) Worth
        from
            Objects o
            inner join Borrowed b on b.NumObj = o.NumId
        where
            o.Object_category = 'borrowed'
        group by
            b.Collection_name
    )
select
    Collection_name,
    Worth
from
    collections
where
    Worth = (select max(worth) from collections)
order by
    Collection_name
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文