Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 maximum price

Author  Topic 

misty
Yak Posting Veteran

80 Posts

Posted - 2009-02-13 : 04:02:03
Good morning. Have a tiny problem. Have 3 tables
writer
code_w,code_book

buy
code_client,code_book

book
code_book,title,price

and must find code_w,book.title,and sum of books that where sold but only for the most expensive book for each writer.
i did this

SELECT writer.code_w, book.title,COUNT (buy.code_book)
FROM writer
LEFT OUTER JOIN book
ON book.code_book=writer.code_book
INNER JOIN buy ON writer.code_book=buy.code_book
WHERE book.price=(SELECT MAX (book.price)FROM book)
GROUP BY writer.code_w, book.title

but instead of giving me the writer.code_w, book.title,COUNT (buy.code_book) for all the writers per most expensive book it gives me only for those who have maximum count(buy.code_book).

what's wrong with that?

if i do this

SELECT writer.code_w, book.title,COUNT (buy.code_book)
FROM writer
LEFT OUTER JOIN book
ON book.code_book=writer.code_book
INNER JOIN buy ON writer.code_book=buy.code_book
WHERE (EXISTS (SELECT distinct MAX(book.price) FROM book))
GROUP BY writer.code_w, book.title

i take all the writers but for all of their books. not only for the most expensive

thanks

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-02-13 : 04:10:41
Homework?
SELECT		writer.code_w,
book.title,
COUNT(buy.code_book)
FROM writer
INNER JOIN buy ON buy.code_book = writer.code_book
INNER JOIN book ON book.code_book = buy.code_book
WHERE book.price = (SELECT MAX(book.price) FROM book AS b WHERE b.code_book = buy.code_book)
GROUP BY writer.code_w,
book.title



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

misty
Yak Posting Veteran

80 Posts

Posted - 2009-02-13 : 04:12:19
quote:
Originally posted by Peso

Homework?
SELECT		writer.code_w,
book.title,
COUNT(buy.code_book)
FROM writer
INNER JOIN buy ON buy.code_book = writer.code_book
INNER JOIN book ON book.code_book = buy.code_book
WHERE book.price = (SELECT MAX(book.price) FROM book AS b WHERE b.code_book = buy.code_book)
GROUP BY writer.code_w,
book.title



E 12°55'05.63"
N 56°04'39.26"




no sql book practice exercises without answers
Go to Top of Page

misty
Yak Posting Veteran

80 Posts

Posted - 2009-02-13 : 04:21:26
quote:
Originally posted by Peso

Homework?
SELECT		writer.code_w,
book.title,
COUNT(buy.code_book)
FROM writer
INNER JOIN buy ON buy.code_book = writer.code_book
INNER JOIN book ON book.code_book = buy.code_book
WHERE book.price = (SELECT MAX(book.price) FROM book AS b WHERE b.code_book = buy.code_book)
GROUP BY writer.code_w,
book.title



E 12°55'05.63"
N 56°04'39.26"




still the same take the maximum counts of two writers only.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-02-13 : 04:32:40
Do you have more than two writers in buy table?



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

misty
Yak Posting Veteran

80 Posts

Posted - 2009-02-13 : 04:38:39
quote:
Originally posted by Peso

Do you have more than two writers in buy table?



E 12°55'05.63"
N 56°04'39.26"




in buy table have no writers. from buy table i count the number of books that were sold. in writer table have 11 writers
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-02-13 : 04:41:12
You wrote you get two records back, that mean you have at least two records in Buy table.
And your original question was about counting (for each writer the most expensive book) how many were sold.

I think you need to read this
http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

misty
Yak Posting Veteran

80 Posts

Posted - 2009-02-13 : 04:59:29
quote:
Originally posted by Peso

You wrote you get two records back, that mean you have at least two records in Buy table.
And your original question was about counting (for each writer the most expensive book) how many were sold.

I think you need to read this
http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx



E 12°55'05.63"
N 56°04'39.26"




my code buy has no writers. it has code_client and code_book.
i join code_book from buy with code_book from writer and thats how i take the writers.
Have 7 writers. Each of them has written 2 or more books.
I want to have for each writer the count of books he sold but only for the most expensive book he wrote.

eg
table writer
code_w,code_book
w01,b01
w01,b02

table book
code_book,title,price
b01,history,300
b02,maths,100

i want to take only
w01,history,(count=6)

if i do

SELECT writer.code_w, book.title,COUNT (buy.code_book)
FROM writer
LEFT OUTER JOIN book
ON book.code_book=writer.code_book
INNER JOIN buy ON writer.code_book=buy.code_book
WHERE (EXISTS (SELECT distinct MAX(book.price) FROM book))
GROUP BY writer.code_w, book.title

i take all the writers with all the books they 've wrote
w01,history,(count)
w01,maths,(count)

if i do what did before i take only the writers with maximum count
w03,title,(count=6)
w05,title,(count=6)

(sorry cant express exactly what i mean. i'm trying though.)
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-02-13 : 05:03:24
Where is sample data for table Buy?



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

misty
Yak Posting Veteran

80 Posts

Posted - 2009-02-13 : 05:06:05
quote:
Originally posted by Peso

Where is sample data for table Buy?



E 12°55'05.63"
N 56°04'39.26"




my first post

quote:
Good morning. Have a tiny problem. Have 3 tables
writer
code_w,code_book

buy
code_client,code_book

book
code_book,title,price
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-02-13 : 05:09:52
That is not sample data. That is table definition.

You posted sample data for writer and book tables like this:
table writer
code_w,code_book
w01,b01
w01,b02

table book
code_book,title,price
b01,history,300
b02,maths,100

where is buy table sample data?
table buy
xx1, yy1, zz1
xx2, yy2, zz2





E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

misty
Yak Posting Veteran

80 Posts

Posted - 2009-02-13 : 05:14:09
quote:
Originally posted by Peso

That is not sample data. That is table definition.

You posted sample data for writer and book tables like this:
table writer
code_w,code_book
w01,b01
w01,b02

table book
code_book,title,price
b01,history,300
b02,maths,100

where is buy table sample data?
table buy
xx1, yy1, zz1
xx2, yy2, zz2





E 12°55'05.63"
N 56°04'39.26"




sorry my mistake
code_client,code_book
cl02,b01
cl03,b01
cl04,b02 ...
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-02-13 : 05:23:46
Now we finally are getting forward!
What is your expected output based on this sample data below?
-- Prepare sample data
SET NOCOUNT ON

DECLARE @Writer TABLE
(
code_w CHAR(3) NOT NULL,
code_book CHAR(3) NOT NULL
)

INSERT @Writer
SELECT 'w01', 'b01' UNION ALL
SELECT 'w01', 'b02'

DECLARE @Book TABLE
(
code_book CHAR(3),
title VARCHAR(7),
price INT
)

INSERT @Book
SELECT 'b01', 'history', 300 UNION ALL
SELECT 'b02', 'maths', 100

DECLARE @Buy TABLE
(
code_client CHAR(4) NOT NULL,
code_book CHAR(3) NOT NULL
)

INSERT @Buy
SELECT 'cl02', 'b01' UNION ALL
SELECT 'cl03', 'b01' UNION ALL
SELECT 'cl04', 'b02'

-- Display initial values
SELECT * FROM @Writer
SELECT * FROM @Buy
SELECT * FROM @Book
Note that I followed the advice given in the link above about how to post questions for forums...


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-02-13 : 05:25:32
You have also missed to give another piece of vital information.
Are you using Microsoft SQL Server 2000 or Microsoft SQL Server 2005 and later?



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

misty
Yak Posting Veteran

80 Posts

Posted - 2009-02-13 : 05:32:34
quote:
Originally posted by Peso

Now we finally are getting forward!
What is your expected output based on this sample data below?
-- Prepare sample data
SET NOCOUNT ON

DECLARE @Writer TABLE
(
code_w CHAR(3) NOT NULL,
code_book CHAR(3) NOT NULL
)

INSERT @Writer
SELECT 'w01', 'b01' UNION ALL
SELECT 'w01', 'b02'

DECLARE @Book TABLE
(
code_book CHAR(3),
title VARCHAR(7),
price INT
)

INSERT @Book
SELECT 'b01', 'history', 300 UNION ALL
SELECT 'b02', 'maths', 100

DECLARE @Buy TABLE
(
code_client CHAR(4) NOT NULL,
code_book CHAR(3) NOT NULL
)

INSERT @Buy
SELECT 'cl02', 'b01' UNION ALL
SELECT 'cl03', 'b01' UNION ALL
SELECT 'cl04', 'b02'

-- Display initial values
SELECT * FROM @Writer
SELECT * FROM @Buy
SELECT * FROM @Book
Note that I followed the advice given in the link above about how to post questions for forums...


E 12°55'05.63"
N 56°04'39.26"





dont know i should put all this. sorry. i expect
w01,history,2

Microsoft SQL Server 2005 and later
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-02-13 : 05:43:26
The link desribes how you should put this together.
A reason this took some time is that the tables are not normalized.
What's the name of the self-study material are you using?
-- Prepare sample data
SET NOCOUNT ON

DECLARE @Writer TABLE
(
code_w CHAR(3) NOT NULL,
code_book CHAR(3) NOT NULL
)

INSERT @Writer
SELECT 'w01', 'b01' UNION ALL
SELECT 'w01', 'b02'

DECLARE @Book TABLE
(
code_book CHAR(3),
title VARCHAR(7),
price INT
)

INSERT @Book
SELECT 'b01', 'history', 300 UNION ALL
SELECT 'b02', 'maths', 100

DECLARE @Buy TABLE
(
code_client CHAR(4) NOT NULL,
code_book CHAR(3) NOT NULL
)

INSERT @Buy
SELECT 'cl02', 'b01' UNION ALL
SELECT 'cl02', 'b01' UNION ALL
SELECT 'cl03', 'b01' UNION ALL
SELECT 'cl04', 'b02'

-- Display the result
SELECT TOP 1 WITH TIES
w.code_w,
b.Title,
b.Price,
y.Books,
y.Clients
FROM @Writer AS w
INNER JOIN @Book AS b ON b.code_book = w.code_book
INNER JOIN (
SELECT code_book,
COUNT(*) AS Books,
COUNT(DISTINCT code_client) AS Clients
FROM @Buy
GROUP BY code_book
) AS y ON y.code_book = b.code_book
ORDER BY ROW_NUMBER() OVER (PARTITION BY w.code_w ORDER BY b.Price DESC)



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

misty
Yak Posting Veteran

80 Posts

Posted - 2009-02-13 : 05:57:58
[quote]Originally posted by Peso

The link desribes how you should put this together.
A reason this took some time is that the tables are not normalized.
What's the name of the self-study material are you using?[code]--

database system,practice (dont know the exact words in english) but its from Aristoteles Universtity
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-13 : 09:21:37
quote:
Originally posted by misty

quote:
Originally posted by Peso

Now we finally are getting forward!
What is your expected output based on this sample data below?
-- Prepare sample data
SET NOCOUNT ON

DECLARE @Writer TABLE
(
code_w CHAR(3) NOT NULL,
code_book CHAR(3) NOT NULL
)

INSERT @Writer
SELECT 'w01', 'b01' UNION ALL
SELECT 'w01', 'b02'

DECLARE @Book TABLE
(
code_book CHAR(3),
title VARCHAR(7),
price INT
)

INSERT @Book
SELECT 'b01', 'history', 300 UNION ALL
SELECT 'b02', 'maths', 100

DECLARE @Buy TABLE
(
code_client CHAR(4) NOT NULL,
code_book CHAR(3) NOT NULL
)

INSERT @Buy
SELECT 'cl02', 'b01' UNION ALL
SELECT 'cl03', 'b01' UNION ALL
SELECT 'cl04', 'b02'

-- Display initial values
SELECT * FROM @Writer
SELECT * FROM @Buy
SELECT * FROM @Book
Note that I followed the advice given in the link above about how to post questions for forums...


E 12°55'05.63"
N 56°04'39.26"





dont know i should put all this. sorry. i expect
w01,history,2

Microsoft SQL Server 2005 and later


did you move from Oracle?

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=119415
Go to Top of Page

misty
Yak Posting Veteran

80 Posts

Posted - 2009-02-14 : 06:17:59
quote:
Originally posted by visakh16

did you move from Oracle?

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=119415




i cant move from oracle. univercity exercises are on oracle. but i'm running them also in sql. trying to understand everything at once.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-14 : 10:48:22
Oh..ok..just checked
Go to Top of Page
   

- Advertisement -