Skip to content

How to substract two value in two tables in sql database

Asim Jena

Asked Aug 29, 2018, 2:50 AM EDT1 answer
Share
LinkedInX
1336.3011CODEOFF
 this is my first table value is total cost and 
1GuchiAbobe 200 Purchase11CODOFFhttp://192.168.0.104/Image/PromoCode/Discount.png200.00
 table value  field is condition value
how to substract 

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Yogendra Sahu
    0
    Use a temporary table...
    in a temporary table, #temp1 store your 1st table and in temporary table #temp2, you can store your 2nd table.
    then create a new table by creating its column name and when you create price column then you can perform subtraction.
    here is the sample code
     
    CREATE TABLE #temp1 (
    Product VARCHAR(100),
    Price int
    )
     
    CREATE TABLE #temp2 (
    Product VARCHAR(100),
    Price int
    )
     
    CREATE TABLE #Subs (
    Product VARCHAR(100),
    Price int
    )
     
    INSERT INTO #temp1
    SELECT Product,Price FROM table1
     
    INSERT INTO #previousyear
    SELECT Product,Price FROM table2
     
    INSERT INTO #Subs
    SELECT t1.Product as Product, t1.Price - t2.Price as diff
    FROM #temp1 t1 JOIN #temp2 t2 ON t2.Product, = t1.Product
     
    SELECT * FROM #Subs
    0