如何使用Group By子句合并两个sql server表?
我有两个表TableA和TableB,如下所示:
TableA:
ItemID Qty Rate
-------- ----- --------
1 10 100.00
2 20 150.00
TableB:
ItemID Qty Rate
-------- ----- -------
1 5 150.00
3 10 200.00
3 20 400.00
现在我想合并这两个表。我想要的结果需要如下:
结果 TableA:
ItemID Qty Rate
-------- ----- -------
1 15 150.00
2 20 150.00
3 30 400.00
我尝试了以下 Insert Select 语句,但它没有给出所需的结果。
INSERT INTO TableA
(
ItemID,
Qty,
Rate
)
SELECT
ItemID,
SUM(Qty),
MAX(Rate)
FROM
TableB
GROUP BY
ItemID
但它给出的结果如下:
ItemID Qty Rate
-------- ----- --------
1 10 100.00
2 20 150.00
1 5 150.00
3 30 400.00
如何达到我想要的结果?
我尝试这样:
MERGE PUR_PODetail AS Target
USING (
SELECT
@POID,
ItemID,
SUM(POQuantity),
MAX(UnitRate),
1,
CASE WHEN D1 = '' THEN NULL ELSE D1 END D1,
CASE WHEN D2 = '' THEN NULL ELSE D2 END D2,
CASE WHEN D3 = '' THEN NULL ELSE D3 END D3,
CASE WHEN RandomDimension = '' THEN NULL ELSE RandomDimension END RandomDimension,
0
FROM
@Detail
GROUP BY
ItemID, D1, D2, D3, RandomDimension
) AS Source ON (Target.ItemID = Source.ItemID) AND
(ISNULL(Target.D1, -999) = ISNULL(Source.D1, -999)) AND
(ISNULL(Target.D2, -999) = ISNULL(Source.D2, -999)) AND
(ISNULL(Target.D3, -999) = ISNULL(Source.D3, -999)) AND
(ISNULL(Target.RandomDimension, -999) = ISNULL(Source.RandomDimension, -999))
WHEN MATCHED
THEN UPDATE SET
Target.POQuantity = Target.POQuantity + Source.POQuantity,
Target.UnitRate = MAX(Source.UnitRate)
WHEN NOT MATCHED
INSERT
(
POID,
ItemID,
POQuantity,
UnitRate,
ItemStatusID,
D1,
D2,
D3,
RandomDimension,
EDInclusive_f
)
VALUES
(
@POID,
Source.ItemID,
Source.POQuantity,
Source.UnitRate,
1,
CASE WHEN Source.D1 = '' THEN NULL ELSE Source.D1 END D1,
CASE WHEN Source.D2 = '' THEN NULL ELSE Source.D2 END D2,
CASE WHEN Source.D3 = '' THEN NULL ELSE Source.D3 END D3,
CASE WHEN Source.RandomDimension = '' THEN NULL ELSE Source.RandomDimension END RandomDimension,
0
)
但它给出了以下错误。 请更正错误。我不知道这里会出什么问题。
消息 102,级别 15,状态 1,过程 PUR_PurchaseOrder_IU,第 936 行 “MERGE”附近的语法不正确。 消息 156,级别 15,状态 1,过程 PUR_PurchaseOrder_IU,第 953 行 关键字“AS”附近的语法不正确。
但是当我从存储过程中删除这些合并语句时,它正在执行......
I have two tables TableA and TableB as follows:
TableA:
ItemID Qty Rate
-------- ----- --------
1 10 100.00
2 20 150.00
TableB:
ItemID Qty Rate
-------- ----- -------
1 5 150.00
3 10 200.00
3 20 400.00
Now I want to consolidate these two tables. My desired result needs to be as follows:
Result TableA:
ItemID Qty Rate
-------- ----- -------
1 15 150.00
2 20 150.00
3 30 400.00
I tried the following Insert Select statement, But it does not give the desired result.
INSERT INTO TableA
(
ItemID,
Qty,
Rate
)
SELECT
ItemID,
SUM(Qty),
MAX(Rate)
FROM
TableB
GROUP BY
ItemID
But it gives the result as follows:
ItemID Qty Rate
-------- ----- --------
1 10 100.00
2 20 150.00
1 5 150.00
3 30 400.00
How to achieve my desired result?
I tried like this:
MERGE PUR_PODetail AS Target
USING (
SELECT
@POID,
ItemID,
SUM(POQuantity),
MAX(UnitRate),
1,
CASE WHEN D1 = '' THEN NULL ELSE D1 END D1,
CASE WHEN D2 = '' THEN NULL ELSE D2 END D2,
CASE WHEN D3 = '' THEN NULL ELSE D3 END D3,
CASE WHEN RandomDimension = '' THEN NULL ELSE RandomDimension END RandomDimension,
0
FROM
@Detail
GROUP BY
ItemID, D1, D2, D3, RandomDimension
) AS Source ON (Target.ItemID = Source.ItemID) AND
(ISNULL(Target.D1, -999) = ISNULL(Source.D1, -999)) AND
(ISNULL(Target.D2, -999) = ISNULL(Source.D2, -999)) AND
(ISNULL(Target.D3, -999) = ISNULL(Source.D3, -999)) AND
(ISNULL(Target.RandomDimension, -999) = ISNULL(Source.RandomDimension, -999))
WHEN MATCHED
THEN UPDATE SET
Target.POQuantity = Target.POQuantity + Source.POQuantity,
Target.UnitRate = MAX(Source.UnitRate)
WHEN NOT MATCHED
INSERT
(
POID,
ItemID,
POQuantity,
UnitRate,
ItemStatusID,
D1,
D2,
D3,
RandomDimension,
EDInclusive_f
)
VALUES
(
@POID,
Source.ItemID,
Source.POQuantity,
Source.UnitRate,
1,
CASE WHEN Source.D1 = '' THEN NULL ELSE Source.D1 END D1,
CASE WHEN Source.D2 = '' THEN NULL ELSE Source.D2 END D2,
CASE WHEN Source.D3 = '' THEN NULL ELSE Source.D3 END D3,
CASE WHEN Source.RandomDimension = '' THEN NULL ELSE Source.RandomDimension END RandomDimension,
0
)
But it gives the following error.
Please correct the error. I dont know what would be wrong here.
Msg 102, Level 15, State 1, Procedure PUR_PurchaseOrder_IU, Line 936
Incorrect syntax near 'MERGE'.
Msg 156, Level 15, State 1, Procedure PUR_PurchaseOrder_IU, Line 953
Incorrect syntax near the keyword 'AS'.
But when I remove these merge statement from my stored procedure, it is executing...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为此,您不能仅使用
INSERT
语句,您必须INSERT
或UPDATE
,具体取决于目标中已存在的 ItemID桌子。SQL Server 2005
SQL Server 2008 提供MERGE 对此的声明。
SQL Server 2008
You can't just use the
INSERT
statement for this, you have to eitherINSERT
orUPDATE
depending on the ItemID already being present in your target table.SQL Server 2005
SQL Server 2008 provides the MERGE statement for this.
SQL Server 2008
试试这个:
Try this one:
声明 @t 表 (ItemID int,Qty int,Rate int)
插入 @t 值 (1,10,100),(2,20,150)
Select * from @t
声明 @t1 表 (ItemID int,Qty int,Rate int)
插入@t1值 (1,5,150),(3,10,200),(3,20,400)
从@t1中选择*
插入@t
从 @t1 中选择 ItemID,sum(Qty) Qty,max(Rate) Rate,其中 ItemID 不在(从 @t 中选择 ItemID)
按 ItemID 分组
从 @t 中选择 *
declare @t table (ItemID int,Qty int,Rate int)
insert into @t values (1,10,100),(2,20,150)
Select * from @t
declare @t1 table (ItemID int,Qty int,Rate int)
insert into @t1 values (1,5,150),(3,10,200),(3,20,400)
Select * from @t1
insert into @t
Select ItemID,sum(Qty) Qty,max(Rate) Rate from @t1 where ItemID not in (Select ItemID from @t)
group by ItemID
Select * from @t