简体   繁体   English

比较一个表中2个列值的总和与第二个表SQL Server中另一个列的值

[英]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如果要获取所有不匹配的行,即:

  1. Rows from table 2 where the id, item or price don't match simultenously with a given row in table 1 表2中ID,商品价格与表1中给定行同时不匹配的行
  2. Rows from table 2 where none of their columns match simultenously with a given row in table 1 表2中的行,其中没有任何列与表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.

相关问题 一个表中的SQL sum列,其中另一表中的值为“ S” - SQL sum column in one table where value in another table is 'S' 在SQL Server中将一个表中的列名与另一表中的列数据进行比较 - Comparing Column Name in one Table with Column Data in another table in SQL Server SQL-基于一个表与另一个表的联接(第一个表的行值到第二个表的列值) - SQL - Joining one table with another based on ( Row value from first table to Column value of second table) 使用另一个表中的列值更新一个表中的列的值SQL? - Updating the values of a column in one table with the values of a column in another table SQL? 将同一表中一列的值更新到 SQL Server 中的另一列 - Update values from one column in same table to another in SQL Server 将同一表中一列中的值更新为 SQL 服务器中的另一列 - Update values in one column in same table to another in SQL Server SQL:将一个表中的多个值与另一个表中的单个值进行比较 - SQL : Comparing multiple values in one table with a single value in another Table SQL Server:根据同一表中另一列与另一表的列的匹配值,更新一个表中的列的值 - SQL Server: update value of a column in one table based on the matching value of another column in the same table to another table's column 根据另一个表中与第一个表中的列名相关的值,更新一个表中的SQL Server表值 - Update SQL Server table values in one table based upon values in another table that relate to the column names in the first SQL - 从一个表列到另一个表列的值 - SQL - values from one table column to another table column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM