Challenge: complex sql instruction?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • huffie
    New Member
    • Nov 2007
    • 2

    #1

    Challenge: complex sql instruction?

    I'll explain my problem using a little example

    table
    article name price
    -------------------------------
    art1 Default 10
    art2 Default 15
    art2 Egmond 12
    art3 Default 20
    art3 Windels 17

    Goal:
    Article prices are saved with customer name Default. When one customer has a specific price, the customer's name is used (ex. customer Egmond can buy article art2 for 12 €; other customers have to pay 15 €).
    If a customer has no specific price for an article, the default price must be shown.

    Using one query, I should be able to get the catalog for customer Egmond:
    art1 10 €
    art2 12 €
    art3 20 €

    Can somebody help me?
  • amitpatel66
    Recognized Expert Top Contributor
    • Mar 2007
    • 2358

    #2
    Originally posted by huffie
    I'll explain my problem using a little example

    table
    article name price
    -------------------------------
    art1 Default 10
    art2 Default 15
    art2 Egmond 12
    art3 Default 20
    art3 Windels 17

    Goal:
    Article prices are saved with customer name Default. When one customer has a specific price, the customer's name is used (ex. customer Egmond can buy article art2 for 12 €; other customers have to pay 15 €).
    If a customer has no specific price for an article, the default price must be shown.

    Using one query, I should be able to get the catalog for customer Egmond:
    art1 10 €
    art2 12 €
    art3 20 €

    Can somebody help me?
    Try this:

    [code=sql]

    SELECT * from table1 WHERE name = 'Default' AND article NOT IN(SELECT aricle from table1 WHERE name = 'Egmond')
    UNION
    SELECT * from table1 WHERE name = 'Egmond'


    [/code]

    Comment

    Working...