Solved

Issue on SQL - User Defined Function

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Query 3 48
SQL Query with Sum and Detail rows 2 40
Linking a DMV to a database id/sql text in SQL server 2008 8 46
Max Consumption Rate (MCR) 3 32
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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…

914 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