Re: Pulling data that could be in one of 2 tables
- From: Matt M <MattM@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 26 Oct 2007 09:41:02 -0700
I ended up figuring it out. Thanks for all your help. See my last reply to
Hugo for my solution and let me know if you have any pointers.
"Russell Fields" wrote:
Matt,.
I am having a little trouble following your narrative. (Sorry.) Sample
tables and sample data often help with this problem.
If I understand correctly, for anything you are querying there is always a
row in Table2 and sometimes a row in Table1. Only if Table2 does not have a
value in the OrderType column do you need to look in Table1. Is this more
what you are looking for?
Select t2.*,
CASE
WHEN t2.OrderType IS NOT NULL THEN
t2.OrderType
ELSE
t1.OrderType
END AS OrderType
From Table2 t2 LEFT OUTER JOIN Table1 t1
ON t2.OriginalOrderNum= t1.OriginalOrderNum
Where....
RLF
"Matt M" <MattM@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:7A051347-94E0-4F1E-A4A3-1E831575401A@xxxxxxxxxxxxxxxx
Sounds promising but I'm not sure if it will work.
The Invoices I am looking up are in Table2. What I need to do is pull all
invoices for let's say the month of September, and look up the
corresponding
Original Order with each Invoices Original Order Number in Table2 first.
If
it is not in Table2, I need to look for it in Table1. Once I find the
Order,
I need to be able grab the Order Type from the Order and the Invoice
Amount
which is on the Invoice. Will this still work.
Summary:
Invoices and Orders are in Table2
If Order has not fully shipped it may still be in Table1
Need to pull the Invoice Ammount from Invoice and the Order Type from the
Invoices original Order
"Russell Fields" wrote:
Matt,
I believe that you are looking for a UNION, such as:
SELECT OriginalOrderNum, Column1, Column2, Column3
FROM Table1
WHERE ...
UNION ALL
SELECT OriginalOrderNum, Column1, Column2, Column3
FROM Table2
WHERE OriginalOrderNum NOT IN
(SELECT OriginalOrderNum FROM Table1)
AND ...
Read about UNION and UNION ALL since they are not identical. The above
query can then be used with whatever other reporting criteria you have.
RLF
"Matt M" <MattM@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:84AF08D4-F845-4412-A7BC-A429110F736E@xxxxxxxxxxxxxxxx
Note: I am using this to create a report with SRS
I need to pull some data that could possibly be sitting in one of two
tables. Table1 is the "Current" table that holds Orders that have not
shipped
yet. Once all of items on the Order have shipped the Order moves to
Table2
and an invoice is created. However, there are times where only part of
the
order ships so there is an Invoice placed in Table2 but the Order still
remains in Table1. The invoice also has a field that always points to
the
original Order number. And Table1 and Table2 have the same exact fields
as
each other.
Here is what I need to do. I need to go through all of the Invoices in
Table2 for a certain time period, and Group them by Order Type (Which
is
stored in the Order record). The problem is, if the order does not
exist
in
Table 2 yet, I need to get the Order type from Table1. I am sure how
exactly
to do this. Is it something in the SQL query like an If or Case
Statement?
Then I though about looking into Stored Procedures but I dont know much
about
using them, especially in SRS so I would need some pointers.
Maybe something like:
Select *, If(Table1.OriginalOrderNum in Table2 then Table2.OrdType else
Table1.Ordtype)
From Table1
Where....
I know that is totally incorrect but I hope it clarifies things.
Once I have that squared away, I just need to sum up the Invoice
amounts
in
each Order Type Group, but that should be easy enough if I can figure
out
the
first part.
Was that clear enough? Let me know if you need any clarification.
- References:
- Re: Pulling data that could be in one of 2 tables
- From: Russell Fields
- Re: Pulling data that could be in one of 2 tables
- From: Matt M
- Re: Pulling data that could be in one of 2 tables
- From: Russell Fields
- Re: Pulling data that could be in one of 2 tables
- Prev by Date: Re: Pulling data that could be in one of 2 tables
- Next by Date: Re: Pulling data that could be in one of 2 tables
- Previous by thread: Re: Pulling data that could be in one of 2 tables
- Next by thread: Re: Select Formatted Text From Table
- Index(es):
Relevant Pages
|
|