Solved

Total Column to sum row

Posted on 2014-09-02
4
160 Views
Last Modified: 2014-09-02
I have a simple SQL Server cross tab select query. I'm trying to simply add a column at the far right named Total to add up the 7 column rows to the left but it keeps saying that its an invalid column name. I was trying to
Sum(A+B+C+D+E+F+G) AS Total

SELECT ProductsDetails.Gender, ProductsDetails.CorePN, ProductsDetails.productdescription, ProductsDetails.Color, ProductsDetails.Category, 
			SUM(CASE WHEN ProductsDetails.SizeNo = 1 THEN Products.stockstatus ELSE 0 END) AS A, 
			SUM(CASE WHEN ProductsDetails.SizeNo = 2 THEN Products.stockstatus ELSE 0 END) AS B, 
			SUM(CASE WHEN ProductsDetails.SizeNo = 3 THEN Products.stockstatus ELSE 0 END) AS C, 
			SUM(CASE WHEN ProductsDetails.SizeNo = 4 THEN Products.stockstatus ELSE 0 END) AS D, 
			SUM(CASE WHEN ProductsDetails.SizeNo = 5 THEN Products.stockstatus ELSE 0 END) AS E, 
			SUM(CASE WHEN ProductsDetails.SizeNo = 6 THEN Products.stockstatus ELSE 0 END) AS F, 
			SUM(CASE WHEN ProductsDetails.SizeNo = 7 THEN Products.stockstatus ELSE 0 END) AS G
FROM Products INNER JOIN ProductsDetails ON Products.productcode = ProductsDetails.productcode 
GROUP BY ProductsDetails.Site, ProductsDetails.Gender, ProductsDetails.CorePN, ProductsDetails.productdescription, ProductsDetails.Color, ProductsDetails.Category 
HAVING (ProductsDetails.Site LIKE N'Event Stock') 
ORDER BY ProductsDetails.Gender, ProductsDetails.CorePN, ProductsDetails.Category, ProductsDetails.Color

Open in new window

0
Comment
Question by:Southern_Gentleman
[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
  • 3
4 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 40300109
There are several ways of skinning this cat and it really depends on your version of SQL Server, if you are still using SQL Server 2000 you can do this:
SELECT  d.Gender,
	d.CorePN,
	d.productdescription,
	d.Color,
	d.Category,
	SUM(CASE WHEN d.SizeNo = 1 THEN p.stockstatus
		ELSE 0
	END) AS A,
	SUM(CASE WHEN d.SizeNo = 2 THEN p.stockstatus
		ELSE 0
	END) AS B,
	SUM(CASE WHEN d.SizeNo = 3 THEN p.stockstatus
		ELSE 0
	END) AS C,
	SUM(CASE WHEN d.SizeNo = 4 THEN p.stockstatus
		ELSE 0
	END) AS D,
	SUM(CASE WHEN d.SizeNo = 5 THEN p.stockstatus
		ELSE 0
	END) AS E,
	SUM(CASE WHEN d.SizeNo = 6 THEN p.stockstatus
		ELSE 0
	END) AS F,
	SUM(CASE WHEN d.SizeNo = 7 THEN p.stockstatus
		ELSE 0
	END) AS G,
	SUM(CASE WHEN d.SizeNo IN (1, 2, 3, 4, 5, 6, 7) THEN p.stockstatus
		ELSE 0
	END) AS Total
FROM    Products p
	INNER JOIN ProductsDetails d ON p.productcode = d.productcode
WHERE   d.[Site] LIKE N'Event Stock'
GROUP BY d.[Site],
	d.Gender,
	d.CorePN,
	d.productdescription,
	d.Color,
	d.Category
ORDER BY d.Gender,
	d.CorePN,
	d.Category,
	d.Color

Open in new window

Notice the way, that I am using aliases, this makes it easier to read.  Also, notice that I placed your condition in the WHERE clause instead of the HAVING.  Typically you use LIKE with wildcards so instead of say d.Site LIKE N'Event Stock', it could be d.Site LIKE N'Event Stock%'.  If you do want an exact match then I would write it as d.Site = N'Event Stock' and lose the d.Site in the GROUP BY clause
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 40300131
This can also be resolved with a derived table as follows:
SELECT Gender, CorePN, productdescription, Color, Category,
	A, B, C, D, E, F, G, A + B + C + D + E + F + G Total
FROM (SELECT	d.Gender,
		d.CorePN,
		d.productdescription,
		d.Color,
		d.Category,
		SUM(CASE WHEN d.SizeNo = 1 THEN p.stockstatus
			ELSE 0
		END) AS A,
		SUM(CASE WHEN d.SizeNo = 2 THEN p.stockstatus
			ELSE 0
		END) AS B,
		SUM(CASE WHEN d.SizeNo = 3 THEN p.stockstatus
			ELSE 0
		END) AS C,
		SUM(CASE WHEN d.SizeNo = 4 THEN p.stockstatus
			ELSE 0
		END) AS D,
		SUM(CASE WHEN d.SizeNo = 5 THEN p.stockstatus
			ELSE 0
		END) AS E,
		SUM(CASE WHEN d.SizeNo = 6 THEN p.stockstatus
			ELSE 0
		END) AS F,
		SUM(CASE WHEN d.SizeNo = 7 THEN p.stockstatus
			ELSE 0
		END) AS G,
		SUM(CASE WHEN d.SizeNo IN (1, 2, 3, 4, 5, 6, 7) THEN p.stockstatus
			ELSE 0
		END) AS Total
	FROM    Products p
		INNER JOIN ProductsDetails d ON p.productcode = d.productcode
	WHERE   d.[Site] LIKE N'Event Stock'
	GROUP BY d.[Site],
		d.Gender,
		d.CorePN,
		d.productdescription,
		d.Color,
		d.Category) a
ORDER BY
	Gender,
	CorePN,
	Category,
	Color

Open in new window

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 40300138
If you are using SQL Server 2005 and up then you can resort to using a CTE or a PIVOT.
0
 

Author Closing Comment

by:Southern_Gentleman
ID: 40300237
Thanks a bunch Anthony, I usually do alias for the tables but figure I give a little more description. Thanks again for the extra info and other ways to go about this.
0

Featured Post

 Database Backup and Recovery Best Practices

Join Percona’s, Architect, Manjot Singh as he presents Database Backup and Recovery Best Practices (with a Focus on MySQL) on Thursday, July 27, 2017 at 11:00 am PDT / 2:00 pm EDT (UTC-7). In the case of a failure, do you know how long it will take to restore your database?

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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.

627 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