Sum two columns from two tables

  • Hello,

    I really need help with this querysyntax

    Example Data:

    Table1

    number location quantity

    23 RED 2

    23 RED 6

    Table2

    number location quantity param1 param2

    23 RED 1 5 Order

    23 RED 2 6 Order

    I want to sum(quantity) in table1 and subtract that with sum(quantity) in table2 and then group by location and number where table2.param1 > 0 and table2.param2 = 'Order'

    Desired output from exampledata above would be:

    number location quantity

    23 RED 5

    Is this even possible?

    Regards

  • select (temp1.qty1-temp2.qty2)as Total from

    (select sum(qty)as qty1 from t1

    group by Number,location) temp1,

    (select sum(qty)as qty2 from t2

    where P1>0 and P2='Order'

    group by Number,location) temp2

    please let me know it doesn't suit your requirement.

  • SELECT T1.location, T1.number, T1.quantity - T2.quantity AS quantity

    FROM

    (

    &nbsp&nbsp&nbsp&nbspSELECT T11.location, T11.number, SUM(T11.quantity) AS quantity

    &nbsp&nbsp&nbsp&nbspFROM Table1 T11

    &nbsp&nbsp&nbsp&nbspGROUP BY T11.location, T11.number

    ) T1

    &nbsp&nbsp&nbsp&nbspJOIN

    &nbsp&nbsp&nbsp&nbsp(

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspSELECT T21.location, T21.number, SUM(T21.quantity) AS quantity

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspFROM Table2 T21

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspWHERE T21.param1 > 0

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspAND T21.param2 = 'Order'

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspGROUP BY T21.location, T21.number

    &nbsp&nbsp&nbsp&nbsp) T2

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspON T1.location = T2.location

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspAND T1.number = T2.number

  • Ok this is great stuff but if I add this row to table1

    location number quantity

    BLUE 10 4

    ...then it doesnt calculate correct.

    the result is still

    location number quantity

    RED 23 5

  • please check this...

    this is giving both the rows

    select temp1.location,temp1.number,(temp1.qty1-temp2.qty2)as Total from

    (select location,number,sum(quantity)as qty1 from test1

    group by Number,location) temp1,

    (select sum(quantity)as qty2 from test11

    where param1>0 and param2='Ord'

    group by Number,location) temp2

  • No, the query then returns

    BLUE 10 1

    It should be

    BLUE 10 4

  • Use an OUTER JOIN and use ISNULL or COALESCE on the outer quantity. eg

    SELECT T1.location, T1.number, T1.quantity - ISNULL(T2.quantity, 0) AS quantity

    FROM

    (

    &nbsp&nbsp&nbsp&nbspSELECT T11.location, T11.number, SUM(T11.quantity) AS quantity

    &nbsp&nbsp&nbsp&nbspFROM Table1 T11

    &nbsp&nbsp&nbsp&nbspGROUP BY T11.location, T11.number

    ) T1

    &nbsp&nbsp&nbsp&nbspLEFT JOIN

    &nbsp&nbsp&nbsp&nbsp(

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspSELECT T21.location, T21.number, SUM(T21.quantity) AS quantity

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspFROM Table2 T21

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspWHERE T21.param1 > 0

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspAND T21.param2 = 'Order'

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspGROUP BY T21.location, T21.number

    &nbsp&nbsp&nbsp&nbsp) T2

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspON T1.location = T2.location

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspAND T1.number = T2.number

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply