Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Issue on SQL - User Defined Function

Posted on 2011-02-16
4
Medium Priority
?
274 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
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…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

916 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