Quantcast
Channel: User Schwern - Stack Overflow
Viewing all articles
Browse latest Browse all 581

Answer by Schwern for declare alternative value for specific lookup values PostgreSQL

$
0
0

Start by left joining with the alias table to get the missing item codes; we use a left join because we want the result to contain every row in pricing even if it has no match. Match the itemcode with the alternate.

SELECT   *FROM pricing pLEFT JOIN alternate_codes ac  ON ac.alternate_code = p.itemcode

Now we have every row of pricing matched to its alternate.

Now to choose the correct itemcode. If there's an alternate_codes.itemcode defined we should use that, else we use pricing.itemcode. Do this with coalesce to choose the first non-null column.

SELECT coalesce(ac.itemcode, p.itemcode) as itemcode, price FROM pricing pLEFT JOIN alternate_codes ac  ON ac.alternate_code = p.itemcodeWHERE coalesce(ac.itemcode, p.itemcode) in ('abcd','xxxx','bark','home','sled','tech','bell')

Viewing all articles
Browse latest Browse all 581

Trending Articles