﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2005 / T-SQL (SS2K5)  / More help wit Sum, Pivot and so on... / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sat, 18 May 2013 06:35:11 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: More help wit Sum, Pivot and so on...</title><link>http://www.sqlservercentral.com/Forums/Topic1426212-338-1.aspx</link><description>[quote][b]John_P (3/5/2013)[/b][hr]Looking at Thomas' select statement made me think of one of my favorite functions, that (IMHO) doesn't get enough use:  SIGNNow, this only works here because there are no negative numbers for each of the countries.  SIGN returns one of four values:SIGN(X) = -1  when X &amp;lt; 0SIGN(X) = 0   when X = 0SIGN(X) = 1   when X = 1SIGN(NULL) = NULLThe bottom part of the average calculation:(CASE WHEN ISNULL(Denmark,0)&amp;gt;0 THEN 1 ELSE 0 END +CASE WHEN ISNULL(Finland,0)&amp;gt;0 THEN 1 ELSE 0 END +CASE WHEN ISNULL(Lithuania,0)&amp;gt;0 THEN 1 ELSE 0 END +CASE WHEN ISNULL(Norway,0)&amp;gt;0 THEN 1 ELSE 0 END +CASE WHEN ISNULL(Sweden,0)&amp;gt;0 THEN 1 ELSE 0 END)Can be replace with the following:(SIGN(ISNULL(Denmark,0)) +SIGN(ISNULL(Finland,0)) + SIGN(ISNULL(Lithuania,0)) + SIGN(ISNULL(Norway,0)) + SIGN(ISNULL(Sweden,0))Good Luck!  John[/quote]Thank you John!This was interesting.By the way my solution above had to be changed a little bit to take care of division by zero problem (when all droprates in countries is null or equal to zero) but handled that by adding a -0.0000001 dummy value to the division operator.As I only output a rounded 6 decimal value to the user it doesn´t affect anything.(ugly workaround I know, but easy ;-( Thank´s again!  //  Thomas</description><pubDate>Wed, 06 Mar 2013 00:35:03 GMT</pubDate><dc:creator>tota00</dc:creator></item><item><title>RE: More help wit Sum, Pivot and so on...</title><link>http://www.sqlservercentral.com/Forums/Topic1426212-338-1.aspx</link><description>Looking at Thomas' select statement made me think of one of my favorite functions, that (IMHO) doesn't get enough use:  SIGNNow, this only works here because there are no negative numbers for each of the countries.  SIGN returns one of four values:SIGN(X) = -1  when X &amp;lt; 0SIGN(X) = 0   when X = 0SIGN(X) = 1   when X = 1SIGN(NULL) = NULLThe bottom part of the average calculation:(CASE WHEN ISNULL(Denmark,0)&amp;gt;0 THEN 1 ELSE 0 END +CASE WHEN ISNULL(Finland,0)&amp;gt;0 THEN 1 ELSE 0 END +CASE WHEN ISNULL(Lithuania,0)&amp;gt;0 THEN 1 ELSE 0 END +CASE WHEN ISNULL(Norway,0)&amp;gt;0 THEN 1 ELSE 0 END +CASE WHEN ISNULL(Sweden,0)&amp;gt;0 THEN 1 ELSE 0 END)Can be replace with the following:(SIGN(ISNULL(Denmark,0)) +SIGN(ISNULL(Finland,0)) + SIGN(ISNULL(Lithuania,0)) + SIGN(ISNULL(Norway,0)) + SIGN(ISNULL(Sweden,0))Good Luck!  John</description><pubDate>Tue, 05 Mar 2013 08:57:58 GMT</pubDate><dc:creator>John_P</dc:creator></item><item><title>RE: More help wit Sum, Pivot and so on...</title><link>http://www.sqlservercentral.com/Forums/Topic1426212-338-1.aspx</link><description>Problem solved! :-)Here is the sql-statement that did the trick!SELECT *, (ISNULL(Denmark,0) + ISNULL(Finland,0) + ISNULL(Lithuania,0) + ISNULL(Norway,0) + ISNULL(Sweden,0)) /(CASE WHEN ISNULL(Denmark,0)&amp;gt;0 THEN 1 ELSE 0 END +CASE WHEN ISNULL(Finland,0)&amp;gt;0 THEN 1 ELSE 0 END +CASE WHEN ISNULL(Lithuania,0)&amp;gt;0 THEN 1 ELSE 0 END +CASE WHEN ISNULL(Norway,0)&amp;gt;0 THEN 1 ELSE 0 END +CASE WHEN ISNULL(Sweden,0)&amp;gt;0 THEN 1 ELSE 0 END) AS Total FROM (SELECT DropRate,MarketingName, Manufacturer, Country FROM tblTest) s PIVOT (AVG(DropRate)FOR Country IN ([Denmark], [Finland], [Lithuania], [Norway], [Sweden]))P Many thanks to Visakh Murukes for the help!Best  //  Thomas</description><pubDate>Tue, 05 Mar 2013 03:52:17 GMT</pubDate><dc:creator>tota00</dc:creator></item><item><title>RE: More help wit Sum, Pivot and so on...</title><link>http://www.sqlservercentral.com/Forums/Topic1426212-338-1.aspx</link><description>Okey!I have come a bit on the way to a solution and have included a script for creating a test-table with sample dummy data in it!The result from the sql-statement now works fine except the total sum result that has to be divided by the number of columns that do not return null values or is greater then zero so in this case with the sample data it should be divided by 3 but how do I solve that?Sql statement:[code="sql"]SELECT *, ISNULL(Denmark,0) + ISNULL(Finland,0) + ISNULL(Lithuania,0) + ISNULL(Norway,0) + ISNULL(Sweden,0)  AS Total FROM (SELECT DropRate,MarketingName, Manufacturer, Country FROM tblTest) s PIVOT (AVG(DropRate)FOR Country IN ([Denmark], [Finland], [Lithuania], [Norway], [Sweden]))P [/code]Generates output:MarketingName	Manufacturer	Denmark	Finland	Lithuania	Norway	Sweden	TotaliPhone 3G	Apple	1.408000000000	1.485000000000	0.000000000000	NULL	2.625000000000	5.518000000000What I need is:Should be: (difference is the division by three in this example)[code="sql"]SELECT *, (ISNULL(Denmark,0) + ISNULL(Finland,0) + ISNULL(Lithuania,0) + ISNULL(Norway,0) + ISNULL(Sweden,0)) / 3  AS Total FROM (SELECT DropRate,MarketingName, Manufacturer, Country FROM tblTest) s PIVOT (AVG(DropRate)FOR Country IN ([Denmark], [Finland], [Lithuania], [Norway], [Sweden]))P [/code]That generates output:MarketingName	Manufacturer	Denmark	Finland	Lithuania	Norway	Sweden	TotaliPhone 3G	Apple	1.408000000000	1.485000000000	0.000000000000	NULL	2.625000000000	1.839333333333But from time over time the amount of null or zero values in the columns will differ so I need intelligence in the sql-statement that takes care of this and this is above my knowledge for the moment :-(And of course this is very simplified and I have my reason to include a country that is not represented in the sample data and so on ;-)Here you go with the sample table.[code="sql"]/****** Object:  Table [dbo].[tblTest]    Script Date: 2013-03-05 09:58:12 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE TABLE [dbo].[tblTest](	[DropRate] [decimal](38, 12) NULL,	[Country] [nchar](10) NULL,	[MarketingName] [nvarchar](500) NULL,	[Manufacturer] [nvarchar](200) NULL) ON [PRIMARY]GOINSERT [dbo].[tblTest] ([DropRate], [Country], [MarketingName], [Manufacturer]) VALUES (CAST(1.547000000000 AS Decimal(38, 12)), N'Denmark   ', N'iPhone 3G', N'Apple')GOINSERT [dbo].[tblTest] ([DropRate], [Country], [MarketingName], [Manufacturer]) VALUES (CAST(1.274000000000 AS Decimal(38, 12)), N'Denmark   ', N'iPhone 3G', N'Apple')GOINSERT [dbo].[tblTest] ([DropRate], [Country], [MarketingName], [Manufacturer]) VALUES (CAST(1.476000000000 AS Decimal(38, 12)), N'Finland   ', N'iPhone 3G', N'Apple')GOINSERT [dbo].[tblTest] ([DropRate], [Country], [MarketingName], [Manufacturer]) VALUES (CAST(1.494000000000 AS Decimal(38, 12)), N'Finland   ', N'iPhone 3G', N'Apple')GOINSERT [dbo].[tblTest] ([DropRate], [Country], [MarketingName], [Manufacturer]) VALUES (CAST(0.000000000000 AS Decimal(38, 12)), N'Lithuania ', N'iPhone 3G', N'Apple')GOINSERT [dbo].[tblTest] ([DropRate], [Country], [MarketingName], [Manufacturer]) VALUES (CAST(1.513000000000 AS Decimal(38, 12)), N'Denmark   ', N'iPhone 3G', N'Apple')GOINSERT [dbo].[tblTest] ([DropRate], [Country], [MarketingName], [Manufacturer]) VALUES (CAST(1.431000000000 AS Decimal(38, 12)), N'Denmark   ', N'iPhone 3G', N'Apple')GOINSERT [dbo].[tblTest] ([DropRate], [Country], [MarketingName], [Manufacturer]) VALUES (CAST(2.625000000000 AS Decimal(38, 12)), N'Sweden    ', N'iPhone 3G', N'Apple')GOINSERT [dbo].[tblTest] ([DropRate], [Country], [MarketingName], [Manufacturer]) VALUES (CAST(1.370000000000 AS Decimal(38, 12)), N'Denmark   ', N'iPhone 3G', N'Apple')GOINSERT [dbo].[tblTest] ([DropRate], [Country], [MarketingName], [Manufacturer]) VALUES (CAST(1.384000000000 AS Decimal(38, 12)), N'Denmark   ', N'iPhone 3G', N'Apple')GOINSERT [dbo].[tblTest] ([DropRate], [Country], [MarketingName], [Manufacturer]) VALUES (CAST(1.337000000000 AS Decimal(38, 12)), N'Denmark   ', N'iPhone 3G', N'Apple')GO[/code]</description><pubDate>Tue, 05 Mar 2013 02:19:45 GMT</pubDate><dc:creator>tota00</dc:creator></item><item><title>RE: More help wit Sum, Pivot and so on...</title><link>http://www.sqlservercentral.com/Forums/Topic1426212-338-1.aspx</link><description>[quote]You need to calculate your averages using a GROUP BY and then add these back to your PIVOT.Here's a start on your sample data: "snip"SELECT Country, Manufacturer, MarketingName, AvgDropRate=AVG(DropRate)FROM @PhonesGROUP BY Country, Manufacturer, MarketingName[/quote]Thank´s dwain! Will start with this :-)Best  //  Thomas</description><pubDate>Tue, 05 Mar 2013 00:21:53 GMT</pubDate><dc:creator>tota00</dc:creator></item><item><title>RE: More help wit Sum, Pivot and so on...</title><link>http://www.sqlservercentral.com/Forums/Topic1426212-338-1.aspx</link><description>[quote][b]tota00 (3/4/2013)[/b][hr]This is fine. I have the total drop rate however I am also looking for the average drop rate per country as well as the average drop rate for the total.  (Total rate / # of countries per model) Ie. IPhone 4 occurs Three times with droprate 1 an 2 and 3 for sweden and 2 and 4 and 6 for Norway then it should produce the average 2 for sweden and 4 for norway and total should be showing (countries + countries) divided by number of countries like (2+4)/2 in this case because it shouldn´t divide by countries that return null values when summaring and Dividing the total.Hope I didn+t confuse you to much!Thank´s in advance  //  TT[/quote]You need to calculate your averages using a GROUP BY and then add these back to your PIVOT.Here's a start on your sample data:[code="sql"]DECLARE @Phones TABLE    (DropRate DECIMAL(38,12)    ,Country VARCHAR(10)    ,MarketingName VARCHAR(50)    ,Manufacturer VARCHAR(50))INSERT INTO @PhonesSELECT 0.000000000000,'Denmark','Zoarmon','Intel'UNION ALL SELECT 0.000000000000,'Denmark','USB316','Sierra Wireless'UNION ALL SELECT 0.000000000000,'Denmark','XP3300-AR1 (P25C005AA)','Sonim'UNION ALL SELECT 0.000000000000,'Denmark','700C','Intermec Technologies Corp.'UNION ALL SELECT 4.000000000000,'Denmark','R6230GE','BlackBerry'UNION ALL SELECT 0.000000000000,'Denmark','SGH-S300','Samsung'UNION ALL SELECT 0.000000000000,'Denmark','Treo 600','Palm'UNION ALL SELECT 0.000000000000,'Sweden','Zoarmon','Intel'UNION ALL SELECT 0.000000000000,'Sweden','USB316','Sierra Wireless'UNION ALL SELECT 0.000000000000,'Sweden','XP3300-AR1 (P25C005AA)','Sonim'UNION ALL SELECT 0.000000000000,'Sweden','700C','Intermec Technologies Corp.'UNION ALL SELECT 2.000000000000,'Sweden','R6230GE','BlackBerry'UNION ALL SELECT 0.000000000000,'Sweden','SGH-S300','Samsung'UNION ALL SELECT 0.000000000000,'Sweden','Treo 600','Palm'UNION ALL SELECT 0.000000000000,'Finland','Zoarmon','Intel'UNION ALL SELECT 0.000000000000,'Finland','USB316','Sierra Wireless'UNION ALL SELECT 0.000000000000,'Finland','XP3300-AR1 (P25C005AA)','Sonim'UNION ALL SELECT 0.000000000000,'Finland','700C','Intermec Technologies Corp.'UNION ALL SELECT 2.000000000000,'Finland','R6230GE','BlackBerry'UNION ALL SELECT 0.000000000000,'Finland','SGH-S300','Samsung'UNION ALL SELECT 0.000000000000,'Finland','Treo 600','Palm'SELECT Country, Manufacturer, MarketingName, AvgDropRate=AVG(DropRate)FROM @PhonesGROUP BY Country, Manufacturer, MarketingName[/code]</description><pubDate>Mon, 04 Mar 2013 18:21:01 GMT</pubDate><dc:creator>dwain.c</dc:creator></item><item><title>RE: More help wit Sum, Pivot and so on...</title><link>http://www.sqlservercentral.com/Forums/Topic1426212-338-1.aspx</link><description>[quote][b]tota00 (3/4/2013)[/b][hr][quote][b]Phil Parkin (3/4/2013)[/b][hr]Please follow the link in my signature to get details of how to post sample DDL, data and desired results to get the best response to your question.[/quote]I´m sorry and will post a script for generating a table containing testdata! Sorry again!//  TT[/quote]You're pretty new here, so no need for apologies.You will find that you get far more responses - with working and tested code - if you take the time to post your question in such a way.</description><pubDate>Mon, 04 Mar 2013 06:54:39 GMT</pubDate><dc:creator>Phil Parkin</dc:creator></item><item><title>RE: More help wit Sum, Pivot and so on...</title><link>http://www.sqlservercentral.com/Forums/Topic1426212-338-1.aspx</link><description>[quote][b]Phil Parkin (3/4/2013)[/b][hr]Please follow the link in my signature to get details of how to post sample DDL, data and desired results to get the best response to your question.[/quote]I´m sorry and will post a script for generating a table containing testdata! Sorry again!//  TT</description><pubDate>Mon, 04 Mar 2013 06:51:50 GMT</pubDate><dc:creator>tota00</dc:creator></item><item><title>RE: More help wit Sum, Pivot and so on...</title><link>http://www.sqlservercentral.com/Forums/Topic1426212-338-1.aspx</link><description>Please follow the link in my signature to get details of how to post sample DDL, data and desired results to get the best response to your question.</description><pubDate>Mon, 04 Mar 2013 06:41:35 GMT</pubDate><dc:creator>Phil Parkin</dc:creator></item><item><title>More help wit Sum, Pivot and so on...</title><link>http://www.sqlservercentral.com/Forums/Topic1426212-338-1.aspx</link><description>Hi!First thanks for the help I got a couple of weeks ago! :-)Now to my new proble that is to change this statement a bit as it works halfway...SELECT *,  ISNULL(Denmark,0) + ISNULL(Finland,0) + ISNULL(Lithuania,0) + ISNULL(Norway,0) + ISNULL(Sweden,0)  AS Total FROM (SELECT DropRate,MarketingName, Manufacturer, Country FROM ReportedNetInfoData) s PIVOT (SUM(DropRate) FOR Country IN ([Denmark], [Finland], [Lithuania], [Norway], [Sweden]))PThe simplified table looks like this:DropRate		Country		MarketingName		Manufacturer0.000000000000	Denmark   	Zoarmon			Intel0.000000000000	Denmark   	USB316			Sierra Wireless0.000000000000	Denmark   	XP3300-AR1 (P25C005AA)	Sonim0.000000000000	Denmark   	700C			Intermec Technologies Corp.4.762000000000	Denmark   	R6230GE			BlackBerry0.000000000000	Denmark   	SGH-S300		Samsung0.000000000000	Denmark   	Treo 600		PalmThe result from the sql-statement is something like this:(the null values under all countries except denmark depends on no data from them yet)MarketingName		Manufacturer	Denmark			Finland	Lithuania	Norway	Sweden	Total5130 XpressMusic	Nokia		153.725000000000	NULL	NULL		NULL	NULL	153.7250000000003310			Nokia		151.236000000000	NULL	NULL		NULL	NULL	151.2360000000001100			Nokia		151.206000000000	NULL	NULL		NULL	NULL	151.206000000000iPhone 4		Apple		144.371000000000	NULL	NULL		NULL	NULL	144.3710000000001208			Nokia		133.742000000000	NULL	NULL		NULL	NULL	133.742000000000iPhone 4S		Apple		130.181000000000	NULL	NULL		NULL	NULL	130.181000000000X1-01			Nokia		129.931000000000	NULL	NULL		NULL	NULL	129.931000000000SGH-E250		Samsung		118.778000000000	NULL	NULL		NULL	NULL	118.7780000000006300			Nokia		112.905000000000	NULL	NULL		NULL	NULL	112.905000000000101,  1010		Nokia		110.319000000000	NULL	NULL		NULL	NULL	110.319000000000This is fine. I have the total drop rate however I am also looking for the average drop rate per country as well as the average drop rate for the total.  (Total rate / # of countries per model) Ie. IPhone 4 occurs Three times with droprate 1 an 2 and 3 for sweden and 2 and 4 and 6 for Norway then it should produce the average 2 for sweden and 4 for norway and total should be showing (countries + countries) divided by number of countries like (2+4)/2 in this case because it shouldn´t divide by countries that return null values when summaring and Dividing the total.Hope I didn+t confuse you to much!Thank´s in advance  //  TT</description><pubDate>Mon, 04 Mar 2013 06:37:16 GMT</pubDate><dc:creator>tota00</dc:creator></item></channel></rss>