Solved

Sql server pivot table summing wrong.

Posted on 2013-05-22
9
246 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Get the latest status 8 32
SQL View nearest date 5 38
Tsql query 6 22
sql server query 6 10
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…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

831 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