tags:

views:

27

answers:

2

Hi

in mysql how to write a sql like this, to get the amount of X > 20 and <20

select date, numberOfXMoreThan20,numberOfXLessThan20, otherValues

from table

group by (date, X>20 and X<20)

my way, but i think it's not good

select less20.id_date, a,b
from (select id_date,count(Duree_Attente_Avant_Abandon) as a from cnav_reporting.contact_global where Duree_Attente_Avant_Abandon>20 group by id_date) as less20, (select id_date,count(Duree_Attente_Avant_Abandon) as b from cnav_reporting.contact_global where Duree_Attente_Avant_Abandon<20 group by id_date) as more20

where

less20.id_date=more20.id_date

thanks

+1  A: 

You're probably looking for the COUNT aggregate:

SELECT COUNT(*) FROM table Where X > 20
SLaks
I would agree to just use two queries. You could get both counts in one query but that would get needlessly complicated.
Tesserex
sorry, i mean to get the count about X>20 and aslo <20, i knwo that with 2 select i can do this ,but is it possible to use just one select with group by to get this done
chun
+4  A: 
SELECT
  date,
  SUM( IF(X > 20), 1, 0 ) AS overTwenty,
  SUM( IF(X < 20), 1, 0 ) AS belowTwenty,
  otherValue
FROM `table`
GROUP BY `date`, `otherValue`
Seb
ah, that's what i'am looking for,thanks!
chun
i have a syntax error near '),1.0) ....'
chun
@Seb should be sum( if(x>20,1,0))
chun
Because you're using a dot instead of a comma :) Replace '),1.0)' by '),1,0)'
Seb
@chun my answer always had commas, not dots :)
Seb