Unix Technical Forum

Problem with SUM

This is a discussion on Problem with SUM within the SQL Server forums, part of the Microsoft SQL Server category; --> SQL newbie alert... I have a table with the fields Location, MachineName ,Hours and Date. (The actual database is ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 01:35 PM
Ray Greene
 
Posts: n/a
Default Problem with SUM

SQL newbie alert...

I have a table with the fields Location, MachineName ,Hours and Date.
(The actual database is more complex but it will do for the purposes of this
post)

I want to return Location, MachineName and the sum of the hours between two
dates. Here is an idea of what I need, although obviously this code doesn't
work.

SELECT Location, MachineName, SUM(Hours)
FROM Table
WHERE Date BETWEEN date1 AND date2

I have experimented with various nested SELECT statements but with no
success. I'm new to SQL and out of my depth already, can anyone explain how
to do this?


Ray Greene.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 01:35 PM
David Portas
 
Posts: n/a
Default Re: Problem with SUM

Don't assume it is obvious what is wrong. It helps to say exactly what
error message you got and also to post enough code to reproduce the
problem - at least a CREATE TABLE statement.

In this case you missed the GROUP BY:

SELECT location, machinename, SUM(hours)
FROM tbl
WHERE date >= '20050101'
AND date < '20060101'
GROUP BY location, machinename ;

It is usually easier to specify date ranges as >= and < unless you are
sure that the DATETIMEs will never contain times other than midnight.

--
David Portas
SQL Server MVP
--

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 01:36 PM
Ray Greene
 
Posts: n/a
Default Re: Problem with SUM

On 12 Oct 2005 03:03:24 -0700, "David Portas"
<REMOVE_BEFORE_REPLYING_dportas@acm.org> wrote:

>Don't assume it is obvious what is wrong. It helps to say exactly what
>error message you got and also to post enough code to reproduce the
>problem - at least a CREATE TABLE statement.


OK, I'll do that in future.

>In this case you missed the GROUP BY:
>
>SELECT location, machinename, SUM(hours)
> FROM tbl
> WHERE date >= '20050101'
> AND date < '20060101'
> GROUP BY location, machinename ;


Well that was simple enough compared to what I was trying to do.

I had assumed that it would take a nested query, I guess I shouldn't jump to
conclusions when I don't know what I'm doing :-)

>It is usually easier to specify date ranges as >= and < unless you are
>sure that the DATETIMEs will never contain times other than midnight.


Good point, I'll do it that way.

Thanks for the help David, it's much appreciated.

-
Ray Greene.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 11:25 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com