Adding Output from two SQL Scripts

Posted on 2011-02-15
Medium Priority
Last Modified: 2012-06-21
SQL 2008

I have two Tables :-


I need to Add BalanceReport.Qty to TransactionReport.Qty for its Corresponding NDC Column

Incase if NDC is not matching, then i need to just include the BalanceReport - NDC:Qty to Transaction Report.

I really need experts help to resolve this issue

select Id,NDC,Qty,CreatedDate from BalanceReport

select		GenericCode,  
			Sum([Dispensed Qty]) as TotalDispensed
from		TransactionReport    
Group By  GenericCode,NDC

Open in new window

Question by:chokka
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 50

Accepted Solution

Lowfatspread earned 1500 total points
ID: 34901304
basically like this...

i cannot work out from your statement which select you wish modifying...
select Id,NDC,Qty +coalesce((select sum([dispensed qty]) from transactionreport as x where x.ndc=b.ndc),0) as qty
,CreatedDate from BalanceReport as b

Open in new window


Author Comment

ID: 34901355
I did it in this way.

Anyways thank you for your help
Create Proc usp_TotalTransaction

declare @TempTable table
	NDC nvarchar(1000), 
	Qty nvarchar(1000) 

Insert Into @TempTable (NDC,Qty)
			Sum(TR.[Dispensed Qty]) as Qty
from		TransactionReport TR    
Group By    TR.NDC

Insert Into @TempTable (NDC,Qty)
select BR.NDC,BR.Qty  from BalanceReport BR

select NDC,Sum(CONVERT(INT, Qty)) as Qty from  @TempTable
Group By NDC


Open in new window


Author Closing Comment

ID: 34901358
Thank you

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question