Solved

sql query - multiple joins with sub select?

Posted on 2016-08-12
8
57 Views
Last Modified: 2016-08-15
Having issues getting the results I am after with this query.  I need the DeptDesc value from [Directory].[dbo].[_Department] table based on the LawsonDept value in the [Directory].[dbo].[_Employees] table for each TID value from the [CorpCard_new].[dbo].[Trans] table that has a LogID of 6332.  Current query below returns 17 rows for each TID with a LogID of 6332.  I am wanting just 1 per TID.

[Trans].ACCT_UNIT = [_Employees].LawsonDept

[_Department].DeptID  = [_Employees].DeptID

ss
SELECT 
t.*, CONVERT(VARCHAR(8), TranDate, 1) As TDate, e.ExpenseDesc, d.DeptName
FROM
[CorpCard_new].[dbo].[Trans] t 
JOIN [CorpCard_new].[dbo].[ExpType] e ON t.ExpID=e.ExpID
JOIN [Directory].[dbo].[_Employees] emp ON t.ACCT_UNIT=emp.LawsonDept
JOIN (SELECT DeptID, DeptName FROM [Directory].[dbo].[_Department]) d ON emp.DeptID=d.DeptID
WHERE 
LogID = 6332

Open in new window

0
Comment
Question by:Ahelbling
  • 5
  • 2
8 Comments
 
LVL 48

Expert Comment

by:Vitor Montalvão
ID: 41753938
You have a t.* that returns all columns from Trans table. Is really that what you want?
Can you provide the actual results and based on that what you really expected to be returned by the query?

Btw, your query can be simplified to:
SELECT t.*, CONVERT(VARCHAR(8), TranDate, 1) As TDate, e.ExpenseDesc, d.DeptName
FROM [CorpCard_new].[dbo].[Trans] t 
	INNER JOIN [CorpCard_new].[dbo].[ExpType] e ON t.ExpID=e.ExpID
	INNER JOIN [Directory].[dbo].[_Employees] emp ON t.ACCT_UNIT=emp.LawsonDept
	INNER JOIN [Directory].[dbo].[_Department] d ON emp.DeptID=d.DeptID
WHERE LogID = 6332

Open in new window

0
 

Author Comment

by:Ahelbling
ID: 41753956
Instead of 1060 DeptID I want DeptDesc to be bound to the repeater
0
 

Author Comment

by:Ahelbling
ID: 41753961
Here are the current results.

ss
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:Ahelbling
ID: 41753963
and the rest...

ss
0
 

Author Comment

by:Ahelbling
ID: 41753965
returns 102 mostly duplicated records.  I only need the 6 unique ones.
0
 

Author Comment

by:Ahelbling
ID: 41754064
Trans table
Trans.PNG
Employee
Emp.PNG
Dept
Dept.PNG
0
 
LVL 40

Expert Comment

by:Sharath
ID: 41754585
Use DISTINCT and list the columns that you want in SELECT clause.
SELECT DISTINCT CONVERT(VARCHAR(8), TranDate, 1) As TDate, e.ExpenseDesc, d.DeptName
FROM [CorpCard_new].[dbo].[Trans] t 
    INNER JOIN [CorpCard_new].[dbo].[ExpType] e ON t.ExpID=e.ExpID
    INNER JOIN [Directory].[dbo].[_Employees] emp ON t.ACCT_UNIT=emp.LawsonDept
    INNER JOIN [Directory].[dbo].[_Department] d ON emp.DeptID=d.DeptID
WHERE LogID = 6332

Open in new window

0
 
LVL 48

Accepted Solution

by:
Vitor Montalvão earned 500 total points
ID: 41756046
But do you need all columns for Trans table to be returned?

And don't use DISTINCT since it's a performance killer. Instead use GROUP BY to get unique values.
I'm giving you an example with only 4 Trans's columns. You may need to add more as you need:
SELECT t.TID, t.LogID, t.Merchant, t.TranAmt,, CONVERT(VARCHAR(8), TranDate, 1) As TDate, e.ExpenseDesc, d.DeptName
FROM [CorpCard_new].[dbo].[Trans] t 
	INNER JOIN [CorpCard_new].[dbo].[ExpType] e ON t.ExpID=e.ExpID
	INNER JOIN [Directory].[dbo].[_Employees] emp ON t.ACCT_UNIT=emp.LawsonDept
	INNER JOIN [Directory].[dbo].[_Department] d ON emp.DeptID=d.DeptID
WHERE LogID = 6332
GROUP BY t.TID, t.LogID, t.Merchant, t.TranAmt, CONVERT(VARCHAR(8), TranDate, 1) As TDate, e.ExpenseDesc, d.DeptName

Open in new window

0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Upgrading my SSIS package in VS 2012 6 62
Location of Dynamics AX Service accounts in SQL 3 16
SQL R 21 28
Setting variables in a stored procedure 5 22
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

830 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