Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Calculations in stored procedures

Author  Topic 

Jabberwocky
Starting Member

4 Posts

Posted - 2006-07-03 : 11:52:09
I would welcome ideas and some code to help me with this one !

I am writing a c# application which incorporates some dynamic barcharts, including one to indicate server perfomance, where the server data is extracted from a database.
In the application, I will iterate over an array of 30 'ServerID' integers, and want to pass them one by one as parameters to an SQL stored procedure.

I need the procedure to:
declare variables for serverid, currentmonth, elapsedminutes, serverdowntime, and serveruptime.

identify the current month and assign the value to an integer variable (currentmonth).

calculate the number of minutes elapsed from the start of the month until the current date/time, and assign the value to a variable (elapsedminutes)

Query a 'ServerDown' table to return a 'DownDuration' value (recorded in minutes) where the server id = the server id passed from the application, and the 'DateDown' field matches the 'current month' variable, and assign the value to the 'serverdown' variable.

Subtract the 'DownDuration' minutes from the 'elapsedminutes' variable and calculate the remainder as a percentage of the 'elapsedminutes' variable, then assign the value to the 'serveruptime' variable, which will need to be type 'float'.

Return the serveruptime variable value to the Application.

The application will then take the returned floating point decimal and build a bar to graphically illustrate the current uptime of the given server during the elapsed current month period.

I have been doing the calculation in the Application, but would prefer to do it in the stored procedure as this will increase program efficiency, but stored procedures are not my strong point !!

Thanks in advance, and sorry its a bit of a brain teaser !!


nr
SQLTeam MVY

12543 Posts

Posted - 2006-07-03 : 11:57:03
Why do you want to hold all these values in variables?


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

Jabberwocky
Starting Member

4 Posts

Posted - 2006-07-03 : 11:59:50
So I can do the calculations, but if there is a better way, I am happy to listen
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-07-03 : 12:02:22
Are you familiar with SQL at all ? SELECT statements, JOINS, expressions, WHERE clauses, aggregate functions, etc? That would be a great place to start. It sounds like all you need is a simple query.
Go to Top of Page

Jabberwocky
Starting Member

4 Posts

Posted - 2006-07-03 : 12:05:55
My big problem is syntax. I know exactly what I need to do but lack the precise syntax. I've had a go but get laods of syntax errors. I would be happy if anyone knows a really good syntax reference they could pass on, then I can work it out myself. I know basic select, insert, where, but there it ends.
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2006-07-03 : 12:12:15
current date = getdate()
start of month = convert(varchar(6),getdate(),112) + '01'
minutes from start of month
datediff(mi,convert(varchar(6),getdate(),112) + '01', getdate())

downtime
select DownDuration from ServerDown where serverID = @ServerID and mth = convert(varchar(6),getdate(),112) + '01'


so the query becomes

select uppercentage = (a.elapsed - convert(float,b.downduration)) / a.elapsed * 100
from
(select elapsed = datediff(mi,convert(varchar(6),getdate(),112) + '01', getdate())) a ,
(select DownDuration from ServerDown where serverID = @ServerID and DateDown = convert(varchar(6),getdate(),112) + '01') b

But you could get all the values in a single query


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2006-07-03 : 12:17:20
Something like this should give you a resultset of all the percentages which you can deal with in the app rather than calling for each one

select severID, uppercentage = (e.elapsed - convert(float,d.downduration)) / e.elapsed * 100
from ServerDown d ,
(select elapsed = datediff(mi,convert(varchar(6),getdate(),112) + '01', getdate())) e
where DateDown = convert(varchar(6),getdate(),112) + '01') b
order by serverid


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

Jabberwocky
Starting Member

4 Posts

Posted - 2006-07-04 : 04:11:10
Thanks guys. Thats an enormous help. I will give it a whirl.
Re my question on an sql syntax reference source, any ideas ??
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2006-07-04 : 04:44:44
Books on line, practise, searching this site and asking questions should work.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2006-07-04 : 04:45:15
quote:
Originally posted by Jabberwocky

Thanks guys. Thats an enormous help. I will give it a whirl.
Re my question on an sql syntax reference source, any ideas ??



Best syntax reference, ofcourse, would be Books Online (SQL Server help) ! Other than that I think Rob Viera's Programming Microsoft SQL Server 2000 (Wrox) is the best book of all time.

Harsh Athalye
India.
"Nothing is Impossible"
Go to Top of Page

em_arivu
Starting Member

1 Post

Posted - 2006-07-22 : 23:02:54
i am familiar with sql. ok but i need to study normalising in sql.
Go to Top of Page

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-07-23 : 04:06:23
Check out this link, there are lots of information on the Normalization

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=67957&SearchTerms=Normalization

Chirag
Go to Top of Page
   

- Advertisement -