Ask a Question related to Microsoft SQL / MS SQL Server, Design and Development.

  1. #1

    Default SELECT statement

    I have 3 tables:

    table countryPrice:

    productID countryId price
    1 Italy 90
    1 England 110

    Table country:
    countryId countryName

    1 Italy
    2 England
    3 Germany

    Table product:

    productId productName productPrice
    1 Book 100
    2 Computer 1000
    . ...... ...

    Now, I have for example product Book.
    I have to get the price for Book for each country, if the price doesn't
    exist for that
    country, the default price from table product should be used.

    So, I need select statement, which returns the result for product Book:

    countryName price
    Italy 90
    England 110
    Germany 100

    Does anybody know the select statement?

    Thank you,
    Simon


    Simon Guest

  2. Similar Questions and Discussions

    1. Select-And-Update in one statement?
      On my site I often do SELECT counter from counters where id = 10 UPDATE counter set counter = counter + 1 Can I somehow merge them into one...
    2. select statement woes
      I hope some one can help me figure this out. I've got a search feature for images and users can enter up to 3 terms and I'm not getting the results...
    3. SP with Select statement
      Hi, I'm trying to select fileds that are in the results of a SP. So I have the table "tblItem" itemID int Identity Key, itemName varchar...
    4. help with SELECT statement
      "Aaron" <abroadway@ameritrust.com> wrote in message news:05a601c365df$b31a8d40$a401280a@phx.gbl... "SUM(ABS(action_date>= {" & start_date2 & "}...
    5. Select Statement Question (Again)
      I have the following simple Select statement which produces an error when I attempt to save it as part of a stored proc. Select InvoiceID,...
  3. #2

    Default SELECT statement

    I have 3 tables:

    table countryPrice:

    productID countryId price
    1 Italy 90
    1 England 110

    Table country:
    countryId countryName

    1 Italy
    2 England
    3 Germany

    Table product:

    productId productName productPrice
    1 Book 100
    2 Computer 1000
    . ...... ...

    Now, I have for example product Book.
    I have to get the price for Book for each country, if the price doesn't
    exist for that
    country, the default price from table product should be used.

    So, I need select statement, which returns the result for product Book:

    countryName price
    Italy 90
    England 110
    Germany 100

    Does anybody know the select statement?

    Thank you,
    Simon



    Simon Guest

  4. #3

    Default SELECT statement

    How do you join books with countryprice table? Sounds
    like using CASE would work.

    Ray Higdon MCSE, MCDBA, CCNA
    >-----Original Message-----
    >I have 3 tables:
    >
    >table countryPrice:
    >
    >productID countryId price
    > 1 Italy 90
    > 1 England 110
    >
    >Table country:
    >countryId countryName
    >
    > 1 Italy
    > 2 England
    > 3 Germany
    >
    >Table product:
    >
    >productId productName productPrice
    > 1 Book 100
    > 2 Computer 1000
    > . ...... ...
    >
    >Now, I have for example product Book.
    >I have to get the price for Book for each country, if
    the price doesn't
    >exist for that
    >country, the default price from table product should be
    used.
    >
    >So, I need select statement, which returns the result
    for product Book:
    >
    >countryName price
    > Italy 90
    > England 110
    > Germany 100
    >
    >Does anybody know the select statement?
    >
    >Thank you,
    >Simon
    >
    >
    >
    >.
    >
    Ray Higdon Guest

  5. #4

    Default Re: SELECT statement

    SELECT country.countryName, countryPrice.price
    FROM product
    INNER JOIN countryPrice on countryPrice.productID = product.productID
    INNER JOIN country on country.countryID = countryPrice.countryID
    WHERE product.productName = 'Book'

    "Simon" <simon.zupan@stud-moderna.si> wrote in message
    news:3f02a59b$1@news.s5.net...
    > I have 3 tables:
    >
    > table countryPrice:
    >
    > productID countryId price
    > 1 Italy 90
    > 1 England 110
    >
    > Table country:
    > countryId countryName
    >
    > 1 Italy
    > 2 England
    > 3 Germany
    >
    > Table product:
    >
    > productId productName productPrice
    > 1 Book 100
    > 2 Computer 1000
    > . ...... ...
    >
    > Now, I have for example product Book.
    > I have to get the price for Book for each country, if the price doesn't
    > exist for that
    > country, the default price from table product should be used.
    >
    > So, I need select statement, which returns the result for product Book:
    >
    > countryName price
    > Italy 90
    > England 110
    > Germany 100
    >
    > Does anybody know the select statement?
    >
    > Thank you,
    > Simon
    >
    >
    >

    Anthony Faull Guest

  6. #5

    Default Re: SELECT statement


    CREATE TABLE Countries (countryid INTEGER PRIMARY KEY, countryname
    VARCHAR(20) NOT NULL UNIQUE)
    INSERT INTO Countries VALUES (1, 'Italy')
    INSERT INTO Countries VALUES (2, 'England')
    INSERT INTO Countries VALUES (3, 'Germany')

    CREATE TABLE Products (productid INTEGER PRIMARY KEY, productname
    VARCHAR(20) NOT NULL UNIQUE, productprice NUMERIC(10,2) NOT NULL CHECK
    (productprice>0))
    INSERT INTO Products VALUES (1, 'Book', 100)
    INSERT INTO Products VALUES (2, 'Computer', 1000)

    CREATE TABLE CountryPrice (productid INTEGER NOT NULL REFERENCES Products
    (productid), countryid INTEGER NOT NULL REFERENCES Countries (countryid),
    price NUMERIC(10,2) NOT NULL CHECK (price>0), PRIMARY
    KEY(productid,countryid))
    INSERT INTO CountryPrice VALUES (1,1,90)
    INSERT INTO CountryPrice VALUES (1,2,110)

    SELECT P.productid, P.productname, C.countryname,
    COALESCE(R.price,P.productprice) AS price
    FROM Products AS P
    CROSS JOIN Countries AS C
    LEFT JOIN CountryPrice AS R
    ON P.productid = R.productid AND C.countryid = R.countryid
    WHERE P.productid = 1

    --
    David Portas
    ------------
    Please reply only to the newsgroup
    --



    David Portas Guest

  7. #6

    Default Re: SELECT statement

    Doesn't help where the price is missing for the country though.

    --
    David Portas
    ------------
    Please reply only to the newsgroup
    --



    David Portas Guest

  8. #7

    Default SELECT statement

    Hi
    Try this one:
    Select c.CountryName, isnull(cp.Price, p.ProdPrice) from
    Country c
    LEFT JOIN CountryPrice cp
    on cp.CountryID = c.CountryID and cp.ProductID = 1
    , Product p where p.ProductID = 1

    Drwback: You have to use ProdID twice...
    >-----Original Message-----
    >I have 3 tables:
    >
    >table countryPrice:
    >
    >productID countryId price
    > 1 Italy 90
    > 1 England 110
    >
    >Table country:
    >countryId countryName
    >
    > 1 Italy
    > 2 England
    > 3 Germany
    >
    >Table product:
    >
    >productId productName productPrice
    > 1 Book 100
    > 2 Computer 1000
    > . ...... ...
    >
    >Now, I have for example product Book.
    >I have to get the price for Book for each country, if the
    price doesn't
    >exist for that
    >country, the default price from table product should be
    used.
    >
    >So, I need select statement, which returns the result for
    product Book:
    >
    >countryName price
    > Italy 90
    > England 110
    > Germany 100
    >
    >Does anybody know the select statement?
    >
    >Thank you,
    >Simon
    >
    >
    >
    >.
    >
    Peter Bengtsson Guest

  9. #8

    Default Re: SELECT statement

    This works but I suggest you avoid mixing SQL89 and SQL92 JOIN syntax in the
    same query. Use CROSS JOIN instead so that the join criteria is unambiguous.

    SELECT c.CountryName, ISNULL(cp.Price, p.ProdPrice)
    FROM Country c
    LEFT JOIN CountryPrice cp
    ON cp.CountryID = c.CountryID AND cp.ProductID = 1
    CROSS JOIN Product p
    WHERE p.ProductID = 1

    --
    David Portas
    ------------
    Please reply only to the newsgroup
    --



    David Portas Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139