Notices
Computer & Technology Related Post here for help and discussion of computing and related technology. Internet, TVs, phones, consoles, computers, tablets and any other gadgets.

Any SQL experts here?

Thread Tools
 
Search this Thread
 
Old 01 April 2008, 03:06 PM
  #1  
Dracoro
Scooby Regular
Thread Starter
 
Dracoro's Avatar
 
Join Date: Sep 2001
Location: A powerslide near you
Posts: 10,261
Likes: 0
Received 0 Likes on 0 Posts
Default Any SQL experts here?

Have some tables with multiple columns and want to count the values in each column.

e.g. columns in table are a1, a2, ....a99.

All column hold different types of data.

So, for example, I would expect results as follows:
col, val, count
a1, 89, 232
a1, 23, 9876
a1, 1221, 23
a2, 'Bob', 234
a2, 'Fred', 4322
a3, 'The times', 543
a3, 'Telegraph', 22231
etc......
Old 01 April 2008, 10:57 PM
  #2  
finalzero
Scooby Regular
 
finalzero's Avatar
 
Join Date: Jan 2007
Location: Buckinghamshire
Posts: 2,272
Likes: 0
Received 0 Likes on 0 Posts
Default

select
count(a1) as 'Column 1',
count(a2) as 'Column 2',
count(a3) as 'Column 3',
count(a4) as 'Column 4',
count(a5) as 'Column 5'
from alist
Old 02 April 2008, 11:06 AM
  #3  
Dracoro
Scooby Regular
Thread Starter
 
Dracoro's Avatar
 
Join Date: Sep 2001
Location: A powerslide near you
Posts: 10,261
Likes: 0
Received 0 Likes on 0 Posts
Default

thanks, but I need a group of all possible values in a1, a2 etc.
Old 02 April 2008, 02:18 PM
  #4  
AlexJReid
Scooby Regular
 
AlexJReid's Avatar
 
Join Date: Apr 2004
Location: Tynemouth
Posts: 213
Likes: 0
Received 0 Likes on 0 Posts
Default

select count(col) as c, col from table group by col;

and repeat for each column you want to do this on. could do some stuff with pivots and systables if you wanted to automate it!

Last edited by AlexJReid; 02 April 2008 at 02:21 PM.
Old 02 April 2008, 07:13 PM
  #5  
GCollier
Scooby Regular
 
GCollier's Avatar
 
Join Date: Jun 1998
Posts: 1,198
Likes: 0
Received 0 Likes on 0 Posts
Default

What database are you using?
Old 03 April 2008, 10:50 AM
  #6  
GaryK
Scooby Regular
 
GaryK's Avatar
 
Join Date: Sep 1999
Location: Bedfordshire
Posts: 4,037
Likes: 0
Received 0 Likes on 0 Posts
Default

If you need to count all columns then you need to include all columns in the select statement, long-winded yes unless you write a stored proc which will build the sql for you and use expression evaluation to run the dynamic sql. Bottom line is its very poorly modelled (which may or may not be something you can change).
Old 03 April 2008, 11:14 AM
  #7  
Dracoro
Scooby Regular
Thread Starter
 
Dracoro's Avatar
 
Join Date: Sep 2001
Location: A powerslide near you
Posts: 10,261
Likes: 0
Received 0 Likes on 0 Posts
Default

Exactly, just how a client datbase storing it's data :banghead:

on mysql - which hasn't impressed me tbh coming from a sqlserver/oracle/db2 background!
Related Topics
Thread
Thread Starter
Forum
Replies
Last Post
roysc
ScoobyNet General
2
16 September 2015 09:10 AM
The Joshua Tree
Computer & Technology Related
18
11 September 2015 09:24 PM
stevem2k
Non Scooby Related
8
06 July 2011 11:11 PM
Mkhan
Non Scooby Related
14
09 August 2001 02:07 AM
SDB
Non Scooby Related
2
26 May 2001 11:55 PM



Quick Reply: Any SQL experts here?



All times are GMT +1. The time now is 10:16 AM.