[英]SUM of columns from different tables
here is my data, 这是我的数据,
Table 1: 表格1:
STORAGE HANDLING TOTAL BILLING
--------------------------------------
1300 10900
0 10950
0 6000
0 5950
Table 2: 表2:
LINER REVENUE
---------------
1300
250
3000
200
I need to calculate Total Billing: 我需要计算总账单:
I tried with this code. 我尝试使用此代码。
UPDATE [dbo].[FCLOverall] SET [Total Revenue] = SELECT SUM([STORAGE]), SUM([HANDLING]), SUM([LINER Revenue])
FROM (SELECT [STORAGE], [HANDLING],[Container No]
FROM [dbo].[FCLOverall]
UNION ALL
SELECT [Container No],[LINER Revenue]
FROM [dbo].[FCL_Child])
It is throwing some error missing brackets and invalid keyword select. 它抛出一些错误,缺少括号和无效的关键字选择。 is the query is right or wrong? 查询是对还是错?
Can someone guide a query how to calculate on this? 有人可以指导查询如何计算吗?
does you find something below 你在下面找到东西吗
UPDATE [dbo].[FCLOverall]
SET [Total Revenue] =
(SELECT SUM([STORAGE])+ SUM([HANDLING]) + SUM([LINER Revenue] ) as s
FROM (
SELECT [Container No],[STORAGE], [HANDLING],0
FROM [dbo].[FCLOverall]
UNION ALL
SELECT [Container No],0,0,[LINER Revenue]
FROM [dbo].[FCL_Child]
) t )
Give this a try based on the assumption that Container No is key field. 请根据“容器号”为关键字段的假设进行尝试。
UPDATE U
SET U.[Total Revenue] = COALESCE(U.Storage,0) + COALESCE(U.Handling,0) + COALESCE(FCLC.[Liner Revenue],0)
FROM dbo.FCLOverall AS U
INNER JOIN dbo.FCL_Child AS FCLC
ON U.[Container No] = FCLC.[Container No]
Try this: 尝试这个:
UPDATE FO
SET [FO.Total Revenue] = ISNULL(FO.[STORAGE], 0) + ISNULL(FO.[HANDLING], 0) + ISNULL(FC.[LINER Revenue],0)
FROM [dbo].[FCLOverall] AS FO
INNER JOIN [dbo].[FCL_Child] FC on FO.[FO.Container No] = FC.[Container No]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.