If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Calling SQL Guru's (query help needed!!)

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-14-07, 17:46
mark990 mark990 is offline
Registered User
 
Join Date: May 2007
Posts: 2
Calling SQL Guru's (query help needed!!)

Hey guys, iam having major problems with this query:

"average percent increase in price of work between initial sale price and last sale price"?


if any one can find a solution it would be GREATLY appreciated.


CREATE TABLE [dbo].[Sales](
[SalesID] [int] IDENTITY(1,1) NOT NULL,
[LocationID] [smallint] NOT NULL,
[VendorID] [smallint] NOT NULL,
[WorkID] [int] NOT NULL,
[Sale_Status] [char](8)
[Previous_Sale_Price] [money] NULL,
[Sale_Price] [money] NULL,
[Commission] [float] NULL,
[Date_Up_For_Sales] [datetime] NULL,
[Date_Withdrawn_From_Sale] [datetime] NULL,
[Sale_Date] [datetime] NULL,
CONSTRAINT [pk_Sales] PRIMARY KEY CLUSTERED


CREATE TABLE [dbo].[Work](
[WorkID] [int] IDENTITY(1,1) NOT NULL,
[Work_TypeID] [tinyint] NOT NULL,
[Work_Title] [varchar](40) NOT NULL,
[Creation_Date] [datetime] NOT NULL,
CONSTRAINT [pk_Work] PRIMARY KEY CLUSTERED
Reply With Quote
  #2 (permalink)  
Old 05-14-07, 20:06
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
not enough info, even for a homework assignment

surely the assignment gives a hint about what constitutes the initial and the final sale price? how do these relate to the columns Sale_Price and Previous_Sale_Price? what is Sale_Status and how does it apply?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 05-14-07, 22:25
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,726
I always find it amusing that these students think an "SQL Guru" is required to help them cheat on their simple homework assignments.
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #4 (permalink)  
Old 05-15-07, 04:20
mark990 mark990 is offline
Registered User
 
Join Date: May 2007
Posts: 2
Try this?

items are put up for sale, these items can be sold many times, if an item is sold or put up for sale it is marked accordingly in the sale status for eg "for sale" "sold" etc...

i need to no the average increase in price for an item between its original sale date and its last sale date.

So i assume the query will go something like this: filter by status to find all the items that have been involved in a sale, "sold" items, and then find first date it was put up for sale "Min" date_up_for_sale: then find its last sale date "max" sale_date: the average Previous_Sale_Price and Sale_Price
Reply With Quote
  #5 (permalink)  
Old 05-15-07, 06:00
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Use a common table expression to find the first price for an item, a second CTE for the last price, then calculate the average over the difference of both prices. Instead of CTEs, you can use subselects. No big deal...
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #6 (permalink)  
Old 05-15-07, 06:14
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,246
if you want the average price.. then presumably you want to look at the AVERAGE of all sales, how you calculate that is up to you.. do you want a simple AVG or a weighted one.. where you give weight to number of items sold at that price
say you had 2 sales one for 10 items at 15.25, one at 14.00, one at 14.25
a simple average would be (15.25+14.00+14.25)/3= 14.50, whereas the weighted average would be (15.25*10 + 14 + 14.25)/12 =15.06

BTW have you investigated what functions are available as part of SQL?
I wonder if SQL has an AVG or even AVERAGE function that may help?
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #7 (permalink)  
Old 05-15-07, 15:49
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
SQL has an AVG() aggregate function (aka column function), of course.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On