Welcome to dbForumz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Pulling data that could be in one of 2 tables

 
   Database Forums (Home) -> MSEQ RSS
Next:  Capturing Query Text of Prepared Statements in Pr..  
Author Message
Matt M

External


Since: Jul 27, 2006
Posts: 7



(Msg. 1) Posted: Wed Oct 24, 2007 7:03 am
Post subject: Pulling data that could be in one of 2 tables
Archived from groups: microsoft>public>sqlserver>mseq (more info?)

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.

 >> Stay informed about: Pulling data that could be in one of 2 tables 
Back to top
Login to vote
Russell Fields

External


Since: Feb 21, 2007
Posts: 457



(Msg. 2) Posted: Wed Oct 24, 2007 1:31 pm
Post subject: Re: Pulling data that could be in one of 2 tables [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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" wrote in message

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

 >> Stay informed about: Pulling data that could be in one of 2 tables 
Back to top
Login to vote
Matt M

External


Since: Jul 27, 2006
Posts: 7



(Msg. 3) Posted: Wed Oct 24, 2007 1:31 pm
Post subject: Re: Pulling data that could be in one of 2 tables [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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" wrote in message
>
> > 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.
>
>
>
 >> Stay informed about: Pulling data that could be in one of 2 tables 
Back to top
Login to vote
Russell Fields

External


Since: Feb 21, 2007
Posts: 457



(Msg. 4) Posted: Thu Oct 25, 2007 9:59 am
Post subject: Re: Pulling data that could be in one of 2 tables [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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" wrote in message

> 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" wrote in message
>>
>> > 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.
>>
>>
>>
 >> Stay informed about: Pulling data that could be in one of 2 tables 
Back to top
Login to vote
Matt M

External


Since: Jul 27, 2006
Posts: 7



(Msg. 5) Posted: Thu Oct 25, 2007 9:59 am
Post subject: Re: Pulling data that could be in one of 2 tables [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

I know, it took me a while to understand what they wanted to do as well.
Basically, the database is a Great Plains database. Not sure if you are
familiar with GP at all? Anyway, I'm going to try my best to show you some
sample data.

Table2
SOPTYPE SOPNUMBE ORIGNUMB DOCID DOCDATE
DOCAMT
3 1234 5678 INVOICE 10/21/2000
100
2 5555 6666 ORDER 10/21/2000
100
3 7777 1111 INVOICE 10/21/2000
100
2 5678 2222 ORDER NO FULFIL 10/21/2000 100


Table1
SOPTYPE SOPNUMBE ORIGNUMB DOCID DOCDATE
DOCAMT
2 1234 5543 ORDER 10/21/2000
100
2 1111 3658 ORDER NO FULFIL 10/21/2000 100


SOPTYPE tells whether it is an Invoice(3) or Order(2). So I want to pull all
Invoices from Table2 and find the corresponding DOCID for its Original Order.
In this example Invoice 1234 has its original order (5678) in the same table
as itself (Table2) and Invoice 7777 Has its original order (1111) in Table1
because it has only partial shipped. So for 7777 I need to pull the DOCID
from Table1 and for 1234 I need the DOCID from table2. I also need the
Invoice amount from table2 so I can add it up in SRS.

Does this help? Your sample query you showed me looks promising but it may
take me a little bit to wrap my brain around it.

Thanks,
Matt

"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" wrote in message
>
> > 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" wrote in message
> >>
> >> > 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.
> >>
> >>
> >>
>
>
>
 >> Stay informed about: Pulling data that could be in one of 2 tables 
Back to top
Login to vote
Matt M

External


Since: Jul 27, 2006
Posts: 7



(Msg. 6) Posted: Thu Oct 25, 2007 9:59 am
Post subject: Re: Pulling data that could be in one of 2 tables [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Ok this may help. The query below works if the Orignal Order is located in
Table1 (even though my CASE statement seems backwards to me). If it was moved
to table2 then I just get a Null value. But I think this might give you a
better idea of what I need to do.

I left the original table names in the query:
Table1 = SOP10100
Table2 = SOP30200


SELECT SOP30200.SOPTYPE, SOP30200.SOPNUMBE, SOP30200.ORIGNUMB,
SOP30200.DOCID, SOP30200.DOCDATE, SOP30200.INVODATE,
SOP30200.MSTRNUMB, SOP30200.DOCAMNT, CASE WHEN
(SELECT DOCID
FROM SOP10100
WHERE SOP10100.SOPNUMBE = ORIGNUMB) IS NOT
NULL THEN SOP30200.DOCID ELSE SOP10100.DOCID END AS ORIGDOCID
FROM SOP30200 LEFT OUTER JOIN
SOP10100 ON SOP30200.ORIGNUMB = SOP10100.SOPNUMBE
WHERE (SOP30200.DOCDATE >= CONVERT(DATETIME, '2007-10-01 00:00:00',
102)) AND (SOP30200.SOPTYPE = '3')


"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" wrote in message
>
> > 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" wrote in message
> >>
> >> > 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.
> >>
> >>
> >>
>
>
>
 >> Stay informed about: Pulling data that could be in one of 2 tables 
Back to top
Login to vote
Hugo Kornelis

External


Since: Jan 31, 2006
Posts: 335



(Msg. 7) Posted: Thu Oct 25, 2007 9:56 pm
Post subject: Re: Pulling data that could be in one of 2 tables [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Thu, 25 Oct 2007 07:30:01 -0700, Matt M wrote:

(snip)
>Does this help? Your sample query you showed me looks promising but it may
>take me a little bit to wrap my brain around it.

Hi Matt,

The main reason that this query is ntroublesome to come up with, is that
the design is bad. I'm not sure if you are able to change that (never
worked with GP, and after seeing their table and column names, I hope to
keep it that way). An order should be in the same table, whether it's
partially or fully shipped, or not shipped at all.

Anyway, if you are stuck with the design, than try sometinh like this:

SELECT base.SOPTYPE, base.SOPNUMBE, base.ORIGNUMB,
COALESCE(orig1.SOPTYPE, orig2.SOPTYPE),
COALESCE(orig1.SOPNUMBE, orig2.SOPNUMBE)
FROM table2 AS base
LEFT JOIN table1 AS orig1
ON orig1.SOPNUMBE = base.ORIGNUMBE
LEFT JOIN table2 AS orig2
ON orig2.SOPNUMBE = base.ORIGNUMBE
WHERE base.SOPTYPE = 3; -- Invoices only

(Untested - see www.aspfaq.com/5006 if you prefer a tested reply)

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
 >> Stay informed about: Pulling data that could be in one of 2 tables 
Back to top
Login to vote
Matt M

External


Since: Jul 27, 2006
Posts: 7



(Msg. 8) Posted: Thu Oct 25, 2007 9:56 pm
Post subject: Re: Pulling data that could be in one of 2 tables [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Yes I agree that the design is very hard to work with but unfortunately I am
stuck with it. I havent had a chance to look into your suggestion yet but did
you see my other post. I have a query that is working halfway (See my other
post in this thread for explanation). Here is the query:

SELECT SOP30200.SOPTYPE, SOP30200.SOPNUMBE, SOP30200.ORIGNUMB,
SOP30200.DOCID, SOP30200.DOCDATE, SOP30200.INVODATE,
SOP30200.MSTRNUMB, SOP30200.DOCAMNT, CASE WHEN
(SELECT DOCID
FROM SOP10100
WHERE SOP10100.SOPNUMBE = ORIGNUMB) IS NOT
NULL THEN SOP30200.DOCID ELSE SOP10100.DOCID END AS ORIGDOCID
FROM SOP30200 LEFT OUTER JOIN
SOP10100 ON SOP30200.ORIGNUMB = SOP10100.SOPNUMBE
WHERE (SOP30200.DOCDATE >= CONVERT(DATETIME, '2007-10-01 00:00:00',
102)) AND (SOP30200.SOPTYPE = '3')


"Hugo Kornelis" wrote:

> On Thu, 25 Oct 2007 07:30:01 -0700, Matt M wrote:
>
> (snip)
> >Does this help? Your sample query you showed me looks promising but it may
> >take me a little bit to wrap my brain around it.
>
> Hi Matt,
>
> The main reason that this query is ntroublesome to come up with, is that
> the design is bad. I'm not sure if you are able to change that (never
> worked with GP, and after seeing their table and column names, I hope to
> keep it that way). An order should be in the same table, whether it's
> partially or fully shipped, or not shipped at all.
>
> Anyway, if you are stuck with the design, than try sometinh like this:
>
> SELECT base.SOPTYPE, base.SOPNUMBE, base.ORIGNUMB,
> COALESCE(orig1.SOPTYPE, orig2.SOPTYPE),
> COALESCE(orig1.SOPNUMBE, orig2.SOPNUMBE)
> FROM table2 AS base
> LEFT JOIN table1 AS orig1
> ON orig1.SOPNUMBE = base.ORIGNUMBE
> LEFT JOIN table2 AS orig2
> ON orig2.SOPNUMBE = base.ORIGNUMBE
> WHERE base.SOPTYPE = 3; -- Invoices only
>
> (Untested - see www.aspfaq.com/5006 if you prefer a tested reply)
>
> --
> Hugo Kornelis, SQL Server MVP
> My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
>
 >> Stay informed about: Pulling data that could be in one of 2 tables 
Back to top
Login to vote
Matt M

External


Since: Jul 27, 2006
Posts: 7



(Msg. 9) Posted: Fri Oct 26, 2007 9:40 am
Post subject: Re: Pulling data that could be in one of 2 tables [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

I couldnt figure out how to get your query suggestion to work with this but
after reading your blog I figured out my problem. I was comaparing my results
in my Case statement with Null. So instead of using '=' I used EXISTS and
that did the trick. My resulting query came out to be:

SELECT SOP30200.SOPTYPE, SOP30200.SOPNUMBE, SOP30200.ORIGNUMB,
SOP30200.DOCID, SOP30200.DOCDATE, SOP30200.INVODATE,
SOP30200.MSTRNUMB, SOP30200.DOCAMNT, CASE WHEN EXISTS
(SELECT DOCID
FROM SOP10100
WHERE SOP10100.SOPNUMBE =
SOP30200.ORIGNUMB) THEN SOP10100.DOCID ELSE
(SELECT DOCID
FROM SOP30200 AS History
WHERE History.SOPNUMBE = SOP30200.ORIGNUMB
AND History.SOPTYPE = '2') END AS ORIGDOCID
FROM SOP30200 LEFT OUTER JOIN
SOP10100 ON SOP30200.ORIGNUMB = SOP10100.SOPNUMBE
WHERE (SOP30200.SOPTYPE = '3') AND (SOP30200.DOCDATE >= @BeginDate) AND
(SOP30200.DOCDATE <= @EndDate)

Probably not the most efficient way to do it but it got the job done and my
numbers came out exact.

Thanks to you and Russel for your input.

"Hugo Kornelis" wrote:

> On Thu, 25 Oct 2007 07:30:01 -0700, Matt M wrote:
>
> (snip)
> >Does this help? Your sample query you showed me looks promising but it may
> >take me a little bit to wrap my brain around it.
>
> Hi Matt,
>
> The main reason that this query is ntroublesome to come up with, is that
> the design is bad. I'm not sure if you are able to change that (never
> worked with GP, and after seeing their table and column names, I hope to
> keep it that way). An order should be in the same table, whether it's
> partially or fully shipped, or not shipped at all.
>
> Anyway, if you are stuck with the design, than try sometinh like this:
>
> SELECT base.SOPTYPE, base.SOPNUMBE, base.ORIGNUMB,
> COALESCE(orig1.SOPTYPE, orig2.SOPTYPE),
> COALESCE(orig1.SOPNUMBE, orig2.SOPNUMBE)
> FROM table2 AS base
> LEFT JOIN table1 AS orig1
> ON orig1.SOPNUMBE = base.ORIGNUMBE
> LEFT JOIN table2 AS orig2
> ON orig2.SOPNUMBE = base.ORIGNUMBE
> WHERE base.SOPTYPE = 3; -- Invoices only
>
> (Untested - see www.aspfaq.com/5006 if you prefer a tested reply)
>
> --
> Hugo Kornelis, SQL Server MVP
> My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
>
 >> Stay informed about: Pulling data that could be in one of 2 tables 
Back to top
Login to vote
Matt M

External


Since: Jul 27, 2006
Posts: 7



(Msg. 10) Posted: Fri Oct 26, 2007 9:41 am
Post subject: Re: Pulling data that could be in one of 2 tables [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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" wrote in message
>
> > 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" wrote in message
> >>
> >> > 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.
> >>
> >>
> >>
>
>
>
 >> Stay informed about: Pulling data that could be in one of 2 tables 
Back to top
Login to vote
Hugo Kornelis

External


Since: Jan 31, 2006
Posts: 335



(Msg. 11) Posted: Sat Oct 27, 2007 12:21 am
Post subject: Re: Pulling data that could be in one of 2 tables [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Fri, 26 Oct 2007 09:40:00 -0700, Matt M wrote:

(snip)
>Probably not the most efficient way to do it but it got the job done and my
>numbers came out exact.

Hi Matt,

This way is probably more efficient:

SELECT Main.SOPTYPE, Main.SOPNUMBE, Main.ORIGNUMB,
Main.DOCID, Main.DOCDATE, Main.INVODATE,
Main.MSTRNUMB, Main.DOCAMNT,
COALESCE(Original.DOCID, History.DOCID) AS ORIGDOCID
FROM SOP30200 AS Main
LEFT JOIN SOP10100 AS Original
ON Original.SOPNUMBE = Main.ORIGNUMB
LEFT JOIN SOP30200 AS History
ON History.SOPNUMBE = Main.ORIGNUMB
AND AND History.SOPTYPE = '2'
WHERE Main.SOPTYPE = '3'
AND Main.DOCDATE >= @BeginDate
AND Main.DOCDATE <= @EndDate;

(untested)

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
 >> Stay informed about: Pulling data that could be in one of 2 tables 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Comparing Data between Tables - I messed up and now we have two tables that have slightly different information. What is a query that would allow me to look at the data differences between the two tables? -- Ever Learning Net Admin

GROUP BY's on 3 tables in one SELECT? - Hi there, I'd like to ask you for help with following: having 3 tables: T1 (Person_ID, Product_ID, Costs) T2 (Person_ID, Product_ID, Balancies) T3 (Product_ID, Product_Type) I have simple GROUP BY query: SELECT T1.Person_ID, T1.Product_ID, MAX(Costs)..

Matching fileds between two tables - I hope I can explain this a little better. I have two tables that I need information from. The first table has all but two fields that I need. I am having two problems. First all I want to do is read the first table take the part number and check....

Query for reordering tables - Hi folks!, I have this table: int char +-----+-------+ | X1 | X2 | +=====+=======+ | 1 | "A" | +-----+-------+ | 1 | "B" | +-----+-------+ | 2 | "C" | +-----+-------+ | 2 | "D" | +-----+--...

Run same query against multiple tables - Hi all, Is there a way to quickly run an sql query against all the tables in a database? or will I have to simply write a line for each table? I have 100+ tables where I want to delete rows from from them where ID= (a particular id). Thanks Gav
   Database Forums (Home) -> MSEQ All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]