?
Solved

Issue on SQL - User Defined Function

Posted on 2011-02-16
4
Medium Priority
?
275 Views
Last Modified: 2012-06-27
SQL 2008.

I have posted on ID: 26826918, But i am opening the issue again.

I am trying to rewrite the below script in either User Defined Function or View.

I received the response from Post Id :26826918,
Based on the UserDefined Function or View provided from that Post Id,I checked my output with database and realized there is a mismatch in the records.

If you happened to see the Excel Sheet in the Output - Under the NDC Column, i am getting Drug Name and under DrugName i am getting NDC. Both are swapped.
declare @TempTable table  
(  
[Generic Code] nvarchar(1000),
 NDC nvarchar(1000),   
 [Drug Name]	nvarchar(1000),   
 Qty nvarchar(1000)   
)  
  
Insert Into @TempTable ([Generic Code],NDC,[Drug Name],Qty)  
select    
   TR.[Generic Code],  
   TR.NDC,     
   TR.[Drug Name],
   Sum(TR.[Dispensed Qty]) as Qty  
from  TransactionReport TR      
Group By     TR.[Generic Code],TR.NDC,TR.[Drug Name]
  
Insert Into @TempTable ([Generic Code],NDC,[Drug Name],Qty)   
select Dr.GenericCode as [Generic Code],Dr.Name as [Drug Name],Br.NDC as [NDC],Br.Qty as [Qty] from BalanceReport Br
Inner Join Drug Dr
on 
stuff(Dr.DrugNDCNbr,case when Dr.DrugNDCType in (50, 56) then 1 when Dr.DrugNDCType in (51, 57) then 6    when Dr.DrugNDCType = 52 then 10     end, 0, '0') = Br.NDC

  
  

  select [Generic Code],NDC,[Drug Name],Qty from @TempTable

Open in new window

SampleOutput-.xls
UserDefinedFunction.txt
0
Comment
Question by:chokka
  • 2
  • 2
4 Comments
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 34912518
not sure what you expect...

the problem appears to be in your initial data... sort it out there...
0
 

Author Comment

by:chokka
ID: 34916747
I posted this

http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SQL_Server_2008/Q_26826918.html

Based on this post, i received two different SQL Queries. When i verified the output, i realized that some values are swapped their column.

I have attached the output as Excel Sheet in the above posting.

Expected :- We need to redefined the script in a View or UserDefined Function format - which helps me to execute as SELECT Statement


0
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 1500 total points
ID: 34920506
so in that previous post you need to correct line 19

select Dr.GenericCode as [Generic Code],Dr.Name as [Drug Name],Br.NDC as [NDC],Br.Qty as [Qty] from BalanceReport Br

and change the order of the select to match the Insert (line 18)

e.g.
select Dr.GenericCode as [Generic Code],Br.NDC as [NDC],Dr.Name as [Drug Name],Br.Qty as [Qty] from BalanceReport Br


I still do not see what/why have it as a view/function not clear why that would help either?
0
 

Author Closing Comment

by:chokka
ID: 35044427
Thanks
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Creating a Cordova application which allow user to save to/load from his Dropbox account the application database.
Microsoft Jet database engine errors can crop up out of nowhere to disrupt the working of the Exchange server. Decoding why a particular error occurs goes a long way in determining the right solution for it.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

571 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