Solved

Sql server pivot table summing wrong.

Posted on 2013-05-22
9
249 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
[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
9 Comments
 
LVL 41

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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 69

Accepted Solution

by:
Scott Pletcher 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
 

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:Scott Pletcher
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:Scott Pletcher
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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 INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

751 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