Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 4729

Union all

$
0
0

Can anybody help me in solving this problem
I have two tables. I wanted to add the values in two columns and to get a grand total.
e.g

    Table 1
    pid      qty     salqty
    211    200      50

   

     Table 2
    pid     qty   yr
    210   10   2016
    211    5    2016
    212    2    2016
    211    3    2015

I wanted to get the grand total of Pid 211 for the year 2016 from these two tables (Grand Total 155)

 I am coding the same as follows:

    Select Pid, Sum(Table1.qty-Salqty)
    from Table1
    where Table1.pid=table2.pid
    and Group by Pid
    UNION ALL
    Select Table2.pid, sum(Table2.qty)
    from Table2
    Group by Table2.pid


Appreciate help


Viewing all articles
Browse latest Browse all 4729

Trending Articles