Data from Same Column Joined in Two Different Ways

aliassybasesybase-sql-anywhere

In the following sybase query I would like to pull the vendor column twice: the first time I would like to pull the vendor associated with the ReceiveWorksheet table by associating it with a key RCW_VEN_FK, and I then would like to pull from the same table again but this time associating it with the StockInventoryLocal table by the key SIL_VEN_FK_Default. The goal is to pull the data into two different columns – one which will show the default vendor and one which will show the vendor associated with the receiving worksheet.

I've been looking for quite some time on how to accomplish this. The first time I received an error that the item was already created (when trying to simply pull from the Vendor table twice), I then tried with aliasing and was unable to accomplish it in that manner.

Does anyone have a suggestion or a resource on how to pull this information from the Vendor table twice and associate it with different keys in different tables and have it populate as two different columns?

ALTER VIEW "ecrs"."00_Test3_BD"( /* view_column_name, ... */ )
AS
SELECT "ecrs"."Vendor"."VEN_Code",
"ecrs"."Vendor"."VEN_CompanyName",
"ecrs"."StockInventoryLocal"."SIL_VEN_FK_Default",
"ecrs"."ReceiveWorksheet"."RCW_VEN_FK"
FROM ( "ecrs"."Vendor" JOIN "ecrs"."StockInventoryLocal" As "DefaultVendor" ON "ecrs"."Vendor"."VEN_PK" = "ecrs"."StockInventoryLocal"."SIL_VEN_FK_Default" ) JOIN "ecrs"."ReceiveWorksheet" As "WorksheetVendor"  ON "ecrs"."Vendor"."VEN_PK" = "ecrs"."ReceiveWorksheet"."RCW_VEN_FK"

I tried the above and received an error "Table 'DefaultVendor' not found."

Best Answer

Assuming ecrs is a user/schema, and keeping in mind I'm not quite following the textual description (eg, not sure which column(s) from the Vendor table you're trying to display twice), see if the following gets you started ... we can tweak based on your feedback:

SELECT v.VEN_Code,
       v.VEN_CompanyName,
       sil.SIL_VEN_FK_Default,
       rcw.RCW_VEN_FK

FROM   ecrs.Vendor v

JOIN   ecrs.StockInventoryLocal sil
ON     v.VEN_PK = sil.SIL_VEN_FK_Default

JOIN   ecrs.ReceiveWorksheet rcw
ON     v.VEN_PK = rcw.RCW_VEN_FK

I'm assuming this isn't really what you want since you mention wanting to pull different vendors, ie, v.VEN_PK = sil.SIL_VEN_FK_Default = rcw.RCW_VEN_FK (all the same vendor).

If sil.SIL_VEN_FK_Default and rcw.RCW_VEN_FK are different values, then we'll need two references to ecrs.Vendor, but then I don't understand what the relationship is between StockInventoryLocal and ReceiveWorksheet.

In other words, let's say we join Vendor(#1) with StockInventoryLocal, and we join Vendor(#2) with ReceiveWorksheet, how do we join these two sets of joins to each other (eg, is there a relationship between StockInventoryLocal and ReceiveWorksheet? is there a relationship berween the two Vendors?)