Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Get rows from both tables

Posted on 2012-03-29
8
Medium Priority
?
222 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

618 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