Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesDatabase Management

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 2nd, 2011, 06:14 AM
Dapa Dapa is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2011
Posts: 6 Dapa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 12 m 47 sec
Reputation Power: 0
SQL Summary Of Columns by Group

Hello,

I'm pretty new to the world of SQL and I'm trying to create a query to give a summary of certain columns. I've tried numerous ways but I just cant figure it out.

My table is like below:

Code:
Status | REF  | Charging Code | QTY |
------------------------------------
R        | 0001 | CHG-001        | 2     |
------------------------------------
P        | 0002 | CHG-001        | 1     |
------------------------------------
R        | 0003 | CHG-002        | 5     |
------------------------------------
R        | 0004 | CHG-001        | 2     |
------------------------------------
P        | 0005 | CHG-003        | 1     |
------------------------------------
S        | 0006 | CHG-001        | 22   |
------------------------------------


What would be the query to give a count result like below:

Code:
Charging Code | Status_R | Status_P | Status_S
-----------------------------------------------
CHG-001        | 2            | 1           | 1
-----------------------------------------------
CHG-002        | 1            | 0           | 0
-----------------------------------------------
CHG-003        | 0            | 1           | 0


Any help would be greatly appreciated, hope the above makes sense.

Thanks

Reply With Quote
  #2  
Old December 2nd, 2011, 06:19 AM
shammat shammat is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Oct 2003
Location: Germany
Posts: 2,685 shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 3 Days 19 h 56 m 12 sec
Reputation Power: 284
Code:
SELECT charging_code,
       sum(case 
             when status = 'R' then 1 
             else 0
           end)  as Status_R,
       sum(case 
             when status = 'P' then 1 
             else 0
           end)  as Status_P,
       sum(case 
             when status = 'S' then 1 
             else 0
           end)  as Status_S
FROM your_table
GROUP BY charging_code

Reply With Quote
  #3  
Old December 2nd, 2011, 06:38 AM
Dapa Dapa is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2011
Posts: 6 Dapa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 12 m 47 sec
Reputation Power: 0
Thumbs up

Quote:
Originally Posted by shammat
Code:
SELECT charging_code,
       sum(case 
             when status = 'R' then 1 
             else 0
           end)  as Status_R,
       sum(case 
             when status = 'P' then 1 
             else 0
           end)  as Status_P,
       sum(case 
             when status = 'S' then 1 
             else 0
           end)  as Status_S
FROM your_table
GROUP BY charging_code


Brilliant, thanks for your help

Reply With Quote
  #4  
Old December 2nd, 2011, 07:41 AM
Dapa Dapa is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2011
Posts: 6 Dapa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 12 m 47 sec
Reputation Power: 0
Using the same logic as above, how would I return a count of unique values in column REF based on the status of S, for example table below will a few fields with the same REF.
Code:
Status | REF  | Charging Code | QTY |
------------------------------------
R        | 0001 | CHG-001        | 2     |
------------------------------------
P        | 0002 | CHG-001        | 1     |
------------------------------------
R        | 0003 | CHG-002        | 5     |
------------------------------------
R        | 0004 | CHG-001        | 2     |
------------------------------------
P        | 0005 | CHG-003        | 1     |
------------------------------------
S        | 0006 | CHG-001        | 22   |
----------------------------------- 
S        | 0006 | CHG-001        | 5


to

Code:
Charging Code | Status_R | Status_P | UNIQUE_S | SUM_QTY_S
-----------------------------------------------
CHG-001        | 2            | 1           | 1           | 27
-----------------------------------------------
CHG-002        | 1            | 0           | 0           | 0
-----------------------------------------------
CHG-003        | 0            | 1           | 0           | 0

Reply With Quote
  #5  
Old December 5th, 2011, 01:40 PM
Dapa Dapa is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2011
Posts: 6 Dapa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 12 m 47 sec
Reputation Power: 0
solved the problem using DISTINCT. thanks for your help anyway.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > SQL Summary Of Columns by Group

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap