Advertisement

05.13.2008 at 09:12AM PDT, ID: 23398387
[x]
Attachment Details

Function vs Table Join

Asked by Wedmore in MS SQL Server

Tags: Microsoft, SQL, 2000

I import spreadsheets from various departments.  Each has their own way of recording an employees name eg some depts include the employees initial, others dont, some put a full stop have the initial, some record the name as <lastname>, <firstname> and other cases its recorded as <firstname lastname>.  You get the idea.  None of them use their NT login id, but to some of our backend databases - this is the unique identifier.

So I created a table called NameLookup which stores the NT User ID for each possible alias that an employee might have eg:

Alias            NTUserID
Bob Smith    bsmith
Smith, Bob   bsmith
Brian Smith  brsmith
Sue Green   sgreen

In the example in the code snippet there are two possible solutions to the same required query result.  tblPayroll stores its data based on NT User ID.  tblEmployeeSales on the other hand, does not and has one row per day per employee of their total sales for that day..  This is where I need to use the alias in order to join on tblPayroll.

This is just one example.  There are many other tables which need to be joined with NameLookup.

Right now the function method returns just as quickly, if not faster than the JOIN method.  However, I dont have that much data to compare - say 70 active employees each with maybe 3 to 6 aliases.  So as time goes on, this will increase.  Should I expect the function performance to degrade?
Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
-- Solution # 1 with JOIN ----------------------------------------------
 
SELECT emp.EmployeeName, pay.Scale, pay.HourlyRate, emp.SalesTotal
  FROM tblPayroll pay
 INNER JOIN (SELECT n.NTUserID, e.EmployeeName, SUM(e.Total) 'SalesTotal'
               FROM tblEmployeeSales e
              INNER JOIN NameLookup n ON n.Alias = e.EmployeeName
              GROUP BY n.NTUserID, e.EmployeeName) Emp ON Emp.NTUSerID = pay.NTUserName
 
-- Solution # 2 with UDF ----------------------------------------------
 
CREATE FUNCTION fnNTID (@NamePass Varchar(50))
 
RETURNS Varchar(50)
 
AS  
BEGIN 
  DECLARE @NTID as Varchar(50)
 
  SELECT @NTID = NTUserID 
    FROM NameLookup
   WHERE Alias = @NamePass
 
  RETURN ISNULL(@NTID, @NamePass)
END
 
 
SELECT emp.EmployeeName, pay.Scale, pay.HourlyRate, SUM(emp.Total) 'SalesTotal'
  FROM tblPayroll pay
 INNER JOIN tblEmployeeSales emp ON dbo.fnNTID(emp.EmployeeName) = pay.NTUserName
 GROUP BY emp.EmployeeName, pay.Scale, pay.HourlyRate
[+][-]05.13.2008 at 09:31AM PDT, ID: 21556732

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]05.13.2008 at 09:44AM PDT, ID: 21556848

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.13.2008 at 11:41AM PDT, ID: 21557982

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: MS SQL Server
Tags: Microsoft, SQL, 2000
Sign Up Now!
Solution Provided By: Zberteoc
Participating Experts: 3
Solution Grade: A
 
 
[+][-]05.13.2008 at 02:51PM PDT, ID: 21559615

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.14.2008 at 06:25AM PDT, ID: 21563859

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628