带条件的分区 SQL 查询

发布于 2025-01-17 14:35:24 字数 7849 浏览 3 评论 0原文

我在SQL Server 2019中有一个这样的表:

ITEM_IDParent_ItemTalleExistencia
90929628064741
90929628064741
90929628064741
90929728064751
90929728064751
90929728064751
90929728064751
90929828064761
90929828064761
90929828064761
90929928064771
90929928064771
90930028064781
90930128064791
909293280647111
909294280647121
909292280647101
1226447280647130

我需要一个名为“Real Exist”的新列。当 'Existencia' = 1 时,'Talle' 列中不同值的总和。在此示例中 (Parent_item = 280647) 结果应为 9 ,因为值 4,5,6,7,8,9,11, “Talle”列中的 12、10:

ITEM_IDParent_ItemTalleExistencia真实存在。
909296280647419
909296280647419
90929628064742806475
909297280647519
909297280647519
9092979119
909297280647519
909298280647619
909298280647619
909298280647619
909299280647719
909299280647719
909300280647819
909301280647919
9092932806471119
9092942806471219
9092922806471019
12264472806471309

怎么办?提前致谢!

I have a table like this, in SQL Server 2019:

ITEM_IDParent_ItemTalleExistencia
90929628064741
90929628064741
90929628064741
90929728064751
90929728064751
90929728064751
90929728064751
90929828064761
90929828064761
90929828064761
90929928064771
90929928064771
90930028064781
90930128064791
909293280647111
909294280647121
909292280647101
1226447280647130

And I need a new column named 'Real Exist.' with the total sum of different values in 'Talle' Column, when 'Existencia' = 1. In this example (Parent_item = 280647) result should be 9 , because of values 4,5,6,7,8,9,11, 12, 10 in 'Talle' Column :

ITEM_IDParent_ItemTalleExistenciaReal Exist.
909296280647419
909296280647419
909296280647419
909297280647519
909297280647519
909297280647519
909297280647519
909298280647619
909298280647619
909298280647619
909299280647719
909299280647719
909300280647819
909301280647919
9092932806471119
9092942806471219
9092922806471019
12264472806471309

How could it be done?. Thanks in advance!

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

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

发布评论

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

评论(1

好久不见√ 2025-01-24 14:35:24

一种方法是使用派生的查询来计算每个parent_item 结果的唯一值数

SELECT yt.Item_Id
       , yt.Parent_Item
       , yt.Talle
       , yt.Existencia
       , cnt.NumOfTalle AS [Real Exist]
FROM   YourTable yt 
        INNER JOIN (
            SELECT Parent_Item, COUNT(DISTINCT Talle) AS NumOfTalle
            FROM   YourTable
            WHERE  Existencia = 1
            GROUP  BY Parent_Item
        ) 
        cnt ON cnt.Parent_Item = yt.Parent_Item

 item_id | parent_item |塔勒|存在|真实存在
------:| -----------:| ----:| ----------:| ------------:
 909296 | 280647 | 4 | 1 | 9
 909296 | 280647 | 4 | 1 | 9
 909296 | 280647 | 4 | 1 | 9
 909297 | 280647 | 5 | 1 | 9
 909297 | 280647 | 5 | 1 | 9
 909297 | 280647 | 5 | 1 | 9
 909297 | 280647 | 5 | 1 | 9
 909298 | 280647 | 6 | 1 | 9
 909298 | 280647 | 6 | 1 | 9
 909298 | 280647 | 6 | 1 | 9
 909299 | 280647 | 7 | 1 | 9
 909299 | 280647 | 7 | 1 | 9
 909300 | 280647 | 8 | 1 | 9
 909301 | 280647 | 9 | 1 | 9
 909293 | 280647 | 11 | 1 | 9
 909294 | 280647 | 12 | 1 | 9
 909292 | 280647 | 10 | 1 | 9
1226447 | 280647 | 13 | 0 | 9

db<

One way is using a derived query to count the number of unique values per Parent_Item

SELECT yt.Item_Id
       , yt.Parent_Item
       , yt.Talle
       , yt.Existencia
       , cnt.NumOfTalle AS [Real Exist]
FROM   YourTable yt 
        INNER JOIN (
            SELECT Parent_Item, COUNT(DISTINCT Talle) AS NumOfTalle
            FROM   YourTable
            WHERE  Existencia = 1
            GROUP  BY Parent_Item
        ) 
        cnt ON cnt.Parent_Item = yt.Parent_Item

Results:

Item_Id | Parent_Item | Talle | Existencia | Real Exist
------: | ----------: | ----: | ---------: | ---------:
 909296 |      280647 |     4 |          1 |          9
 909296 |      280647 |     4 |          1 |          9
 909296 |      280647 |     4 |          1 |          9
 909297 |      280647 |     5 |          1 |          9
 909297 |      280647 |     5 |          1 |          9
 909297 |      280647 |     5 |          1 |          9
 909297 |      280647 |     5 |          1 |          9
 909298 |      280647 |     6 |          1 |          9
 909298 |      280647 |     6 |          1 |          9
 909298 |      280647 |     6 |          1 |          9
 909299 |      280647 |     7 |          1 |          9
 909299 |      280647 |     7 |          1 |          9
 909300 |      280647 |     8 |          1 |          9
 909301 |      280647 |     9 |          1 |          9
 909293 |      280647 |    11 |          1 |          9
 909294 |      280647 |    12 |          1 |          9
 909292 |      280647 |    10 |          1 |          9
1226447 |      280647 |    13 |          0 |          9

db<>fiddle here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文