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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Sum function is changing the sum of 2 numbers

Author  Topic 

Norader
Starting Member

1 Post

Posted - 2014-01-30 : 16:26:59
The sum in number (nnr) 1 and 2 is changing, i don't know why just in these numbers, the other numbers the sum is good.

select distinct sum(mitschrifteinzel.points) as SUM, mitschrifteinzel.vernr,
mitschrifteinzel.nnr,
veranstaltung.verdatum
from nennungeneinzel, veranstaltung, mitschrifteinzel
where nennungeneinzel.vernr= veranstaltung.vernr
and mitschrifteinzel.nnr=nennungeneinzel.nnr
and mitschrifteinzel.vernr= veranstaltung.vernr
group by mitschrifteinzel.nnr ;

SUM vernr nnr
138 1 1 <------ here the SUM should be 72
95 1 2 <------ here the SUM Should be 52
27 1 3
24 1 4
51 1 5
39 1 6
48 1 7
27 1 8
21 1 9
51 1 10
49 1 11
66 1 12
21 1 13
70 1 14
37 1 15
66 1 16
49 1 17
50 1 18

PLEASE HELP ME

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2014-01-30 : 16:45:41
Check your JOIN conditions.
Also, I don't believe your query is a valid one. You are groping by nnr only, but selecting vernr as well.


Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-01-31 : 07:33:20
quote:
Originally posted by Norader

The sum in number (nnr) 1 and 2 is changing, i don't know why just in these numbers, the other numbers the sum is good.

select distinct sum(mitschrifteinzel.points) as SUM, mitschrifteinzel.vernr,
mitschrifteinzel.nnr,
veranstaltung.verdatum
from nennungeneinzel, veranstaltung, mitschrifteinzel
where nennungeneinzel.vernr= veranstaltung.vernr
and mitschrifteinzel.nnr=nennungeneinzel.nnr
and mitschrifteinzel.vernr= veranstaltung.vernr
group by mitschrifteinzel.nnr ;

SUM vernr nnr
138 1 1 <------ here the SUM should be 72
95 1 2 <------ here the SUM Should be 52
27 1 3
24 1 4
51 1 5
39 1 6
48 1 7
27 1 8
21 1 9
51 1 10
49 1 11
66 1 12
21 1 13
70 1 14
37 1 15
66 1 16
49 1 17
50 1 18

PLEASE HELP ME


posted query wont even work in sql server as you're missing fields in GROUP BY. In SQL Server if you're using GROUP BY you can directly include columns in select unless they're part of GROUP BY
Also DISTINCT Is redundant here as GROUP will already make sure you get only distinct values for the involed columns

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -