Solved

Issue on SQL - User Defined Function

Posted on 2011-02-16
4
256 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 500 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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Suggested Solutions

This article describes some very basic things about SQL Server filegroups.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now