counting possible values of one column

Thread Solved

Join Date: May 2007
Posts: 52
Reputation: hashinclude is an unknown quantity at this point 
Solved Threads: 0
hashinclude hashinclude is offline Offline
Junior Poster in Training

counting possible values of one column

 
0
  #1
Jul 21st, 2009
Hello,

please bear with me if this question is stupid.
I'm fairly new to this and i really need to know. is it possible to use "SELECT COUNT" to get the number of DIFFERENT values one column currently holds grouped by a value from another column.?
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 119
Reputation: cgyrob is on a distinguished road 
Solved Threads: 18
cgyrob's Avatar
cgyrob cgyrob is offline Offline
Junior Poster

Re: counting possible values of one column

 
0
  #2
Jul 21st, 2009
do you mean something like this

  1. SELECT field2,count(Field1)
  2. FROM Table
  3. GROUP BY field2
Last edited by cgyrob; Jul 21st, 2009 at 2:14 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 119
Reputation: cgyrob is on a distinguished road 
Solved Threads: 18
cgyrob's Avatar
cgyrob cgyrob is offline Offline
Junior Poster

Re: counting possible values of one column

 
0
  #3
Jul 21st, 2009
Since you didn't give too much information I thought of a few other interpetations of what you asked.

If field1 is a numeric value you might want the sum of the field grouped by field2

  1. SELECT field2, sum(field1)
  2. FROM Table
  3. GROUP BY field2

or you might want to know how many records of field1 there are that are also rouped by field2

  1. SELECT field2, field1, count(*)
  2. FROM table
  3. GROUP BY field2, field1

I'm sure one of the 3 syntax's will work for what you are asking.
Last edited by cgyrob; Jul 21st, 2009 at 3:54 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 52
Reputation: hashinclude is an unknown quantity at this point 
Solved Threads: 0
hashinclude hashinclude is offline Offline
Junior Poster in Training

Re: counting possible values of one column

 
0
  #4
Jul 21st, 2009
Thank you for replying. let me explain with an example
let's say i have this table: (check attachment please)
i want to get a count for example like this :
CASE1:
cars|---------------|color red
bmw|---------------------------|1
audi|----------------------------|0
ford|-----------------------------| 1
toyota|---------------------------|0
OR CASE2:
cars|---------------|car count
bmw|----------------------------|3
audi|-----------------------------|2
ford|------------------------------| 1
toyota|--------------------------|1
in the first case the count returns possible values (cars) based on a condition (red cars)
Or in the second case the count returns how the number of different values assigned to the column "car" (as in how many is bmw and how many is audi...etc)

can this be done with a single SELECT query ?
Attached Thumbnails
untitled.jpg  
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 119
Reputation: cgyrob is on a distinguished road 
Solved Threads: 18
cgyrob's Avatar
cgyrob cgyrob is offline Offline
Junior Poster

Re: counting possible values of one column

 
0
  #5
Jul 21st, 2009
Nevermind, I take it the attachment is the table schema.

Is it important to do it all in one query. What are you using the resultset for because if you do it in one query you will most likely get back something like the following.

BMW 2 RED 1
BMW 2 BLACK 1
AUDI 8 RED 3
AUDI 8 BLACK 2
AUDI 8 BLUE 3
etc....
Last edited by cgyrob; Jul 21st, 2009 at 7:14 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 52
Reputation: hashinclude is an unknown quantity at this point 
Solved Threads: 0
hashinclude hashinclude is offline Offline
Junior Poster in Training

Re: counting possible values of one column

 
0
  #6
Jul 21st, 2009
Originally Posted by cgyrob View Post
What is the table schema?
see attachment
Attached Thumbnails
untitled.jpg  
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 119
Reputation: cgyrob is on a distinguished road 
Solved Threads: 18
cgyrob's Avatar
cgyrob cgyrob is offline Offline
Junior Poster

Re: counting possible values of one column

 
1
  #7
Jul 21st, 2009
Try this

  1. SELECT MAKE.MAKE, MAKE.count, color.Color, color.count
  2. FROM(
  3. SELECT MAKE, count(*)count
  4. FROM cars
  5. GROUP BY MAKE
  6. )MAKE,
  7. ( SELECT MAKE, color, count(*)count
  8. FROM cars
  9. GROUP BY MAKE,color
  10. )color
  11. WHERE make.make = color.make
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 52
Reputation: hashinclude is an unknown quantity at this point 
Solved Threads: 0
hashinclude hashinclude is offline Offline
Junior Poster in Training

Re: counting possible values of one column

 
0
  #8
Jul 22nd, 2009
Originally Posted by cgyrob View Post
Try this

  1. SELECT MAKE.MAKE, MAKE.count, color.Color, color.count
  2. FROM(
  3. SELECT MAKE, count(*)count
  4. FROM cars
  5. GROUP BY MAKE
  6. )MAKE,
  7. ( SELECT MAKE, color, count(*)count
  8. FROM cars
  9. GROUP BY MAKE,color
  10. )color
  11. WHERE make.make = color.make
sorry for the late reply....
although your code didn't quite work.. it did direct me to the right way in solving my problem so thanks you my friend .

problem solved.
added to your rep.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 119
Reputation: cgyrob is on a distinguished road 
Solved Threads: 18
cgyrob's Avatar
cgyrob cgyrob is offline Offline
Junior Poster

Re: counting possible values of one column

 
0
  #9
Jul 23rd, 2009
Im glad i could atleast point you in the right direction. good luck with the rest.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC