?
Solved

sql query - multiple joins with sub select?

Posted on 2016-08-12
8
Medium Priority
?
60 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
8 Comments
 
LVL 51

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 41

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 51

Accepted Solution

by:
Vitor Montalvão earned 2000 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

How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

752 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