Ask a Question related to Microsoft SQL / MS SQL Server, Design and Development.
-
Simon #1
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
-
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... -
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... -
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... -
help with SELECT statement
"Aaron" <abroadway@ameritrust.com> wrote in message news:05a601c365df$b31a8d40$a401280a@phx.gbl... "SUM(ABS(action_date>= {" & start_date2 & "}... -
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,... -
Simon #2
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
-
Ray Higdon #3
SELECT statement
How do you join books with countryprice table? Sounds
like using CASE would work.
Ray Higdon MCSE, MCDBA, CCNA
the price doesn't>-----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, ifused.>exist for that
>country, the default price from table product should befor product Book:>
>So, I need select statement, which returns the result>
>countryName price
> Italy 90
> England 110
> Germany 100
>
>Does anybody know the select statement?
>
>Thank you,
>Simon
>
>
>
>.
>Ray Higdon Guest
-
Anthony Faull #4
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
-
David Portas #5
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
-
David Portas #6
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
-
Peter Bengtsson #7
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...
price doesn't>-----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 theused.>exist for that
>country, the default price from table product should beproduct Book:>
>So, I need select statement, which returns the result for>
>countryName price
> Italy 90
> England 110
> Germany 100
>
>Does anybody know the select statement?
>
>Thank you,
>Simon
>
>
>
>.
>Peter Bengtsson Guest
-
David Portas #8
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



Reply With Quote

