从子表列值更新父表列

时间:2019-07-18 13:03:55

标签: sql-server while-loop inner-join parent-child aggregate-functions

我有一个表SaleOrder(SO)和SaleOrderDetail(SOD),其中一个可能与发货有关。 ID和SOID是主键外键。 在基于主键进行一些聚合之后,我需要使用SOD表的值更新SO表。 请参见下面。

SO
-----------------------------------
ID  SaleOrderQty
1   --
2   --

SOD
-------------------------------------
SOID    Qty PerPack
1       3   10
1       7   6
2       4   5
2       5   8

数量乘以PerPack

1   3*10 = 30
1   7*6 = 42
2   4*5 = 20
2   5*8 = 40

并根据键将所有乘法结果相加

1   30+42 = 72
2   20+40 = 60

并更新父表

SO
-----------------------------------
ID  SaleOrderQty
1   72
2   60

我尝试过

Declare @Id varchar(50)
declare @Next int
set @Next =1
WHILE @Next <= 30
Begin
    Select @Id = Id From SO Where SOSerial=@Next

    Update SO 
    Set SaleOrderQty = (SELECT  sum((SOD.Quantity* SOD.PerPack)) total
                        FROM  SO INNER JOIN
                        SOD ON SO.Id = SOD.SOId
                        WHERE SOD.SOId=@Id
                        group by SOD.SOId)


                        set @Next=@Next+1
                        --print @Id
End

总和可以,但是会将所有SaleOrderQty值设置为最后一个总和。

还有一件事。我在父表中有30条记录。查询完成后,它会显示30条消息。

1 个答案:

答案 0 :(得分:1)

根据我之前的评论,您希望避免循环。

2019-07-18T08:56:25.74-0400 [APP/PROC/WEB/0] OUT 12:56:25.742 [main] WARN org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
   2019-07-18T08:56:25.74-0400 [APP/PROC/WEB/0] OUT 12:56:25.747 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
   2019-07-18T08:56:25.74-0400 [APP/PROC/WEB/0] OUT org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
   2019-07-18T08:56:25.74-0400 [APP/PROC/WEB/0] OUT     at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:155)
   2019-07-18T08:56:25.74-0400 [APP/PROC/WEB/0] OUT     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
   2019-07-18T08:56:25.74-0400 [APP/PROC/WEB/0] OUT     at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)
   2019-07-18T08:56:25.74-0400 [APP/PROC/WEB/0] OUT     at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:742)
   2019-07-18T08:56:25.74-0400 [APP/PROC/WEB/0] OUT     at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:389)
   2019-07-18T08:56:25.74-0400 [APP/PROC/WEB/0] OUT     at org.springframework.boot.SpringApplication.run(SpringApplication.java:311)
   2019-07-18T08:56:25.74-0400 [APP/PROC/WEB/0] OUT     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1213)
   2019-07-18T08:56:25.74-0400 [APP/PROC/WEB/0] OUT     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1202)
   2019-07-18T08:56:25.74-0400 [APP/PROC/WEB/0] OUT     at com.homedepot.assortment.transmission.MerchTransmissionApplication.main(MerchTransmissionApplication.java:10)
   2019-07-18T08:56:25.74-0400 [APP/PROC/WEB/0] OUT Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
   2019-07-18T08:56:25.74-0400 [APP/PROC/WEB/0] OUT     at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:202)
   2019-07-18T08:56:25.74-0400 [APP/PROC/WEB/0] OUT     at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:178)
   2019-07-18T08:56:25.74-0400 [APP/PROC/WEB/0] OUT     at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:152)
   2019-07-18T08:56:25.74-0400 [APP/PROC/WEB/0] OUT     ... 8 common frames omitted
   2019-07-18T08:56:25.77-0400 [APP/PROC/WEB/0] OUT Exit status 1
   2019-07-18T08:56:25.77-0400 [CELL/SSHD/0] OUT Exit status 0
   2019-07-18T08:56:32.78-0400 [CELL/0] OUT Cell 3e951dff-2425-4c92-b7bb-94071ffc2ac5 stopping instance a4a2bdc5-d02e-4312-748e-8eb2
   2019-07-18T08:56:32.78-0400 [CELL/0] OUT Cell 3e951dff-2425-4c92-b7bb-94071ffc2ac5 destroying container for instance a4a2bdc5-d02e-4312-748e-8eb2
   2019-07-18T08:56:32.81-0400 [API/4] OUT Process has crashed with type: "web"