[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Get rows from both tables

Posted on 2012-03-29
8
Medium Priority
?
224 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 1600 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
Industry Leaders: 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!

 
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 400 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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…
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Suggested Courses

834 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