Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 3rd, 2003, 05:09 PM
akc akc is offline
Registered User
 
Join Date: Oct 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Aggregate Sum Problem

Hi,

Here is my data:

PosID | Name | Gender | Ethnicity
---------------------------------
828 alan male caucasian
828 jen female black
828 cathy female black
829 david male caucasian
829 peter male caucasian
829 vince male black

Here is the outcome I need

PosID | male/caucasian | female/caucasian | male/black / female/black
828 1 0 0 2
829 2 0 1 0

Any help will be appreciated.
Thanks in advance.

Alan
 
Old November 3rd, 2003, 05:38 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

You're try to do a variation on a cross tab (or pivot) query.

It's ugly.

The idea is to create an expression for each output column where you SUM either a 1 or a zero depending on whether the column criteria match for each desired output column:
Code:
SELECT PosID, 
    SUM(CASE WHEN Gender='male' AND Ethnicity='caucasian' THEN 1 ELSE 0 END) as 'male/caucassian',
    SUM(CASE WHEN Gender='female' AND Ethnicity='caucasian' THEN 1 ELSE 0 END) as 'female/caucasian',
...
GROUP BY PosID
ORDER BY PosID;
Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old November 4th, 2003, 10:07 AM
akc akc is offline
Registered User
 
Join Date: Oct 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Jeff.

Alan






Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use the SUM Aggregate gregalb SQL Server 2000 1 May 10th, 2007 01:19 AM
Problem with Sum() swierzym Reporting Services 0 October 18th, 2006 01:34 PM
Aggregate function MAX creating problem..? Manu SQL Language 4 May 30th, 2006 04:41 AM
Aggregate Function MAX, SUM Manu SQL Language 2 May 30th, 2006 04:40 AM
Problem with AGGREGATE FUNCTIONS in SQL SERVER 200 vikiboye SQL Server 2005 1 February 20th, 2006 12:07 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.