Solved

Get rows from both tables

Posted on 2012-03-29
8
216 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

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…
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…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

820 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