If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > PostgreSQL > Count occurences

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-19-11, 11:16
krontrex krontrex is offline
Registered User
 
Join Date: Sep 2010
Posts: 37
Count occurences

Dear all,
I need to find a number of occurrences of certain value in table. The way to find this number is:
Code:
select COUNT(val) from my_table GROUP BY "val";
but my problem is when I want to get this number together with the values of other columns in the row for example
Code:
select ID, COUNT(val) from my_table GROUP BY "val";
I get an error that ID is not included in GROUP BY statement. How to handle this
Reply With Quote
  #2 (permalink)  
Old 09-19-11, 12:39
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,540
Quote:
Originally Posted by krontrex View Post
How to handle this
use one query for the counts, and a separate query for the details
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 09-19-11, 12:41
krontrex krontrex is offline
Registered User
 
Join Date: Sep 2010
Posts: 37
Could you please write me an example.
Reply With Quote
  #4 (permalink)  
Old 09-19-11, 14:02
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,540
which values?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 09-24-11, 05:52
arvindps arvindps is offline
Registered User
 
Join Date: Aug 2011
Posts: 27
please quote with example , sample data.

It is possible if ID is in a master table and you can refere that ID to the transaction table for a count or sum or average or any other aggregate function.

It can also work if you have ID and val in the same table with the following SQL

select ID,count(val) from my_table group by ID
Reply With Quote
  #6 (permalink)  
Old 09-24-11, 07:21
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,408
Quote:
Originally Posted by krontrex View Post
Code:
select COUNT(val) from my_table GROUP BY "val";
This statement does not make sense to me. What you would get is something like: 2,6,8
What do you do with that information?

Quote:
select ID, COUNT(val) from my_table GROUP BY "val";
What exactly do you want to count there?
How many distinct values val has for a certain ID?

You need to show us some sample data (ideally as INSERT INTO ...) and the desired output based on that sample data.


Although I do not really understand your question, the following might(!) be what you are looking for:
Code:
SELECT id,
      val
      count(*) over (partition by val) as counter
FROM my_table
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 On
Pingbacks are On
Refbacks are On