Solved

Get rows from both tables

Posted on 2012-03-29
8
214 Views
Last Modified: 2012-03-29
I am using the below query to get results from my primary table [GLJrnDtl]

I have a join to another table and all results are coming in great.  The problem I'm just not understanding is that I need results from BOTH tables actually.  There are certain records in [InvcHed] that ARE NOT contained within [GLJrnDtl] yet I DO need all records from [GLJrnDtl].

So basically,  I need to know how to show results from both tables.  


SELECT     GLJrnDtl.FiscalYear, GLJrnDtl.FiscalPeriod, GLJrnDtl.PostedBy, GLJrnDtl.JournalNum, GLJrnDtl.JournalLine, GLJrnDtl.Description, GLJrnDtl.ARInvoiceNum,
                      GLJrnDtl.SegValue1, GLJrnDtl.SegValue2, GLJrnDtl.JournalCode, GLJrnDtl.DebitAmount, GLJrnDtl.CreditAmount, InvcHead.InvoiceNum
FROM         GLJrnDtl WITH (NOLOCK) LEFT OUTER JOIN
                      InvcHead ON GLJrnDtl.ARInvoiceNum = InvcHead.InvoiceNum
WHERE     (GLJrnDtl.FiscalYear = '2012') AND (GLJrnDtl.FiscalPeriod = '2') AND (GLJrnDtl.SegValue1 BETWEEN '400000' AND '499999')
ORDER BY GLJrnDtl.JournalCode
0
Comment
Question by:chrisryhal
  • 4
  • 3
8 Comments
 
LVL 39

Expert Comment

by:appari
ID: 37785300
change LEFT OUTER JOIN to Full Outer Join.

Full outer join returns all the records from both the tables, returning null values in case matching records(based on join condition) are not found.

try

SELECT     GLJrnDtl.FiscalYear, GLJrnDtl.FiscalPeriod, GLJrnDtl.PostedBy, GLJrnDtl.JournalNum, GLJrnDtl.JournalLine, GLJrnDtl.Description, GLJrnDtl.ARInvoiceNum,
                      GLJrnDtl.SegValue1, GLJrnDtl.SegValue2, GLJrnDtl.JournalCode, GLJrnDtl.DebitAmount, GLJrnDtl.CreditAmount, InvcHead.InvoiceNum
FROM         GLJrnDtl WITH (NOLOCK) Full OUTER JOIN
                      InvcHead ON GLJrnDtl.ARInvoiceNum = InvcHead.InvoiceNum
WHERE     (GLJrnDtl.FiscalYear = '2012') AND (GLJrnDtl.FiscalPeriod = '2') AND (GLJrnDtl.SegValue1 BETWEEN '400000' AND '499999')
ORDER BY GLJrnDtl.JournalCode
0
 
LVL 39

Accepted Solution

by:
appari earned 400 total points
ID: 37785308
also you may need to shift conditions in where clause to join condition.

SELECT     GLJrnDtl.FiscalYear, GLJrnDtl.FiscalPeriod, GLJrnDtl.PostedBy, GLJrnDtl.JournalNum, GLJrnDtl.JournalLine, GLJrnDtl.Description, GLJrnDtl.ARInvoiceNum,
                      GLJrnDtl.SegValue1, GLJrnDtl.SegValue2, GLJrnDtl.JournalCode, GLJrnDtl.DebitAmount, GLJrnDtl.CreditAmount, InvcHead.InvoiceNum
FROM         GLJrnDtl WITH (NOLOCK) Full OUTER JOIN
                      InvcHead ON GLJrnDtl.ARInvoiceNum = InvcHead.InvoiceNum
-- Check the data returned by prev query and in case missing records try this
AND (GLJrnDtl.FiscalYear = '2012') AND (GLJrnDtl.FiscalPeriod = '2') AND (GLJrnDtl.SegValue1 BETWEEN '400000' AND '499999')
ORDER BY GLJrnDtl.JournalCode
0
 
LVL 2

Author Comment

by:chrisryhal
ID: 37785323
Looks like I'm getting better results from the shifted condition you stated as teh previous FULL OUTER JOIN didn't work and I actually have tried that before.

Seems I'm getting records I DON'T want though.  Example, I need only GLJrnDtl.FiscalYear = '2012' but now I'm getting 2011 and on and on.  Here my modified query from your assistance.

I think I'm almost there.   Basically, its an accounting query for a report I'm writting.  Everything in GLJrnDtl has been posted to the General Ledger but I need to also include the invoices from InvcHed WHERE InvcHed.Posted = '0' as THOSE invoices haven't yet been posted to the ledger so I need to include those as well

SELECT     GLJrnDtl.FiscalYear, GLJrnDtl.FiscalPeriod, GLJrnDtl.PostedBy, GLJrnDtl.JournalNum, GLJrnDtl.JournalLine, GLJrnDtl.Description, GLJrnDtl.ARInvoiceNum,
                      GLJrnDtl.SegValue1, GLJrnDtl.SegValue2, GLJrnDtl.JournalCode, GLJrnDtl.DebitAmount, GLJrnDtl.CreditAmount, InvcHead.InvoiceNum
FROM         GLJrnDtl WITH (NOLOCK) Full OUTER JOIN
                      InvcHead ON GLJrnDtl.ARInvoiceNum = InvcHead.InvoiceNum
AND (GLJrnDtl.FiscalYear = '2012') AND (GLJrnDtl.FiscalPeriod = '2') AND (GLJrnDtl.SegValue1 BETWEEN '400000' AND '499999')
ORDER BY GLJrnDtl.JournalCode
0
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.

 
LVL 2

Author Comment

by:chrisryhal
ID: 37785330
This seems to be working.  Your opinion?

Notice my OR statement

SELECT     GLJrnDtl.FiscalYear, GLJrnDtl.FiscalPeriod, GLJrnDtl.PostedBy, GLJrnDtl.JournalNum, GLJrnDtl.JournalLine, GLJrnDtl.Description, GLJrnDtl.ARInvoiceNum,
                      GLJrnDtl.SegValue1, GLJrnDtl.SegValue2, GLJrnDtl.JournalCode, GLJrnDtl.DebitAmount, GLJrnDtl.CreditAmount, InvcHead.InvoiceNum
FROM         GLJrnDtl WITH (NOLOCK) Full OUTER JOIN
                      InvcHead ON GLJrnDtl.ARInvoiceNum = InvcHead.InvoiceNum
WHERE ((GLJrnDtl.FiscalYear = '2012') AND (GLJrnDtl.FiscalPeriod = '2') AND (GLJrnDtl.SegValue1 BETWEEN '400000' AND '499999')) OR (InvcHead.Posted = '0')
ORDER BY GLJrnDtl.JournalCode
0
 
LVL 16

Assisted Solution

by:DcpKing
DcpKing earned 100 total points
ID: 37785336
Lets say you have 1000 records in GLJrnDtl (with invoice numbers 1 to 10000). Then with your query you should get 1000 records - one for each of the GLJrnDtl records. You may get the InvoiceNum field from the InvcHead table left as null sometimes, if there are missing records in the InvcHead table.

You are worrying about records in InvcHead that aren't showing up in this query: if all you're interested in is getting all the data from GLJrnDtl then you should be fine. If you want to see an entry for those records too, make it a full outer join instead of a left outer join, and you'll get some records where InvcHead.InvoiceNum has a value but none of the fields from GLJrnDtl have anything other than null.

To test it, make yourself four records - two in each table. Make just one of each pair share an invoice number. Then run your query on those and see what you get as you play with the four types of join...

Good luck

Mike
0
 
LVL 39

Expert Comment

by:appari
ID: 37785363
your last query seems OK. What to do you want to do in case

(GLJrnDtl.FiscalYear = '2012') AND (GLJrnDtl.FiscalPeriod = '2') AND (GLJrnDtl.SegValue1 BETWEEN '400000' AND '499999')
is true and no matching records in InvcHead table. if you want to show the records from GLJrnDtl checnge the or condition to
isnull(InvcHead.Posted ,'0') = '0'
0
 
LVL 39

Expert Comment

by:appari
ID: 37785390
ignore my last comment, your last query should work as it is now.
0
 
LVL 2

Author Closing Comment

by:chrisryhal
ID: 37785432
Thanks all
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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.
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.

809 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