?
Solved

sql query - multiple joins with sub select?

Posted on 2016-08-12
8
Medium Priority
?
62 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 53

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 53

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

862 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