我从4个不同的表中以不同的名称进行销售。例如人力资源销售,客户销售等
我需要这样的东西。
Type Sales
----------------------
Table1 FCL ?
Table2 LCL ?
Table3 Import ?
Table4 Export ?
Overall ?
我尝试了以下查询,
INSERT INTO [dbo].[Warehouse_Type] ([Overall Revenue])
SELECT (SELECT SUM([Amount]) FROM [dbo].[Bond_Overall]) +
(SELECT SUM([Amount]) FROM [dbo].[Export_Overall])+
(SELECT SUM([TOTAL BILLING]) FROM [dbo].[FCL Test]) +
(SELECT SUM([Total Revenue Generated]) FROM [dbo].[LCL_overall])
注意:我的查询提供了来自不同表的值总和,而我需要总和来更新各个值。例如在我的查询中,第一选择给出第一类型的总和,第二选择给出第二类型的总和,依此类推。我需要在该表的后面附加另一个名为Warehouse_type的表。
哪个给出了总收入及其价值,但没有给出其他类型。
有人可以在此查询中帮助我计算和更新每种类型的销售额吗?
答案 0 :(得分:3)
首先建立销售联盟:
(SELECT 'Import' As Type, SUM([Amount]) FROM [dbo].[Bond_Overall])
Union All
(SELECT 'Export' As Type, SUM([Amount]) FROM [dbo].[Export_Overall])
Union All
(SELECT 'FCL' As Type, SUM([TOTAL BILLING]) FROM [dbo].[FCL Test])
Union All
(SELECT 'LCL' As Type, SUM([Total Revenue Generated]) FROM [dbo].[LCL_overall])
如果要插入:
INSERT INTO [dbo].[Warehouse_Type](Type, Sales) ([Overall Revenue])
Select From (
(SELECT 'Import', SUM([Amount]) FROM [dbo].[Bond_Overall])
Union All
(SELECT 'Export', SUM([Amount]) FROM [dbo].[Export_Overall])
Union All
(SELECT 'FCL', SUM([TOTAL BILLING]) FROM [dbo].[FCL Test])
Union All
(SELECT 'LCL', SUM([Total Revenue Generated]) FROM [dbo].[LCL_overall])) As D
也许没有必要进行子选择。
答案 1 :(得分:1)
类似的东西
;WITH CTE AS
(
SELECT 'FCL' [Type], SUM(Col) Sales
FROM Table1
UNION
SELECT 'LCL', SUM(Col)
FROM Table2
UNION
SELECT 'Import', SUM(Col)
FROM Table3
UNION
SELECT 'Export', SUM(Col)
FROM Table4
)
SELECT *
FROM CTE
UNION
SELECT 'Overall', SUM(Sales)
FROM CTE;
如果要将值插入到表中,则
INSERT INTO [dbo].[Warehouse_Type] ([Overall Revenue])
SELECT Sales
FROM CTE
UNION
SELECT SUM(Salse)
FROM CTE;
如果您不插入Type
,我不知道有什么需要。