Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 3rd, 2003, 05:09 PM
akc akc is offline
Registered User
 
Join Date: Oct 2003
Location: , , .
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
Reply With Quote
  #2 (permalink)  
Old November 3rd, 2003, 05:38 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
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
Reply With Quote
  #3 (permalink)  
Old November 4th, 2003, 10:07 AM
akc akc is offline
Registered User
 
Join Date: Oct 2003
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Jeff.

Alan

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 08:47 PM.


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