带条件的分区 SQL 查询
我在SQL Server 2019中有一个这样的表:
ITEM_ID | Parent_Item | Talle | Existencia |
---|---|---|---|
909296 | 280647 | 4 | 1 |
909296 | 280647 | 4 | 1 |
909296 | 280647 | 4 | 1 |
909297 | 280647 | 5 | 1 |
909297 | 280647 | 5 | 1 |
909297 | 280647 | 5 | 1 |
909297 | 280647 | 5 | 1 |
909298 | 280647 | 6 | 1 |
909298 | 280647 | 6 | 1 |
909298 | 280647 | 6 | 1 |
909299 | 280647 | 7 | 1 |
909299 | 280647 | 7 | 1 |
909300 | 280647 | 8 | 1 |
909301 | 280647 | 9 | 1 |
909293 | 280647 | 11 | 1 |
909294 | 280647 | 12 | 1 |
909292 | 280647 | 10 | 1 |
1226447 | 280647 | 13 | 0 |
我需要一个名为“Real Exist”的新列。当 'Existencia' = 1 时,'Talle' 列中不同值的总和。在此示例中 (Parent_item = 280647) 结果应为 9 ,因为值 4,5,6,7,8,9,11, “Talle”列中的 12、10:
ITEM_ID | Parent_Item | Talle | Existencia | 真实存在。 |
---|---|---|---|---|
909296 | 280647 | 4 | 1 | 9 |
909296 | 280647 | 4 | 1 | 9 |
909296 | 280647 | 4 | 280647 | 5 |
909297 | 280647 | 5 | 1 | 9 |
909297 | 280647 | 5 | 1 | 9 |
909297 | 9 | 1 | 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 |
怎么办?提前致谢!
I have a table like this, in SQL Server 2019:
ITEM_ID | Parent_Item | Talle | Existencia |
---|---|---|---|
909296 | 280647 | 4 | 1 |
909296 | 280647 | 4 | 1 |
909296 | 280647 | 4 | 1 |
909297 | 280647 | 5 | 1 |
909297 | 280647 | 5 | 1 |
909297 | 280647 | 5 | 1 |
909297 | 280647 | 5 | 1 |
909298 | 280647 | 6 | 1 |
909298 | 280647 | 6 | 1 |
909298 | 280647 | 6 | 1 |
909299 | 280647 | 7 | 1 |
909299 | 280647 | 7 | 1 |
909300 | 280647 | 8 | 1 |
909301 | 280647 | 9 | 1 |
909293 | 280647 | 11 | 1 |
909294 | 280647 | 12 | 1 |
909292 | 280647 | 10 | 1 |
1226447 | 280647 | 13 | 0 |
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_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 |
How could it be done?. Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种方法是使用派生的查询来计算每个parent_item 结果的唯一值数
:
db<
One way is using a derived query to count the number of unique values per Parent_Item
Results:
db<>fiddle here