Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Sql server pivot table summing wrong.

Posted on 2013-05-22
9
Medium Priority
?
256 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 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 70

Accepted Solution

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

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 70

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
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.

972 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