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.
| Author |
Topic |
|
misty
Yak Posting Veteran
80 Posts |
Posted - 2009-02-13 : 04:02:03
|
| Good morning. Have a tiny problem. Have 3 tableswritercode_w,code_bookbuycode_client,code_bookbookcode_book,title,priceand 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 thisSELECT writer.code_w, book.title,COUNT (buy.code_book)FROM writerLEFT OUTER JOIN bookON book.code_book=writer.code_bookINNER JOIN buy ON writer.code_book=buy.code_bookWHERE book.price=(SELECT MAX (book.price)FROM book)GROUP BY writer.code_w, book.titlebut 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 writerLEFT OUTER JOIN bookON book.code_book=writer.code_bookINNER JOIN buy ON writer.code_book=buy.code_bookWHERE (EXISTS (SELECT distinct MAX(book.price) FROM book))GROUP BY writer.code_w, book.titlei take all the writers but for all of their books. not only for the most expensivethanks |
|
|
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 writerINNER JOIN buy ON buy.code_book = writer.code_bookINNER JOIN book ON book.code_book = buy.code_bookWHERE 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" |
 |
|
|
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 writerINNER JOIN buy ON buy.code_book = writer.code_bookINNER JOIN book ON book.code_book = buy.code_bookWHERE 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 |
 |
|
|
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 writerINNER JOIN buy ON buy.code_book = writer.code_bookINNER JOIN book ON book.code_book = buy.code_bookWHERE 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. |
 |
|
|
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" |
 |
|
|
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 |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
|
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 thishttp://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 writercode_w,code_bookw01,b01w01,b02table bookcode_book,title,priceb01,history,300b02,maths,100i want to take onlyw01,history,(count=6)if i do SELECT writer.code_w, book.title,COUNT (buy.code_book)FROM writerLEFT OUTER JOIN bookON book.code_book=writer.code_bookINNER JOIN buy ON writer.code_book=buy.code_bookWHERE (EXISTS (SELECT distinct MAX(book.price) FROM book))GROUP BY writer.code_w, book.titlei take all the writers with all the books they 've wrotew01,history,(count)w01,maths,(count)if i do what did before i take only the writers with maximum countw03,title,(count=6)w05,title,(count=6)(sorry cant express exactly what i mean. i'm trying though.) |
 |
|
|
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" |
 |
|
|
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 postquote: Good morning. Have a tiny problem. Have 3 tableswritercode_w,code_bookbuycode_client,code_bookbookcode_book,title,price
|
 |
|
|
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 writercode_w,code_bookw01,b01w01,b02table bookcode_book,title,priceb01,history,300b02,maths,100where is buy table sample data?table buyxx1, yy1, zz1xx2, yy2, zz2 E 12°55'05.63"N 56°04'39.26" |
 |
|
|
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 writercode_w,code_bookw01,b01w01,b02table bookcode_book,title,priceb01,history,300b02,maths,100where is buy table sample data?table buyxx1, yy1, zz1xx2, yy2, zz2 E 12°55'05.63"N 56°04'39.26"
sorry my mistakecode_client,code_bookcl02,b01cl03,b01cl04,b02 ... |
 |
|
|
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 dataSET NOCOUNT ONDECLARE @Writer TABLE ( code_w CHAR(3) NOT NULL, code_book CHAR(3) NOT NULL )INSERT @WriterSELECT 'w01', 'b01' UNION ALLSELECT 'w01', 'b02'DECLARE @Book TABLE ( code_book CHAR(3), title VARCHAR(7), price INT )INSERT @BookSELECT 'b01', 'history', 300 UNION ALLSELECT 'b02', 'maths', 100DECLARE @Buy TABLE ( code_client CHAR(4) NOT NULL, code_book CHAR(3) NOT NULL )INSERT @BuySELECT 'cl02', 'b01' UNION ALLSELECT 'cl03', 'b01' UNION ALLSELECT 'cl04', 'b02'-- Display initial valuesSELECT * FROM @WriterSELECT * FROM @BuySELECT * 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" |
 |
|
|
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" |
 |
|
|
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 dataSET NOCOUNT ONDECLARE @Writer TABLE ( code_w CHAR(3) NOT NULL, code_book CHAR(3) NOT NULL )INSERT @WriterSELECT 'w01', 'b01' UNION ALLSELECT 'w01', 'b02'DECLARE @Book TABLE ( code_book CHAR(3), title VARCHAR(7), price INT )INSERT @BookSELECT 'b01', 'history', 300 UNION ALLSELECT 'b02', 'maths', 100DECLARE @Buy TABLE ( code_client CHAR(4) NOT NULL, code_book CHAR(3) NOT NULL )INSERT @BuySELECT 'cl02', 'b01' UNION ALLSELECT 'cl03', 'b01' UNION ALLSELECT 'cl04', 'b02'-- Display initial valuesSELECT * FROM @WriterSELECT * FROM @BuySELECT * 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,2Microsoft SQL Server 2005 and later |
 |
|
|
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 dataSET NOCOUNT ONDECLARE @Writer TABLE ( code_w CHAR(3) NOT NULL, code_book CHAR(3) NOT NULL )INSERT @WriterSELECT 'w01', 'b01' UNION ALLSELECT 'w01', 'b02'DECLARE @Book TABLE ( code_book CHAR(3), title VARCHAR(7), price INT )INSERT @BookSELECT 'b01', 'history', 300 UNION ALLSELECT 'b02', 'maths', 100DECLARE @Buy TABLE ( code_client CHAR(4) NOT NULL, code_book CHAR(3) NOT NULL )INSERT @BuySELECT 'cl02', 'b01' UNION ALLSELECT 'cl02', 'b01' UNION ALLSELECT 'cl03', 'b01' UNION ALLSELECT 'cl04', 'b02'-- Display the resultSELECT TOP 1 WITH TIES w.code_w, b.Title, b.Price, y.Books, y.ClientsFROM @Writer AS wINNER JOIN @Book AS b ON b.code_book = w.code_bookINNER 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_bookORDER BY ROW_NUMBER() OVER (PARTITION BY w.code_w ORDER BY b.Price DESC) E 12°55'05.63"N 56°04'39.26" |
 |
|
|
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 |
 |
|
|
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 dataSET NOCOUNT ONDECLARE @Writer TABLE ( code_w CHAR(3) NOT NULL, code_book CHAR(3) NOT NULL )INSERT @WriterSELECT 'w01', 'b01' UNION ALLSELECT 'w01', 'b02'DECLARE @Book TABLE ( code_book CHAR(3), title VARCHAR(7), price INT )INSERT @BookSELECT 'b01', 'history', 300 UNION ALLSELECT 'b02', 'maths', 100DECLARE @Buy TABLE ( code_client CHAR(4) NOT NULL, code_book CHAR(3) NOT NULL )INSERT @BuySELECT 'cl02', 'b01' UNION ALLSELECT 'cl03', 'b01' UNION ALLSELECT 'cl04', 'b02'-- Display initial valuesSELECT * FROM @WriterSELECT * FROM @BuySELECT * 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,2Microsoft SQL Server 2005 and later
did you move from Oracle?http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=119415 |
 |
|
|
misty
Yak Posting Veteran
80 Posts |
Posted - 2009-02-14 : 06:17:59
|
quote: Originally posted by visakh16did 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.  |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-14 : 10:48:22
|
Oh..ok..just checked |
 |
|
|
|
|
|
|
|