Solved

SQL SUM - producing wrong results

Posted on 2014-03-05
4
879 Views
Last Modified: 2014-03-05
Hi

Please look at the queries here
sum
You will see the first results which show the correct results

What I'm trying to do is group the results by days, and give an overall sum for each column
however, i'm getting all -1 results and then werid results for [23-00] column

i'll actually be selecting lots of machines (removing the MachineId=160 bit)
and selecting over a 14 day period, but removed for simplicity

what am i doing wrong
0
Comment
Question by:websss
[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
  • 2
  • 2
4 Comments
 
LVL 11

Expert Comment

by:John_Vidmar
ID: 39906188
Your column-names have special characters and do not start with a letter, therefore, you must enclose them in an object-delimiter (like brackets, or double-quotes).

This:  sum(06-07) as [06-07]

should be:  sum([06-07]) as [06-07]
0
 

Author Comment

by:websss
ID: 39906255
I now get

Msg 8117, Level 16, State 1, Line 4
Operand data type bit is invalid for sum operator.
0
 
LVL 11

Accepted Solution

by:
John_Vidmar earned 500 total points
ID: 39906271
If aggregate-functions do not work on bit-operands then cast them to integer:

sum( cast([06-07] as integer) ) as [06-07]
0
 

Author Comment

by:websss
ID: 39906277
actually

this worked
Select  BranchId,datename(dw,ReportedDate)ReportedDate,
sum(CONVERT(INT,[06-07])) as [06-07], sum(CONVERT(INT,[07-08])) as [07-08], sum(CONVERT(INT,[08-09])) as [08-09],sum(CONVERT(INT,[09-10])) as [09-10],sum(CONVERT(INT,[10-11])) as [10-11],sum(CONVERT(INT,[11-12])) as [11-12],sum(CONVERT(INT,[12-13])) as [12-13],
sum(CONVERT(INT,[13-14])) as [13-14],sum(CONVERT(INT,[14-15])) as [14-15],sum(CONVERT(INT,[15-16])) as [15-16],sum(CONVERT(INT,[16-17])) as [16-17],sum(CONVERT(INT,[17-18])) as [17-18],sum(CONVERT(INT,[18-19])) as [19-20],sum(CONVERT(INT,[20-21])) as [20-21],
sum(CONVERT(INT,[21-22])) as [21-22],sum(CONVERT(INT,[22-23])) as [22-23],sum(CONVERT(INT,[23-00])) as [23-00],sum(CONVERT(INT,[00-01])) as [00-01],sum(CONVERT(INT,[01-02])) as [01-02],sum(CONVERT(INT,[02-03])) as [02-03],sum(CONVERT(INT,[02-03])) as [02-03],
sum(CONVERT(INT,[03-04])) as [03-04],sum(CONVERT(INT,[04-05])) as [04-05],sum(CONVERT(INT,[05-06])) as [05-06], MachineType 
From [UsageLogs]
WHERE BranchId = 51 AND MachineType = 1  and MachineType = 1 and MachineId = 160
group by BranchId, MachineType ,datename(dw,ReportedDate)

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

739 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