Oracle SQL - 将行保留在特定列类别中列值较多的组内

发布于 2025-01-11 15:17:24 字数 2162 浏览 0 评论 0原文

我需要弄清楚如何从下表中获取结果,并且只显示图像中绿色突出显示的行。在每个 account_id/site_id 组合中(示例中显示了两个 - 123/usa 和 456/can),我只需要保留 group_ids,其中品牌的 Y(标志)数量较多(如side_id “can” - 该品牌的 group_id 1 中的 Y 数量比 group_id 2 中的 Y 数量多)。在 account_id/site 组合中,如果有两个 group_id 对于某个品牌具有超过两个 Y,则保留它们(如 site_id“usa”中所示)。

group_idaccount_idsite_id品牌标志
1123美国丰田N
1123美国丰田N
1123美国特斯拉Y
2123美国特斯拉Y
1123美国丰田Y
2123美国特斯拉N
2123美国丰田Y
2123美国特斯拉N
3123美国丰田Y
3123美国丰田N
3123美国特斯拉Y
3123美国特斯拉N
4123美国丰田Y
4123美国丰田Y
4123美国特斯拉N
4123美国特斯拉N
1456本田Y
1456本田Y
1456本田Y
2456可以本田Y
2456可以本田N
2456可以本田Y

在此处输入图像描述

I need to figure out how to take the results from the table below and only show me the green highlighted rows in the image. Within each account_id/site_id combo (there are two shown in the example - 123/usa and 456/can), I only need to keep the group_ids where there are a larger number of Y (flag) for the brand (as seen in the side_id "can" - there are more Y in group_id 1 for that brand than there are in group_id 2). Within the account_id/site combo, if there are two group_ids that have more than two Y for a brand, then keep them both (as seen in the site_id "usa").

group_idaccount_idsite_idbrandflag
1123usatoyotaN
1123usatoyotaN
1123usateslaY
1123usateslaY
2123usatoyotaY
2123usatoyotaN
2123usateslaY
2123usateslaN
3123usatoyotaY
3123usatoyotaN
3123usateslaY
3123usateslaN
4123usatoyotaY
4123usatoyotaY
4123usateslaN
4123usateslaN
1456canhondaY
1456canhondaY
1456canhondaY
2456canhondaY
2456canhondaN
2456canhondaY

enter image description here

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

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

发布评论

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

评论(1

野稚 2025-01-18 15:17:24

通过使用窗口函数的组合,您可以计算每个组/帐户/网站/品牌的 Y 标志数量,然后找到使用另一个分析函数分区的 Y 标志的最大数量,然后仅返回这两个函数所在的行价值观匹配。

查询

WITH
    grps (GROUP_ID,
          account_id,
          site_id,
          brand,
          flag)
    AS
        (SELECT 1, 123, 'usa', 'toyota', 'N' FROM DUAL
         UNION ALL
         SELECT 1, 123, 'usa', 'toyota', 'N' FROM DUAL
         UNION ALL
         SELECT 1, 123, 'usa', 'tesla', 'Y' FROM DUAL
         UNION ALL
         SELECT 1, 123, 'usa', 'tesla', 'Y' FROM DUAL
         UNION ALL
         SELECT 2, 123, 'usa', 'toyota', 'Y' FROM DUAL
         UNION ALL
         SELECT 2, 123, 'usa', 'toyota', 'N' FROM DUAL
         UNION ALL
         SELECT 2, 123, 'usa', 'tesla', 'Y' FROM DUAL
         UNION ALL
         SELECT 2, 123, 'usa', 'tesla', 'N' FROM DUAL
         UNION ALL
         SELECT 3, 123, 'usa', 'toyota', 'Y' FROM DUAL
         UNION ALL
         SELECT 3, 123, 'usa', 'toyota', 'N' FROM DUAL
         UNION ALL
         SELECT 3, 123, 'usa', 'tesla', 'Y' FROM DUAL
         UNION ALL
         SELECT 3, 123, 'usa', 'tesla', 'N' FROM DUAL
         UNION ALL
         SELECT 4, 123, 'usa', 'toyota', 'Y' FROM DUAL
         UNION ALL
         SELECT 4, 123, 'usa', 'toyota', 'Y' FROM DUAL
         UNION ALL
         SELECT 4, 123, 'usa', 'tesla', 'N' FROM DUAL
         UNION ALL
         SELECT 4, 123, 'usa', 'tesla', 'N' FROM DUAL
         UNION ALL
         SELECT 1, 456, 'can', 'honda', 'Y' FROM DUAL
         UNION ALL
         SELECT 1, 456, 'can', 'honda', 'Y' FROM DUAL
         UNION ALL
         SELECT 1, 456, 'can', 'honda', 'Y' FROM DUAL
         UNION ALL
         SELECT 2, 456, 'can', 'honda', 'Y' FROM DUAL
         UNION ALL
         SELECT 2, 456, 'can', 'honda', 'N' FROM DUAL
         UNION ALL
         SELECT 2, 456, 'can', 'honda', 'Y' FROM DUAL)
  SELECT g3.GROUP_ID,
         g3.account_id,
         g3.site_id,
         g3.brand,
         g3.flag
    FROM (SELECT g2.*, MAX (g2.y_flag_cnt) OVER (PARTITION BY account_id) AS max_y_flag_cnt
            FROM (SELECT g.*,
                         SUM (CASE WHEN flag = 'Y' THEN 1 ELSE 0 END)
                             OVER (PARTITION BY GROUP_ID,
                                                account_id,
                                                site_id,
                                                brand)    AS y_flag_cnt
                    FROM grps g) g2) g3
   WHERE flag = 'Y' AND y_flag_cnt = max_y_flag_cnt
ORDER BY account_id, GROUP_ID;

输出

   GROUP_ID    ACCOUNT_ID    SITE_ID     BRAND    FLAG
___________ _____________ __________ _________ _______
          1           123 usa        tesla     Y
          1           123 usa        tesla     Y
          4           123 usa        toyota    Y
          4           123 usa        toyota    Y
          1           456 can        honda     Y
          1           456 can        honda     Y
          1           456 can        honda     Y

By using a combination of window functions, you can count the number of Y flags for each group/account/site/brand, then find the maximum number of Y flags of those partitioned with another analytic function, then only return the rows where those two values match.

Query

WITH
    grps (GROUP_ID,
          account_id,
          site_id,
          brand,
          flag)
    AS
        (SELECT 1, 123, 'usa', 'toyota', 'N' FROM DUAL
         UNION ALL
         SELECT 1, 123, 'usa', 'toyota', 'N' FROM DUAL
         UNION ALL
         SELECT 1, 123, 'usa', 'tesla', 'Y' FROM DUAL
         UNION ALL
         SELECT 1, 123, 'usa', 'tesla', 'Y' FROM DUAL
         UNION ALL
         SELECT 2, 123, 'usa', 'toyota', 'Y' FROM DUAL
         UNION ALL
         SELECT 2, 123, 'usa', 'toyota', 'N' FROM DUAL
         UNION ALL
         SELECT 2, 123, 'usa', 'tesla', 'Y' FROM DUAL
         UNION ALL
         SELECT 2, 123, 'usa', 'tesla', 'N' FROM DUAL
         UNION ALL
         SELECT 3, 123, 'usa', 'toyota', 'Y' FROM DUAL
         UNION ALL
         SELECT 3, 123, 'usa', 'toyota', 'N' FROM DUAL
         UNION ALL
         SELECT 3, 123, 'usa', 'tesla', 'Y' FROM DUAL
         UNION ALL
         SELECT 3, 123, 'usa', 'tesla', 'N' FROM DUAL
         UNION ALL
         SELECT 4, 123, 'usa', 'toyota', 'Y' FROM DUAL
         UNION ALL
         SELECT 4, 123, 'usa', 'toyota', 'Y' FROM DUAL
         UNION ALL
         SELECT 4, 123, 'usa', 'tesla', 'N' FROM DUAL
         UNION ALL
         SELECT 4, 123, 'usa', 'tesla', 'N' FROM DUAL
         UNION ALL
         SELECT 1, 456, 'can', 'honda', 'Y' FROM DUAL
         UNION ALL
         SELECT 1, 456, 'can', 'honda', 'Y' FROM DUAL
         UNION ALL
         SELECT 1, 456, 'can', 'honda', 'Y' FROM DUAL
         UNION ALL
         SELECT 2, 456, 'can', 'honda', 'Y' FROM DUAL
         UNION ALL
         SELECT 2, 456, 'can', 'honda', 'N' FROM DUAL
         UNION ALL
         SELECT 2, 456, 'can', 'honda', 'Y' FROM DUAL)
  SELECT g3.GROUP_ID,
         g3.account_id,
         g3.site_id,
         g3.brand,
         g3.flag
    FROM (SELECT g2.*, MAX (g2.y_flag_cnt) OVER (PARTITION BY account_id) AS max_y_flag_cnt
            FROM (SELECT g.*,
                         SUM (CASE WHEN flag = 'Y' THEN 1 ELSE 0 END)
                             OVER (PARTITION BY GROUP_ID,
                                                account_id,
                                                site_id,
                                                brand)    AS y_flag_cnt
                    FROM grps g) g2) g3
   WHERE flag = 'Y' AND y_flag_cnt = max_y_flag_cnt
ORDER BY account_id, GROUP_ID;

Output

   GROUP_ID    ACCOUNT_ID    SITE_ID     BRAND    FLAG
___________ _____________ __________ _________ _______
          1           123 usa        tesla     Y
          1           123 usa        tesla     Y
          4           123 usa        toyota    Y
          4           123 usa        toyota    Y
          1           456 can        honda     Y
          1           456 can        honda     Y
          1           456 can        honda     Y
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文