Solved

sql query - multiple joins with sub select?

Posted on 2016-08-12
8
52 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 46

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
 

Author Comment

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

ss
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

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 46

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

911 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

19 Experts available now in Live!

Get 1:1 Help Now