Solved

sql 2005 trend query.

Posted on 2008-06-20
8
283 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

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

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

628 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