Solved

T-SQL Extracting Distinct Records

Posted on 2014-02-24
4
759 Views
Last Modified: 2014-02-24
Hello:

Below is my T-SQL query and attached are the results of running the query.  Until I added the last column, DSCRIPTN, I only received four rows of data.  Four, indeed, is the correct number.  I'm surprised that adding just one new column from the same table as other columns caused this.
 
Unfortunately, I do need the data from the DSCRIPTN column as well.  

I tried using the "DISTINCT" keyword.  But, that did not solve the problem.

How do I allow for this last column but have the correct number of rows returned, at the same time?

Thanks!

TBSupport

SELECT AAG30000.JRNENTRY, AAG30000.aaTRXSource, AAG30001.DEBITAMT, AAG30001.CRDTAMNT, GL00105.ACTNUMST, GL00100.ACTDESCR, AAG00401.aaTrxDimCode, AAG00401.aaTrxDimCodeDescr,
GL20000.ORMSTRNM, GL20000.TRXDATE, GL20000.DSCRIPTN
FROM AAG30003
INNER JOIN AAG30000 on
AAG30000.aaGLHdrID =  AAG30003.aaGLHdrID
 INNER JOIN EBSGP..AAG30001
 on
 AAG30000.aaGLHdrID =  AAG30001.aaGLHdrID
  INNER JOIN [EBSGP]..GL00100
 on
 AAG30001.ACTINDX =  GL00100.ACTINDX
   INNER JOIN [EBSGP]..GL00105
 on
 GL00100.ACTINDX =  GL00105.ACTINDX
 INNER JOIN EBSGP..AAG00401
 on
 AAG30003.[aaTrxDimID] =  AAG00401.[aaTrxDimID] and
 AAG30003.aaTrxCodeID = AAG00401.aaTrxDimCodeID
 INNER JOIN GL20000 ON
 AAG30000.JRNENTRY = GL20000.JRNENTRY
where AAG30000.JRNENTRY = '80125'
GROUP BY AAG30001.aaGLDistID, AAG30000.JRNENTRY, AAG30000.aaTRXSource, AAG30001.DEBITAMT, AAG30001.CRDTAMNT, GL00105.ACTNUMST, GL00100.ACTDESCR, AAG00401.aaTrxDimCode, AAG00401.aaTrxDimCodeDescr,
GL20000.ORMSTRNM, GL20000.TRXDATE, GL20000.DSCRIPTN
AA.docx
0
Comment
Question by:TBSupport
  • 2
  • 2
4 Comments
 
LVL 11

Expert Comment

by:John_Vidmar
ID: 39883513
You have no aggregate-functions but you have a group-by clause... I would eliminate the group-by and optionally add distict to the select-clause.
0
 
LVL 1

Author Comment

by:TBSupport
ID: 39883568
Hi John_Vidmar:

I get the same result.

TBSupport
0
 
LVL 11

Accepted Solution

by:
John_Vidmar earned 500 total points
ID: 39883611
INNER JOIN GL20000 ON
 AAG30000.JRNENTRY = GL20000.JRNENTRY

The above statement does not uniquely identify a GL20000.DSCRIPTN for a GL20000.JRNENTRY... would you be able to add additional filters to the on-clause against AAG30000 (or another table) ?
0
 
LVL 1

Author Comment

by:TBSupport
ID: 39883741
Thanks, John_Vidmar!

You led me down the right path!  I simply took the phrase "GL20000.DSCRIPTN" and made it "GL20000_1.DSCRIPN".  To do so, I added the following clause:

 INNER JOIN GL20000 AS GL20000_1
 ON AAG30001.ACTINDX = GL20000_1.ACTINDX

Thanks, Again!

TBSupport
0

Featured Post

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.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 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

23 Experts available now in Live!

Get 1:1 Help Now