Insert Records to a Table from Different Tables
Lets say we are executing this query below to retrieve each customer and the amount associated to a table 

"INSERT INTO tblReceiptDue (Dealer, Amount) SELECT CustomerName, SUM(CASE WHEN VoucherType = 'Sales' then Outbound ELSE  - Inbound END) AS AMOUNT from tblSaleStatementCustomer  WHERE CustomerType = 'Dealer' GROUP BY CustomerName"

Which display the data like below

DEALER                AMOUNT
------------------------------------------------
ABC                          2000
XYZ                          1000    

However I have one more table TABLE2 which contains two columns

DEALER                OPENING
-------------------------------------------------------
ABC                               5000
XYZ                               7000

I want to modify my query in such a way that AMOUNT column should also add OPENING from TABLE2 So that I must get the result like below

DEALER                AMOUNT
------------------------------------------------
ABC                            7000
XYZ                            8000    

Can somebody help ?

Thanks
August 30th, 2015 4:50am

You can JOIN the tables  on Dealer for example  like

SELECT Dealer, AMOUNT+OPENING as AMOUNT FROM t1 JOIN t2 ON .......

Free Windows Admin Tool Kit Click here and download it now
August 30th, 2015 5:20am

Thanks for your reply, Uri Dimant !

Actually I am new to t-sql, could you please try and show me the query by modifying my query

"INSERT INTO tblReceiptDue (Dealer, Amount) SELECT CustomerName, SUM(CASE WHEN VoucherType = 'Sales' then Outbound ELSE  - Inbound END) AS AMOUNT fromtblSaleStatementCustomer  WHERE CustomerType = 'Dealer' GROUP BY CustomerName"

Thanks

August 30th, 2015 5:42am

Hi Sid,

Please try below script to achieve your scenario,

Declare @tempt1 table
(Dealer VARCHAR(50),AMOUNT INT)
insert into @tempt1
select 'ABC',2000 Union ALL
select 'XYZ',1000 

Declare @tempt2 table
(Dealer VARCHAR(50),OPENING INT)
insert into @tempt2
select 'ABC',5000 Union ALL
select 'XYZ',7000 

Select * from @tempt1
Select * from @tempt2

Select A.Dealer, SUM(AMOUNT+OPENING) as AMOUNT from @tempt1 A
Join @tempt2 B on A.DEALER=B.DEALER
Group by A.Dealer

Free Windows Admin Tool Kit Click here and download it now
August 30th, 2015 5:46am

Hi Sid,

If you are new in SQL then I suggest you to please learn and understand the SQL scripting from below link.

http://www.w3schools.com/sql/

August 30th, 2015 5:52am

Sid,

Why you have written CASE inside SUM.

CASE WHEN VoucherType = 'Sales' then Outbound ELSE  - Inbound END

Could you please clear here what you want to achieve then probably we can give you proper answer.

Free Windows Admin Tool Kit Click here and download it now
August 30th, 2015 5:54am

Thanks Kamlesh Kumar,

Lets say we are executing this query below to retrieve each customer and the amount associated to a table 

"INSERT INTO tblReceiptDue (Dealer, Amount) SELECT CustomerName, SUM(Inbound) AS AMOUNT fromtblSaleStatementCustomer GROUP BY CustomerName"

Which display the data like below

DEALER                AMOUNT
------------------------------------------------
ABC                          2000
XYZ                          1000    

However I have one more table TABLE2 which contains two columns

DEALER                OPENING
-------------------------------------------------------
ABC                               5000
XYZ                               7000

I want to modify my query in such a way that AMOUNT column should also add OPENING from TABLE2 So that I must get the result like below

DEALER                AMOUNT
------------------------------------------------
ABC                            7000
XYZ                            8000    

Can you help me now, I hope I cleared everything.

Thanks

August 30th, 2015 6:03am

Please modify your as below script,

INSERT INTO tblReceiptDue (Dealer, Amount)
Select A.Dealer [CustomerName], SUM(AMOUNT+OPENING) as AMOUNT from tblSaleStatementCustomer A
Join TABLE2 B on A.DEALER=B.DEALER

you can achieve your result with using JOIN, Please try this. 

And Please read my first answer also, already gave you simple and understandable script.

Free Windows Admin Tool Kit Click here and download it now
August 30th, 2015 6:11am

Hi Sid,

Kamlesh should work in your case

Declare @tempt1 table
(Dealer VARCHAR(50),AMOUNT INT)
insert into @tempt1
select 'ABC',2000 Union ALL
select 'XYZ',1000 

Declare @tempt2 table
(Dealer VARCHAR(50),OPENING INT)
insert into @tempt2
select 'ABC',5000 Union ALL
select 'XYZ',7000 

Select * from @tempt1
Select * from @tempt2

Select A.Dealer, SUM(AMOUNT+OPENING) as AMOUNT from @tempt1 A
Join @tempt2 B on A.DEALER=B.DEALER
Group by A.Dealer

August 30th, 2015 6:14am

Here you go

WITH cte
AS
(
SELECT CustomerName, SUM(CASE WHEN VoucherType = 'Sales' then Outbound ELSE  - Inbound END) AS AMOUNT 
from 
tblSaleStatementCustomer  WHERE CustomerType = 'Dealer' GROUP BY CustomerName
) INSERT INTO tblReceiptDue (Dealer, Amount) 
  SELECT CustomerName ,SUM(AMOUNT+OPENING) as AMOUNT FROM cte JOIN table1 ON
   cte.CustomerName=table1.dealre
   GROUP BY CustomerName
 

Free Windows Admin Tool Kit Click here and download it now
August 30th, 2015 6:42am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics