PostgreSQL Help
 
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 ForumsDatabasesPostgreSQL Help

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 January 22nd, 2013, 08:22 PM
stanbridge stanbridge is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Location: Australia
Posts: 7 stanbridge User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 45 m
Reputation Power: 0
Transpose - is there a better way than this?

Hi there,

I have a whole bunch of bank records that need to be transposed (grouped by country).

The method below works OK, but requires the various groupings of the subquery to be listed manually. This is fine for a small number of banks (6 in this instance) but as you can imagine, would be painful if I had, for example, 100 banks!

See code below...
Code:
/* bank funds grouped by country (rows) and bank (columns) */
SELECT 
  country AS "Country",
  MAX(CASE WHEN mydata.bank_fund = 'Bank of America' OR 'Merrill Lynch' THEN mydata.count ELSE NULL END) AS "Bank of America",
  MAX(CASE WHEN mydata.bank_fund = 'Bank of China' THEN mydata.count ELSE NULL END) AS "Bank of China",          
  MAX(CASE WHEN mydata.bank_fund = 'Bank of London' THEN mydata.count ELSE NULL END) AS "Bank of London", 
  MAX(CASE WHEN mydata.bank_fund = 'Central Russian Bank' THEN mydata.count ELSE NULL END) AS "Central Russian Bank", 
  MAX(CASE WHEN mydata.bank_fund = 'Australia Bank' THEN mydata.count ELSE NULL END) AS "Australia Bank", 
  MAX(CASE WHEN mydata.bank_fund = 'African National Bank' THEN mydata.count ELSE NULL END) AS "African National Bank",
  SUM(mydata.count) AS "GRAND TOTAL"
FROM 
(
   SELECT 
     country,
     bank_fund,
     count(1)
   FROM financial.banks
   WHERE date BETWEEN '2012-11-01' AND '2012-11-30'
   GROUP BY country, bank_fund
   ORDER BY bank_fund
) AS mydata
GROUP BY country
ORDER BY country

Can somebody show me a more efficient way to do this?


Cheers,

Stanbridge

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesPostgreSQL Help > Transpose - is there a better way than this?

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