Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Help with SUM and Group By Expand / Collapse
Author
Message
Posted Thursday, January 31, 2013 6:24 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, March 08, 2013 4:18 AM
Points: 9, Visits: 18
[code="plain"]
Hi!

I have a table shown below (just a part of it) and need to aggregate the data as the example under the table.

I can solve it if i just want the total but needs the sql-statement to return the sum for every item and country (zero if it doesn´t exists for that country and so on)

TABLE and data in it
--------------------


Total_Active Country ReportYear ReportMonth MarketingName Manufacturer
30 Finland 2012 November MTCBA-G-UF4 Multi-Tech Systems, Inc
3 Finland 2012 November Treo 680 Palm
1 Finland 2012 November Treo 750na Palm
19 Finland 2012 November iPhone Apple
1 Finland 2012 November ThinkPad T61 14 Lenovo
1 Finland 2012 November CU515 LG
3 Finland 2012 November W580 Sony Ericsson
105 Finland 2012 November iPhone Apple
165 Finland 2012 November iPhone Apple
36 Finland 2012 November GM400 Ubinetics
2 Finland 2012 November N/A N/A
30 Sweden 2012 November MTCBA-G-UF4 Multi-Tech Systems, Inc
103 Sweden 2012 November Treo 680 Palm
7 Sweden 2012 November Treo 750na Palm
23 Sweden 2012 November iPhone Apple
165 Norway 2012 November iPhone Apple
30 Norway 2012 November MTCBA-G-UF4 Multi-Tech Systems, Inc
103 Norway 2012 November Treo 680 Palm
7 Norway 2012 November Treo 750na Palm
23 Norway 2012 November iPhone 5 Apple

The Output I need from the sql-statement
is something like this and of course the month has to be november as well...
and as You see some items exists more than one time like "apple IPhone" in Finland and some exits only in one country etc..
(You get the idéa)
-----------------
MarketingName Manufacturer Finland Sweden Norway Total
MTCBA-G-UF4 Multi-Tech Systems, Inc 30 30 30 90
Treo 680 Palm 3 103 103 209
Treo 750na Palm 1 7 7 15
iPhone Apple 289 23 165 477
ThinkPad T61 14 Lenovo 1 0 0 0
CU515 LG 1 0 0 1
W580 Sony Ericsson 105 0 0 105
iPhone 5 Apple 0 0 23 23
GM400 Ubinetics 36 0 0 23

Thanks in advance // TT
[/code]
Post #1414070
Posted Thursday, January 31, 2013 6:41 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, March 08, 2013 4:18 AM
Points: 9, Visits: 18
Solved!

I got help from another forum and here is the answer that worked like a charm!

You can use the PIVOT operator like shown below. If you have an unknown number of countries, or if the country names are not known in advance, you would have to use a dynamic query to construct the pivot.

SELECT
*,
ISNULL(Finland,0) + ISNULL(SWeden,0) + ISNULL(Norway,0) AS Total
FROM
(
SELECT Total_Active,MarketingName, Manufacturer, Country FROM TheTable
) s
PIVOT (SUM(Total_active) FOR Country IN ([Finland],[Sweden],[Norway]))P

Post #1414091
Posted Thursday, January 31, 2013 6:11 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 12:22 AM
Points: 2,370, Visits: 3,250
Right! Works until you have more than 3 countries.

I prefer the crosstab query approach myself. Jeff Moden has a couple of nifty articles on the subject:
http://www.sqlservercentral.com/articles/T-SQL/63681/
http://www.sqlservercentral.com/articles/Crosstab/65048/

Part 2 (second link) describes how you could handle the case when you don't know how many countries may appear in your data. Look for Dynamic Crosstabs.



No loops! No CURSORs! No RBAR! Hoo-uh!

INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?

Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Are you too recursively challenged?
Splitting strings based on patterns can be fast!
Post #1414414
Posted Friday, February 01, 2013 9:56 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, March 08, 2013 4:18 AM
Points: 9, Visits: 18
Hi Dwain!

I´m dynamicly building my statement and adding and removing countrys from the sql-question on the fly depending on what filteroptions the user has choosen.

I´m for the moment generating this statement with everything between 1 and 22 countries and a table that contains 1250 000 rows and it works perfectly and still haven´t got any performance problems.

If an new country is added or removed from the database it doesn´t matter as the statement is buildt dynamically and I have a cached list of them to loop through.

However the links You mentioned looks very interesting and I´m gonna test the performance by them contra the above solution.

My question above was really simplified by the way and is much more complexed in reality so there are more parameters involved but that one pointed me to an solution that for now works great!

Anyway! Thanks for Your reply and everything that improves my T-SQL skills are welcome so testing the stuff You wrote is gonna be interesting!

Best // Thomas

Ps. Hope that I can contribute in this forum some day but for the moment You fellows are a little(irony) ahead of me!
Post #1414747
Posted Sunday, February 03, 2013 5:25 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 12:22 AM
Points: 2,370, Visits: 3,250
tota00 (2/1/2013)
Hi Dwain!

I´m dynamicly building my statement and adding and removing countrys from the sql-question on the fly depending on what filteroptions the user has choosen.

I´m for the moment generating this statement with everything between 1 and 22 countries and a table that contains 1250 000 rows and it works perfectly and still haven´t got any performance problems.

If an new country is added or removed from the database it doesn´t matter as the statement is buildt dynamically and I have a cached list of them to loop through.

However the links You mentioned looks very interesting and I´m gonna test the performance by them contra the above solution.

My question above was really simplified by the way and is much more complexed in reality so there are more parameters involved but that one pointed me to an solution that for now works great!

Anyway! Thanks for Your reply and everything that improves my T-SQL skills are welcome so testing the stuff You wrote is gonna be interesting!

Best // Thomas

Ps. Hope that I can contribute in this forum some day but for the moment You fellows are a little(irony) ahead of me!


Sounds like you've got it covered then. Just wanted to make sure.



No loops! No CURSORs! No RBAR! Hoo-uh!

INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?

Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Are you too recursively challenged?
Splitting strings based on patterns can be fast!
Post #1415078
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse