Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 288
  • Last Modified:

how can i count values between min and max out from a table?

hello experts,
i have the following table:
table_prod
id : integer
date: timestamp
weight: integer.
paid : boolean.

i need a query that will give me an answer as following :
1-19 | 20-29| 30-39|
25 | 57 | 74|


here i can read that records containing between 1-19 appear 25 times in my table,
the next array between 20 and 29 appear 57 times....etc.

question:
what would be the query?
thank in advance
0
toshi_
Asked:
toshi_
  • 3
  • 2
1 Solution
 
toshi_Author Commented:
my starting point is as follow,
i need an answer for diffrerent values :


SELECT count(*)
FROM trash_prod
WHERE weight BETWEEN 2  AND 19;

0
 
mahomeCommented:

SELECT 
  case when weight between 0 and 19 then '0-19' 
  case when weight between 20 and 29 then '20-29' 
  case when weight between 30 and 39 then '30-39' 
  else 'other' 
  end as weightRange, count(*)  
FROM trash_prod
GROUP BY 1
 

Open in new window

0
 
toshi_Author Commented:
if i try it, i receive the following error
------------------------------------------------------------

ERROR:  syntax error at or near "case"
LINE 7:   case when weight between 21 and 40 then '20-29'
          ^

********** Error **********

ERROR: syntax error at or near "case"
SQL state: 42601
Character: 147



0
 
toshi_Author Commented:
hooo,
i found the error , i only need one case ;o)
thanks a lot
0
 
mahomeCommented:
Oh sorry. Copy and paste error. Yes only one CASE.
http://www.postgresql.org/docs/7.1/static/functions-conditional.html
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now