Solved

Sql server pivot table summing wrong.

Posted on 2013-05-22
9
245 Views
Last Modified: 2013-05-24
good afternoon, I am having some trouble creating a pivot table.

 
USE [nominals]
GO
set dateformat ymd
select * from
(
 select distinct account_title, (tran_amount + vat_amount) as total,project_code from nominalsview
  where managerid = 1052 and tran_date between '2013-04-01' and '2014-03-31'
 ) x
 pivot
 (
 sum(total)
 for project_code in (CORP1, EMP01, FAC01, FAM01, FIN01, FIR01, MAR01, MAT01, MUN01, 
 PLA01, PRIV1, PRO01, PRO02, REC01, REP01, RES01, ADMIN, CON01, COR01, HR001, IT001,
  LIT01, MIS01, PI001, PRF01, PRL01, PROP1, SSD01, TAX01, TRA01, TRU01)
 ) p;

Open in new window


the table appears to be fine but the summed totals are not correct, its as if some rows aren't being added to the equation. am I missing something really obvious here?
0
Comment
Question by:forsters
9 Comments
 
LVL 40

Expert Comment

by:Sharath
ID: 39187999
Can you post sample data from your table
0
 

Author Comment

by:forsters
ID: 39188184
not really its sensitive info. the columns in the sum function are decimals if that helps. and its obvious that its a case of some rows just not being included in the sum but these rows don't stand out for any reason.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39188484
not really its sensitive info.
Nobody is asking you to post the real data.  The keyword here is "sample" data.
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
ID: 39188721
First, we need to correct the date comparison, as below; that alone might cause the totals to be off a little bit.

The "distinct" couild also be a problem; instead of using the nominals view, it would be better to write a query directly against the underlying tables that did not require "distinct".



select * from
(
 select distinct account_title, (tran_amount + vat_amount) as total,project_code from nominalsview
  where managerid = 1052 and tran_date >= '20130401' and tran_date < '20140401'
) x
 pivot
 (
 sum(total)
 for project_code in (CORP1, EMP01, FAC01, FAM01, FIN01, FIR01, MAR01, MAT01, MUN01,
 PLA01, PRIV1, PRO01, PRO02, REC01, REP01, RES01, ADMIN, CON01, COR01, HR001, IT001,
  LIT01, MIS01, PI001, PRF01, PRL01, PROP1, SSD01, TAX01, TRA01, TRU01)
 ) p;
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 

Author Closing Comment

by:forsters
ID: 39191005
Scott your a legend, tidy up the view a bit and removed the distinct from the pivot procedure and its all working well now. Cheers fellah.
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 39191033
Thanks ... it's nice to be a "B"-level legend!
0
 

Author Comment

by:forsters
ID: 39191055
sorry Scott i though i had it on A can it be edited?
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 39191512
LOL, np, you'd have to have it re-opened and then re-close it, not worth the hassle :-)
0
 

Author Comment

by:forsters
ID: 39193890
sorry mate.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

910 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now