Solved

sql query - multiple joins with sub select?

Posted on 2016-08-12
8
44 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 45

Expert Comment

by:Vitor Montalvão
Comment Utility
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
Comment Utility
Instead of 1060 DeptID I want DeptDesc to be bound to the repeater
0
 

Author Comment

by:Ahelbling
Comment Utility
Here are the current results.

ss
0
 

Author Comment

by:Ahelbling
Comment Utility
and the rest...

ss
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

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

Author Comment

by:Ahelbling
Comment Utility
Trans table
Trans.PNG
Employee
Emp.PNG
Dept
Dept.PNG
0
 
LVL 40

Expert Comment

by:Sharath
Comment Utility
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 45

Accepted Solution

by:
Vitor Montalvão earned 500 total points
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

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.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
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.

772 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

10 Experts available now in Live!

Get 1:1 Help Now