【问题标题】:How to create (multiple) Ranks in 1 Query by cluster?如何按集群在 1 个查询中创建(多个)排名?
【发布时间】:2021-06-18 17:59:43
【问题描述】:

[在此处输入图片描述][1]

我有一个关于在不同国家销售的不同“集群”产品的查询。

  • 集群 1:计算机-键盘-鼠标
  • 集群 2:PlayStation-Xbox-Nintendo
  • 集群 3:Macbook-Lenovo-hp

每个集群在 >= 5 个国家/地区销售。

我需要为国家 by 集群创建单独的排名。 如何告诉我的查询按集群创建这些多个排名?

这是我尝试过的:

SELECT [Abfrage 10_Profit].[product 1]
     , [Abfrage 10_Profit].[product 2]
     , [Abfrage 10_Profit].[product 3]
     , [Abfrage 10_Profit].[Country]
     , [Abfrage 10_Profit].Profit
     , [Country] & [Profit] AS [CountryProfit]
     , (SELECT COUNT([Abfrage 10_Profit].[Country]) 
        FROM [Abfrage 10_Profit] as [CountryProfit]
        WHERE [Country] = [Abfrage 10_Profit].[Country] 
          AND Profit > [Abfrage 10_Profit].Profit)+1 AS Rank
FROM [Abfrage 10_Profit]

但是查询不去,它永远收费。 然后我尝试了:

SELECT [Abfrage 10_Profit].[Product 1]
     , [Abfrage 10_Profit].[Product 2]
     , [Abfrage 10_Profit].[Product 3]
     , [Abfrage 10_Profit].[Country]
     , [Abfrage 10_Profit].Profit
     , (SELECT COUNT([Abfrage 10_Profit].[Country])
        FROM [Abfrage 10_Profit]
        WHERE [Abfrage 10_Profit].[Country] = [Abfrage 10_Profit].[Country] 
          AND [Abfrage 10_Profit].Profit >= [Abfrage 10_Profit].Profit) AS Rank 
FROM [Abfrage 10_Profit] 
ORDER BY [Abfrage 10_Profit].[Country] DESC; 

通过这样做,我在所有排名行中获得 10604(10604 = 我拥有的总行数)我无法在组内获得多个排名。我该如何解决这个问题?

非常感谢!

[![查询输出][2]][2]

【问题讨论】:

  • 请发布您迄今为止尝试过的内容,以及可以复制和粘贴的示例(即不是图像)。
  • 含:SELECT [Abfrage 10_Profit].[Product 1], [Abfrage 10_Profit].[Product 2], [Abfrage 10_Profit].[Product 3], [Abfrage 10_Profit].[Country], [Abfrage 10_Profit].Profit, (SELECT COUNT([Abfrage 10_Profit].[Country]) FROM [Abfrage 10_Profit] WHERE [Abfrage 10_Profit].[Country] = [Abfrage 10_Profit].[Country] AND [Abfrage 10_Profit].Profit >= [Abfrage 10_Profit].Profit) AS Rank FROM [Abfrage 10_Profit] ORDER BY [Abfrage 10_Profit].[Country] DESC;我在排名中获得 10604(我拥有的总行数)我无法在组内获得多个排名。我该如何解决这个问题?
  • 这能回答你的问题吗? Use Access SQL to do a grouped ranking
  • 应该编辑问题以将示例数据显示为文本表,而不是图像以及尝试的 SQL。
  • 不幸的是,作为新成员,我无法插入表格,所以我不得不尝试制作它。我使用的 SQL 在评论中,但我可以将其复制粘贴到问题中。

标签: sql ms-access rank


【解决方案1】:

只需在相关聚合子查询中为每个集群添加WHERE条件。此外,在使用子查询时,请务必使用表别名来有效区分使用相同源的内部和外部查询(即[Abfrage 10_Profit])。

SELECT ap.[Product 1]
     , ap.[Product 2]
     , ap.[Product 3]
     , ap.[Country]
     , ap.Profit
     , (SELECT COUNT(ap.[Country])
        FROM [Abfrage 10_Profit] sub_ap
        WHERE sub_ap.Profit >= ap.Profit
          AND sub_ap.[Product 1] = ap.[Product 1]
          AND sub_ap.[Product 2] = ap.[Product 2]
          AND sub_ap.[Product 3] = ap.[Product 3]
          AND sub_ap.[Country] = ap.[Country]) AS Rank 
FROM [Abfrage 10_Profit] ap
ORDER BY ap.[Country] DESC;

此外,相关子查询对性能有点负担,因为查询对外部查询中的 每一 行运行 COUNT 聚合。如果您的表非常大且产品集群多种多样,则处理时间可能会很长。考虑将结果保存在临时表中,以避免每次需要排名时重新运行。

另外,对于一个非常大的表(数万到数十万行),请尝试打破五个国家/地区中的每一个:

SELECT ap.[Product 1]
     , ap.[Product 2]
     , ap.[Product 3]
     , ap.[Country]
     , ap.Profit
     , (SELECT COUNT(ap.[Country])
        FROM [Abfrage 10_Profit] sub_ap
        WHERE sub_ap.Profit >= ap.Profit
          AND sub_ap.[Product 1] = ap.[Product 1]
          AND sub_ap.[Product 2] = ap.[Product 2]
          AND sub_ap.[Product 3] = ap.[Product 3]
          AND sub_ap.[Country] = 'Switzerland') AS Rank 
FROM [Abfrage 10_Profit] ap
WHERE ap.[Country] = 'Switzerland'
ORDER BY ap.[Country] DESC;

并将它们堆叠在一个临时表中:

INSERT INTO RankTable ([Product 1], [Product 2], [Product 3], [Country], [Profit], [Rank])
SELECT [Product 1], [Product 2], [Product 3], [Country], [Profit], [Rank]
FROM mySwitzerlandQuery
INSERT INTO RankTable ([Product 1], [Product 2], [Product 3], [Country], [Profit], [Rank])
SELECT [Product 1], [Product 2], [Product 3], [Country], [Profit], [Rank]
FROM myGermanyQuery
INSERT INTO RankTable ([Product 1], [Product 2], [Product 3], [Country], [Profit], [Rank])
SELECT [Product 1], [Product 2], [Product 3], [Country], [Profit], [Rank]
FROM myAustriaQuery
...

甚至可以按集群的每个组合(如果组合的大小可管理)进行分解,甚至可以通过代码(C#、Java、Python、PHP、R、VBA 等)自动化。

SELECT ap.[Product 1]
     , ap.[Product 2]
     , ap.[Product 3]
     , ap.[Country]
     , ap.Profit
     , (SELECT COUNT(ap.[Country])
        FROM [Abfrage 10_Profit] sub_ap
        WHERE sub_ap.Profit >= ap.Profit
          AND sub_ap.[Product 1] = 'Computer'
          AND sub_ap.[Product 2] = 'Keyboard'
          AND sub_ap.[Product 3] = 'Mouse'
          AND sub_ap.[Country] = 'Switzerland') AS Rank 
FROM [Abfrage 10_Profit] ap
WHERE ap.[Product 1] = 'Computer'
  AND ap.[Product 2] = 'Keyboard'
  AND ap.[Product 3] = 'Mouse'
  AND ap.[Country] = 'Switzerland'
ORDER BY ap.[Country] DESC;

也许有一天,MS Access 将像我几年前在 Access 团队 Upgrade Access SQL dialect 中提倡的那样,像大多数其他 DBMS 一样拥有窗口函数来替换相关子查询。 (请投票!)

【讨论】:

    猜你喜欢
    • 2021-10-05
    • 1970-01-01
    • 2012-11-22
    • 1970-01-01
    • 1970-01-01
    • 2014-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多