Solved

Get rows from both tables

Posted on 2012-03-29
8
211 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 2

Author Comment

by:chrisryhal
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 16

Assisted Solution

by:DcpKing
DcpKing earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
ignore my last comment, your last query should work as it is now.
0
 
LVL 2

Author Closing Comment

by:chrisryhal
Comment Utility
Thanks all
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

728 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

10 Experts available now in Live!

Get 1:1 Help Now