Showing posts with label initial. Show all posts
Showing posts with label initial. Show all posts

Sunday, March 25, 2012

Calling SQL Gurus (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 CLUSTEREDnot 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?|||I always find it amusing that these students think an "SQL Guru" is required to help them cheat on their simple homework assignments.|||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|||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...|||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?|||SQL has an AVG() aggregate function (aka column function), of course.

Tuesday, February 14, 2012

Calculating initial size tempdb

Although I may not be sure I know how to set the initial size of the tempdb
(this is on a production server), I am aware from the reading I have done,
that it may be possible to increase performance if the initial size of the
log is adequate to prevent the tempdb from performing a great deal of
autogrowth [viewable in the dbcc loginfo('mydb') command]. Is there a
formula to calculate approximately what this initial size should be set at?
Do I risk hurting performance if this is done incorrectly? Currently there
is plenty of disk space available but we are considering log shipping which
will substantially reduce that space. The log file for the very large
database tends to hover around 10 gig which seems fairly large to me, but
considering there is a history filegroup that hovers around 55 gig and a
primary filegroup around 35 gig, it may be correct.
I am looking for some general guidance on how to set the tempdb initial
size for greater performance and perhaps how to fix log files that are
disporportionately large resulting from constant bulk inserts. Two sites I
looked at
http://www.karaszi.com/SQLServer/info_dont_shrink.asp
and
http://sqlserver2000.databases.aspfaq.com/why-is-tempdb-full-and-how-can-i-prevent-this-from-happening.html
have pushed me to inquire in this fashion.
Regards,
Jamie
Correction to the above. The test database indicates that the initial size
should be about 400 MB if it is to be larger than the one currently there.
I have set the initial size to 400 MB. Is there a way to test this size?
Regards,
Jamie
"thejamie" wrote:

> Although I may not be sure I know how to set the initial size of the tempdb
> (this is on a production server), I am aware from the reading I have done,
> that it may be possible to increase performance if the initial size of the
> log is adequate to prevent the tempdb from performing a great deal of
> autogrowth [viewable in the dbcc loginfo('mydb') command]. Is there a
> formula to calculate approximately what this initial size should be set at?
> Do I risk hurting performance if this is done incorrectly? Currently there
> is plenty of disk space available but we are considering log shipping which
> will substantially reduce that space. The log file for the very large
> database tends to hover around 10 gig which seems fairly large to me, but
> considering there is a history filegroup that hovers around 55 gig and a
> primary filegroup around 35 gig, it may be correct.
> I am looking for some general guidance on how to set the tempdb initial
> size for greater performance and perhaps how to fix log files that are
> disporportionately large resulting from constant bulk inserts. Two sites I
> looked at
> http://www.karaszi.com/SQLServer/info_dont_shrink.asp
> and
>
> http://sqlserver2000.databases.aspfaq.com/why-is-tempdb-full-and-how-can-i-prevent-this-from-happening.html
> have pushed me to inquire in this fashion.
> --
> Regards,
> Jamie
|||Hi Jamie
I am not sure there is a formula that tells you a figure that you can set
tempdb to to get the size. There are separate ones for things like how much
space would be used by rebuilding indexes
http://msdn2.microsoft.com/en-us/library/ms191183.aspx. In general I would
look at setting a reasonable size and then monitoring when and by how much it
grows. Then setting the initial size to a value that will cover normal usage.
John
"thejamie" wrote:
[vbcol=seagreen]
> Correction to the above. The test database indicates that the initial size
> should be about 400 MB if it is to be larger than the one currently there.
> I have set the initial size to 400 MB. Is there a way to test this size?
> --
> Regards,
> Jamie
>
> "thejamie" wrote:

Calculating initial size tempdb

Although I may not be sure I know how to set the initial size of the tempdb
(this is on a production server), I am aware from the reading I have done,
that it may be possible to increase performance if the initial size of the
log is adequate to prevent the tempdb from performing a great deal of
autogrowth [viewable in the dbcc loginfo('mydb') command]. Is there a
formula to calculate approximately what this initial size should be set at?
Do I risk hurting performance if this is done incorrectly? Currently there
is plenty of disk space available but we are considering log shipping which
will substantially reduce that space. The log file for the very large
database tends to hover around 10 gig which seems fairly large to me, but
considering there is a history filegroup that hovers around 55 gig and a
primary filegroup around 35 gig, it may be correct.
I am looking for some general guidance on how to set the tempdb initial
size for greater performance and perhaps how to fix log files that are
disporportionately large resulting from constant bulk inserts. Two sites I
looked at
http://www.karaszi.com/SQLServer/info_dont_shrink.asp
and
http://sqlserver2000.databases.aspf...-happening.html
have pushed me to inquire in this fashion.
Regards,
JamieCorrection to the above. The test database indicates that the initial size
should be about 400 MB if it is to be larger than the one currently there.
I have set the initial size to 400 MB. Is there a way to test this size?
--
Regards,
Jamie
"thejamie" wrote:

> Although I may not be sure I know how to set the initial size of the tempd
b
> (this is on a production server), I am aware from the reading I have done,
> that it may be possible to increase performance if the initial size of the
> log is adequate to prevent the tempdb from performing a great deal of
> autogrowth [viewable in the dbcc loginfo('mydb') command]. Is there
a
> formula to calculate approximately what this initial size should be set at
?
> Do I risk hurting performance if this is done incorrectly? Currently ther
e
> is plenty of disk space available but we are considering log shipping whic
h
> will substantially reduce that space. The log file for the very large
> database tends to hover around 10 gig which seems fairly large to me, but
> considering there is a history filegroup that hovers around 55 gig and a
> primary filegroup around 35 gig, it may be correct.
> I am looking for some general guidance on how to set the tempdb initial
> size for greater performance and perhaps how to fix log files that are
> disporportionately large resulting from constant bulk inserts. Two sites
I
> looked at
> http://www.karaszi.com/SQLServer/info_dont_shrink.asp
> and
>
> http://sqlserver2000.databases.aspf...-happening.html
> have pushed me to inquire in this fashion.
> --
> Regards,
> Jamie|||Hi Jamie
I am not sure there is a formula that tells you a figure that you can set
tempdb to to get the size. There are separate ones for things like how much
space would be used by rebuilding indexes
http://msdn2.microsoft.com/en-us/library/ms191183.aspx. In general I would
look at setting a reasonable size and then monitoring when and by how much i
t
grows. Then setting the initial size to a value that will cover normal usage
.
John
"thejamie" wrote:
[vbcol=seagreen]
> Correction to the above. The test database indicates that the initial siz
e
> should be about 400 MB if it is to be larger than the one currently there.
> I have set the initial size to 400 MB. Is there a way to test this size?
> --
> Regards,
> Jamie
>
> "thejamie" wrote:
>