School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,414 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,603 people online right now. Registration is fast and FREE... Join Now!




Probaly a SImple question.

 

Probaly a SImple question.

Outdor

16 Jun, 2009 - 04:01 PM
Post #1

New D.I.C Head
*

Joined: 7 Jun, 2009
Posts: 21



Thanked: 1 times
My Contributions
I have not worked with SQL very much and am way out of my admittedly shallow depth. I need to build a query to retrieve a data set for a VB program using a Stored Procedure, it must use data from two fields: a year to date sales field called ytd_sales and a field that lists book types called types. From these fields I need to create four fields three containing calculated data each record corresponding to a book type. The first field is the book type field which will just contain each type of book. The first calculated field will be the ytd for all books of a specific type, the second is the average ytd of a book of that type, and the last is just a count of the number books in that type. I am including my code as it stands right now but it is doing nothing close to what I want. I would greatly appreciate any help or suggestions you might offer.

-Keith

CODE

ALTER PROCEDURE YTDSalesByBookType
AS
    SELECT SUM(ytd_sales), ytd_sales/COUNT(type) AS Average_Sales, type, COUNT(type) AS BooksSold
    FROM titles
    GROUP BY type, ytd_sales WITH ROLLUP
    RETURN


User is offlineProfile CardPM
+Quote Post


June7

RE: Probaly A SImple Question.

16 Jun, 2009 - 06:47 PM
Post #2

D.I.C Regular
Group Icon

Joined: 9 Dec, 2008
Posts: 476



Thanked: 37 times
My Contributions
I am confused by the field ytd_sales. This sounds like a value that should be calculated in a report, not saved in a table. You must have multiple records for each type book because you want to count them so how can each record have a ytd_sales value for the type?
User is offlineProfile CardPM
+Quote Post

Outdor

RE: Probaly A SImple Question.

16 Jun, 2009 - 09:12 PM
Post #3

New D.I.C Head
*

Joined: 7 Jun, 2009
Posts: 21



Thanked: 1 times
My Contributions
QUOTE(June7 @ 16 Jun, 2009 - 06:47 PM) *

I am confused by the field ytd_sales. This sounds like a value that should be calculated in a report, not saved in a table. You must have multiple records for each type book because you want to count them so how can each record have a ytd_sales value for the type?



There are multipule books of each type and each book has a field that contains the year to date sales for that book I need to create a ytd feild that contains the ytd total for all books in each type. Hope this helps.

-Keith
User is offlineProfile CardPM
+Quote Post

June7

RE: Probaly A SImple Question.

17 Jun, 2009 - 09:09 AM
Post #4

D.I.C Regular
Group Icon

Joined: 9 Dec, 2008
Posts: 476



Thanked: 37 times
My Contributions
So you are saving calculated results for each book and then again for each type? Double violation of Relational Database Design Principles. What is your raw data? Is there a table that stores record for each book sold? Then you should not be saving the year-to-date calculations at any level. Generate these values in a report output. Life will be much simpler. What is your DB app?

This post has been edited by June7: 17 Jun, 2009 - 09:10 AM
User is offlineProfile CardPM
+Quote Post

Outdor

RE: Probaly A SImple Question.

17 Jun, 2009 - 04:44 PM
Post #5

New D.I.C Head
*

Joined: 7 Jun, 2009
Posts: 21



Thanked: 1 times
My Contributions
QUOTE(June7 @ 17 Jun, 2009 - 09:09 AM) *

So you are saving calculated results for each book and then again for each type? Double violation of Relational Database Design Principles. What is your raw data? Is there a table that stores record for each book sold? Then you should not be saving the year-to-date calculations at any level. Generate these values in a report output. Life will be much simpler. What is your DB app?

Unfortunately I didn't design the database I am just using it for an assignment.
User is offlineProfile CardPM
+Quote Post

June7

RE: Probaly A SImple Question.

17 Jun, 2009 - 10:46 PM
Post #6

D.I.C Regular
Group Icon

Joined: 9 Dec, 2008
Posts: 476



Thanked: 37 times
My Contributions
Okay, but what is the database app?

This post has been edited by June7: 17 Jun, 2009 - 10:47 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 11:22PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month