在Sql Server 2008中,如何为每个客户计算发货数量

发布于 2024-12-10 02:37:02 字数 525 浏览 0 评论 0原文

我有两张表:客户------>>发货

我想为每个客户的每批发货提供一个唯一的编号,例如

CustomerID      ShipmentID       ShipmnetNumber
    10              50                  1
    10              51                  2
    10              55                  3

    15              56                  1
    15              57                  2
    15              58                  3

    17              59                  1
    17              60                  2

等...

我如何在 sql server 2008 中做到这一点

I have two tables : Customer ------>> Shipment

I want to give each shipment a unique number for each customer, for example

CustomerID      ShipmentID       ShipmnetNumber
    10              50                  1
    10              51                  2
    10              55                  3

    15              56                  1
    15              57                  2
    15              58                  3

    17              59                  1
    17              60                  2

etc ...

how can I do it in sql server 2008

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

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

发布评论

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

评论(1

那请放手 2024-12-17 02:37:02

要通过查询获取编号,您可以执行此操作

select CustomerID,
       ShipmentID,
       row_number() over(partition by Shipment.CustomerID  
                         order by ShipmentID) as ShipmentNumber
from Shipment

如果您想使用 ShipmentNumber(新添加的列)更新表,您可以执行此操作

;with S as
(
  select ShipmentNumber,
         row_number() over(partition by Shipment.CustomerID  
                           order by ShipmentID) as ShipNum
  from Shipment
)
update S
set ShipmentNumber = ShipNum

To get the numbering with a query you can do this

select CustomerID,
       ShipmentID,
       row_number() over(partition by Shipment.CustomerID  
                         order by ShipmentID) as ShipmentNumber
from Shipment

If you want to update a table with ShipmentNumber (newly added column) you can do this

;with S as
(
  select ShipmentNumber,
         row_number() over(partition by Shipment.CustomerID  
                           order by ShipmentID) as ShipNum
  from Shipment
)
update S
set ShipmentNumber = ShipNum
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文