[英]comparing sum of 2 column values in one table with another column value in second table sql server
I have two tables... 我有两张桌子
table1 ( id, item, price ) values: table1(id,item,price)值:
id | item | price
-------------
10 | book | 20
20 | copy | 30
30 | pen | 10
....table2 ( id, item, price) values: .... table2(id,item,price)值:
id | item | price
-------------
10 | book | 20
10 | book | 30
now i if do not have a record with id-10,item-book and price-(20+30) in table 1 then i want to insert that row with sum(20+30) in a new table ... 现在我如果在表1中没有id-10,item-book和price-(20 + 30)的记录,那么我想在新表中插入具有sum(20 + 30)的行...
Try the following 尝试以下
SELECT T2.id,T2.item,T2.Price as Table2_Price,T1.Price as Table1_Price FROM (
SELECT id,Item,Sum(Price) as Price
FROM Table2
Group BY id,Item ) AS T2 LEFT JOIN Table1 AS T1
ON T1.Item = T2.Item and T1.ID = T2.ID
After reading your comments when sum of these two not equals the one in another table it should return the sum from table 2
, you are asking for the following logic 在读完注释后,
when sum of these two not equals the one in another table it should return the sum from table 2
,您需要以下逻辑
SELECT T2.id,T2.Item, CASE WHEN T1.Price IS NULL THEN T2.Price
WHEN T2.Price <> T1.Price THEN T2.Price
ELSE T1.Price END as Price FROM (
SELECT id,Item,Sum(Price) as Price
FROM Table2
Group BY id,Item ) AS T2 LEFT JOIN Table1 AS T1
ON T1.Item = T2.Item and T1.ID = T2.ID
But this logic isn't useful, because if Sum(Table2_price) <> Table1_Price
you want to select Sum(Table2_price)
else when Sum(Table2_price) = Table1_Price
you want Table1_Price
!! 但这逻辑没有用,因为如果
Sum(Table2_price) <> Table1_Price
您想选择Sum(Table2_price)
否则当Sum(Table2_price) = Table1_Price
您想要Table1_Price
! So you want to always choose Sum(Table2_Price)
所以您要始终选择
Sum(Table2_Price)
If I haven't misunderstood your requirements, this should do it: 如果我没有误解您的要求,可以这样做:
SELECT T2.ID, T2.ITEM,T2.SUMPRICE FROM
(SELECT ID, ITEM, SUM(PRICE) AS SUMPRICE FROM table1 GROUP BY ID, ITEM) AS T1
INNER JOIN
(SELECT ID, ITEM, SUM(PRICE) AS SUMPRICE FROM table2 GROUP BY ID, ITEM) AS T2
ON T1.ID = T2.id AND T1.item = T2.item WHERE T1.SUMPRICE <> T2.SUMPRICE
If your 3rd table is already created you could just use an INSERT INTO SELECT statement. 如果已经创建了第三个表,则可以使用INSERT INTO SELECT语句。 Otherwise you could use a SELECT INTO like this:
否则,您可以像这样使用SELECT INTO :
SELECT T2.ID, T2.ITEM,T2.SUMPRICE as price into table3 FROM
(SELECT ID, ITEM, SUM(PRICE) AS SUMPRICE FROM table1 GROUP BY ID, ITEM) AS
T1
INNER JOIN
(SELECT ID, ITEM, SUM(PRICE) AS SUMPRICE FROM table2 GROUP BY ID, ITEM) AS
T2
ON T1.ID = T2.id AND T1.item = T2.item WHERE T1.SUMPRICE <> T2.SUMPRICE
Hope it helps! 希望能帮助到你!
EDIT 1 In the case that you want to get all the unmatched rows, that is: 编辑1如果要获取所有不匹配的行,即:
You could use the EXCEPT statement, for example: 您可以使用EXCEPT语句,例如:
(SELECT ID, ITEM, SUM(PRICE) AS SUMPRICE FROM table2 GROUP BY ID, ITEM)
EXCEPT
(SELECT ID, ITEM, SUM(PRICE) AS SUMPRICE FROM table1 GROUP BY ID, ITEM)
This returns: 返回:
ID ITEM SUMPRICE
---- -------------------- -----------
10 book 50
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.