Solved

sql 2005 trend query.

Posted on 2008-06-20
8
280 Views
Last Modified: 2010-04-21
i have a data.
sales
25,000
60,000
12,000

i want to write a query to show the trend for those sales. or a function so that it automatically can trend. so that i can use that function for everything.

i want like (sales*noofdaysinamonth)/presentday

so that i can show the trend for sales.
0
Comment
Question by:romeiovasu
  • 4
  • 4
8 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 21833892
can you give an example of what you'd want the output to look like?
0
 

Author Comment

by:romeiovasu
ID: 21834414
sales      Trend
$25,000.00       $37,500.00
$60,000.00       $90,000.00
$12,000.00       $18,000.00

here is the sample.
it actually done like this. trend = (sales*noofdaysinamonth)/dayofthemonth.
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21834463
Great...this should do it:


select (Sales * day(dateadd(d, -1, cast(cast(month(dateadd(m, 1, getdate())) as varchar(2)) + '/1/' + cast(year(getdate()) as varchar(4)) as datetime)))) / day(getdate())
FRom YOurTableName
0
Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

 

Author Comment

by:romeiovasu
ID: 21834497
if i want to create the samething as a funtion how can i do that.
0
 
LVL 60

Accepted Solution

by:
chapmandew earned 500 total points
ID: 21834547
this should do it:

CREATE FUNCTION udf_SalesTrend ( @SalesValue MONEY )
RETURNS MONEY
AS BEGIN
    DECLARE @RetVal MONEY
    SELECT  @RetVal = ( @SalesValue * DAY(DATEADD(d, -1,
                                                  CAST(CAST(MONTH(DATEADD(m, 1, GETDATE())) AS VARCHAR(2))
                                                  + '/1/'
                                                  + CAST(YEAR(GETDATE()) AS VARCHAR(4)) AS DATETIME))) )
            / DAY(GETDATE())
    RETURN ( @RetVal )
   END
GO

THen, you can just call it like this:

select Sales, Trend = dbo.udf_SalesTrend(Sales)
FROM YourTableName
0
 

Author Closing Comment

by:romeiovasu
ID: 31469290
thank you
0
 

Author Comment

by:romeiovasu
ID: 21834676
thank you
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21834689
very welcome.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Format Data Field - SQL 11 40
Filtering characters in an SQL field 2 19
Complex Query - help please 5 60
Applying Roles in Common Scenarios 3 13
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question