Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Please Help!! I'm going to JOIN a mental asylum!!

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-12-04, 12:15
orekin orekin is offline
Registered User
 
Join Date: Jun 2004
Posts: 6
Exclamation Please Help!! I'm going to JOIN a mental asylum!!

I've tried this query with a few DBMS, but they either freeze or tell me Ambiguous Outer Join:

SELECT "OEINVD"."ITEM" as "Item #", "OEINVD"."QTYSHIPPED" as "Quantity Shipped", "OEINVD"."EXTINVMISC" as "$ Sales", "OEINVD"."EXTICOST" as "$COGS", "OEINVH"."INVNUMBER" as "Invoice #", "OEINVH"."CUSTOMER" as "Customer", "OECRDH"."CRDNUMBER" as "Credit Note #", SUM("OECRDD"."QTYRETURN") as "Quantity Returned",SUM("OECRDD"."EXTCRDMISC") as "$ Return", SUM("OECRDD"."EXTCCOST") as "$ Return COGS"

FROM (("OEINVD" INNER JOIN "OEINVH" ON "OEINVD"."INVNUMBER" = "OEINVH"."INVNUMBER") LEFT OUTER JOIN "OECRDH" ON "OEINVH"."INVNUMBER" = "OECRDH"."INVNUMBER") INNER JOIN "OECRDD" ON "OECRDH"."CRDUNIQ" = "OECRDD"."CRDUNIQ"

WHERE "OEINVD"."QTYSHIPPED">0 AND "OEINVH"."INVDATE">20040501 AND "OEINVD.ITEM" = "OECRDD.ITEM"

GROUP BY "OEINVD"."ITEM", "OEINVD"."QTYSHIPPED", "OEINVD"."EXTINVMISC", "OEINVD"."EXTICOST","OEINVH"."INVNUMBER","OEINVH". "CUSTOMER", "OEINVH"."INVDATE", "OECRDH"."CRDNUMBER";

....

OEINVH & OEINVD represent invoices, and are linked to each other via inner join on INVNUMBER

OECRDH and OECRDD represent credit notes and are linked to each other via inner join on CRDUNIQ

What I'm trying to do is list all the invoice detail lines (OEINVD) with credit note information if applicable. My first problem is that the detail level information for credit notes is in OECRDD. The only way I can see to link from OEINVD is:

OEINVD.INVNUMER = OEINVH.INVNUMBER

OEINVH.INVNUMBER OuterLeft OECRDH.INVNUMBER

OECRDH.CRDUNIQ = OECRDD.CRDUNIQ

OEINVD.ITEM = OECRDD.ITEM

I need that last join, otherwise the recordset would have incorrect summing on the credit note side.

My second issue is this - not every invoice has a credit note, and just to make life difficult, there can be > 1 credit note against an invoice! So in other words an invoice could have 0,1 or more credit notes against it. Where there is > 1 credit note against an invoice I want to sum the credit note fields.

I've come to a dead end, how can I get this working ?
Thanks
Bill
Reply With Quote
  #2 (permalink)  
Old 06-12-04, 19:33
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,554
that looks like microsoft access

whaddya mean, "tried this query with a few DBMS"????

where do you want it to run?
__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon
Reply With Quote
  #3 (permalink)  
Old 06-13-04, 07:09
orekin orekin is offline
Registered User
 
Join Date: Jun 2004
Posts: 6
Joins

I want to run it in Access. I have actually got it working in Pervasive, but not in Access, which won't accept the 'Ambiguous Outer Joins' ? For someone that knows SQL this would probably be easy to figure out ....

Here is a simple access database with the four tables I am interested
in:

http://s2.yousendit.com/d.aspx?id=8D...CDD280DECE067B

Here is a csv file showing my desired output:

http://s2.yousendit.com/d.aspx?id=5E...83D4FD933F3F0C

Neither of the above contain macros.

Below is the query I wrote in Pervasive v8, but:
(a) It crashes Pervasive for all but the smallest databases
(b) I'm pretty much positive it is badly written !!!

Thanks In Advance
Bill
__________

SELECT

"OEINVH"."INVNUMBER" as "Invoice #",
"OEINVH"."CUSTOMER" as "Customer",
"OEINVH"."SHIPTO" as "Ship to Code",
"OEINVH"."SHPNAME" as "Ship To Name",
"OEINVH"."INVNETNOTX" as "Invoice Total",
"OEINVD"."ITEM" as "Item #",
"OEINVD"."QTYSHIPPED" as "Quantity Shipped",
"OEINVD"."EXTINVMISC" as "$ Sales",
"OEINVD"."EXTICOST" as "$COGS",
SUM("OECRDD"."QTYRETURN") as "Quantity Returned",
SUM("OECRDD"."EXTCRDMISC") as "$ Return",
SUM("OECRDD"."EXTCCOST") as "$ Return COGS"

FROM "OEINVD" INNER JOIN "OEINVH" ON "OEINVD"."INVNUMBER" =
"OEINVH"."INVNUMBER"
INNER JOIN "OECRDH" ON "OEINVH"."INVNUMBER" = "OECRDH"."INVNUMBER"
INNER JOIN "OECRDD" ON "OECRDH"."CRDUNIQ" = "OECRDD"."CRDUNIQ"

WHERE "OEINVD"."QTYSHIPPED">0 AND "OEINVD"."ITEM" = "OECRDD"."ITEM"

GROUP BY
"OEINVH"."INVNUMBER",
"OEINVH"."CUSTOMER",
"OEINVH"."SHIPTO",
"OEINVH"."SHPNAME",
"OEINVH"."INVNETNOTX",
"OEINVD"."ITEM",
"OEINVD"."QTYSHIPPED",
"OEINVD"."EXTINVMISC",
"OEINVD"."EXTICOST"

UNION

SELECT
"OEINVH"."INVNUMBER" as "Invoice #",
"OEINVH"."CUSTOMER" as "Customer",
"OEINVH"."SHIPTO" as "Ship to Code",
"OEINVH"."SHPNAME" as "Ship To Name",
"OEINVH"."INVNETNOTX" as "Invoice Total",
"OEINVD"."ITEM" as "Item #",
"OEINVD"."QTYSHIPPED" as "Quantity Shipped",
"OEINVD"."EXTINVMISC" as "$ Sales",
"OEINVD"."EXTICOST" as "$COGS",
convert(0,sql_decimal) as "Quantity Returned",
convert(0,sql_decimal) as "$ Return",
convert(0,sql_decimal) as "$ Return COGS"

FROM "OEINVD" INNER JOIN "OEINVH" ON "OEINVD"."INVNUMBER" =
"OEINVH"."INVNUMBER"

WHERE "OEINVD"."QTYSHIPPED">0 AND ("OEINVH"."INVNUMBER" NOT IN (SELECT
DISTINCT OECRDH.INVNUMBER from OECRDH));
Reply With Quote
  #4 (permalink)  
Old 06-13-04, 08:42
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,554
access requires that you parenthesize your joins
Code:
select ... from ( ( OEINVD inner join OEINVH on OEINVD.INVNUMBER = OEINVH.INVNUMBER ) inner join OECRDH on OEINVH.INVNUMBER = OECRDH.INVNUMBER ) inner join OECRDD on OECRDH.CRDUNIQ = OECRDD.CRDUNIQ
sorry, can't test this, can't read your MDB
__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On