EDIT: the responses work well but not quite what i was after i dont think i explained it very well, i just want to return 1 result per function so count how many odd numbers are in the result and count how many even numbers are in the result?
sql fiddle for the data
http://sqlfiddle.com/#!9/dfbfa7/1
Im trying to count even and odd numbers by splitting the the results using 2 functions but my results dont match up to the results in the mysql table.
data
select number from table group by number
results
1,4,6,7,8,9,11,12
function to return odd number count
SELECT
COUNT(CASE WHEN (number% 2) > 0 THEN number ELSE NULL END) as odd
FROM test.table
WHERE date = CURDATE() and time > now() - interval 60 second group by number
LIMIT 1
and the function to return even number count
SELECT
COUNT(CASE WHEN (number% 2) = 0 THEN number ELSE NULL END) as even
FROM test.table
WHERE date = CURDATE() and time > now() - interval 60 second group by number
LIMIT 1
i was hoping the results would be
odd = 4
even = 4
but instead im getting
odd = 8
even = 0
where am i going wrong?
Maybe try something like thise.
For even numbers:
COUNT(SELECT * FROM `table` WHERE mod(number, 2)=0);
For odd numbers:
COUNT(SELECT * FROM `table` WHERE mod(number, 2)>0);
This will return the only number of odd and even numbers, then collect those numbers with php.
COUNT will add up all the lines it traverses
In your case you must use SUM:
SUM(CASE WHEN (number% 2) > 0 THEN 1 ELSE 0 END) as odd
SUM(CASE WHEN (number% 2) = 0 THEN 1 ELSE 0 END) as even
for simple operations the IF is much cleaner than the CASE:
SUM(IF(number % 2 > 0, 1, 0)) as odd
SUM(IF(number% 2 = 0, 1, 0)) as even
The reason your query is returning wrong result is probably because you are using limit 1 try removing it also if you have to use group by use group by number in before you do calculations for this query, i have altered the query here for example.
SELECT
count(CASE WHEN (a.number% 2) = 0 THEN a.number ELSE NULL END) as even,
count(CASE WHEN (a.number% 2) <> 0 THEN a.number ELSE NULL END) as odd
FROM (select number from test group by number) a;
http://sqlfiddle.com/#!9/dfbfa7/22
for the set up you have created im getting 5 even and 5 odd in results, may be you have to recheck the condition you have in where clause if your results are different with actual data.
Related
I have this table:
And I need:
Where:
Complete have all records with same _legajo and _count > 0
Zero have all records with same_legajo and _count = 0
Track have all records with same _legajo, some records with _count 0 and some with _count >
My table is 20k records
I working on PHP and MySQL
some ideas?
You could first select MIN and MAX for each _legajo:
SELECT _legajo, MIN(_count), MAX(_count)
FROM signups
GROUP BY _legajo;
If MAX is zero, then it naturally belongs to the zero category. If MIN is larger than zero then it belongs to complete category (MAX is naturally larger than zero then). If MIN is zero and MAX isn't, then it's in track.
Then you can just use SUM to determine the counts with the conditions:
SELECT
SUM(CASE WHEN min>0 THEN 1 ELSE 0 END) as complete,
SUM(CASE WHEN max=0 THEN 1 ELSE 0 END) as zero,
SUM(CASE WHEN min=0 AND max>0 THEN 1 ELSE 0 END) as track
FROM (SELECT _legajo, MIN(_count), MAX(_count)
FROM signups
GROUP BY _legajo) a;
I have the following sql-statement that I want to transform into doctrine query builder. The goal is to count how many ratings exist with rating value 1 and with rating value 2.
SELECT
COUNT(CASE WHEN rating.rating = 1 THEN rating.rating END) as rat1,
COUNT(CASE WHEN rating.rating = 2 THEN rating.rating END) as rat2
FROM rating
This sql statement is working fine - but when I try to transform it into a Doctrine statement, it does not anymore. When nothing should get counted (because no ratings for this value exist), it returns me a "1" instead of a 0. How can I tell doctrine to simply return a zero when there is nothing to count?
I tried it by removing the "ELSE 0" , but then I get an error that this part is required..
return $qb
->addSelect('COUNT(CASE WHEN r.rating = 1 THEN r.rating ELSE 0 END) as rat_1')
->addSelect('COUNT(CASE WHEN r.rating = 2 THEN r.rating ELSE 0 END) as rat_2')
->getQuery()
->getResult();
Regards,
"sum" is not required - example:
votings 2,2,2,2,2 should return 5 , because the rating with value 2 got voted 5 times.
To count distinct id's in one column depending on the value in another column the answer from Fuzzy Tree does not work in this case.
To count only the distinct values you should give null as a parameter and set it to null like:
->addSelect('COUNT(DISTINCT(CASE WHEN r.rating = 1 THEN rating.rating ELSE :nada END)) as rat_1')
->setParameter(':nada', null)
As vkp mentioned, you can use SUM but instead of summing the rating, sum either a 1 or a 0 to simulate a COUNT
SUM(CASE WHEN r.rating = 1 THEN 1 ELSE 0 END)
Hi I have mysql table named content where i have a column "status" which have 3 values, converted, negotiating and received. now i want to count how many have status received, negotiating, and converted for developing a chart.
here is what i used:
SELECT status,
SUM(CASE WHEN status = 'converted' = 1 THEN 1 ELSE 0 END) AS converted,
SUM(CASE WHEN status = 'negotiating' = 1 THEN 1 ELSE 0 END) AS negotiating,
SUM(CASE WHEN status = 'Received NA' = 1 THEN 1 ELSE 0 END) AS ReceivedNA
FROM content GROUP BY status;
It shows me the result but in a way that i can not use it.
to feed my chart i used this:
$data = array(
array('converted', $converted),
array('negotiating', $negotiating),
array('received', $received)
);
So i guess some thing like this table will solve my problem:
status result
--------------------------- --------
converted 1
negotiating 5
received 4
So can anyone suggest how can modify my sql to get the expected result?
thanks again
Use GROUP By. Try this -
SELECT status, count(status) result FROM content GROUP BY status
To get the distinct count use GROUP BY.
select status,count(1) as result from content GROUP BY status;
Instead of using sum, count is always a better and easier way
EDIT-to answer the comment
The parameter to the COUNT function is an expression that is to be evaluated for each row. The COUNT function returns the number of rows for which the expression evaluates to a non-null value. ( * is a special expression that is not evaluated, it simply returns the number of rows.)
There are two additional modifiers for the expression: ALL and DISTINCT. These determine whether duplicates are discarded. Since ALL is the default, your example is the same as count(ALL 1), which means that duplicates are retained.
Since the expression "1" evaluates to non-null for every row, and since you are not removing duplicates, COUNT(1) should always return the same number as COUNT(*).
Will this work for you?
SELECT status, count(status) FROM content GROUP BY status;
It confuses me.. total left calculation seems doesn't work.
I am trying to get the total voucher and get total used and the total left.
please help.
SELECT
(IFNULL(SUM(value), 0)) AS total_voucher,
(
SELECT
IFNULL(SUM(value), 0))
FROM
voucher_history
WHERE
idUser = 1 AND isUsed = 1 AND DATE(FROM_UNIXTIME(datetime)) = '2014-03-04'
) AS total_used,
(total_voucher-total_used) AS total_left
FROM
voucher_history
WHERE
idUser = 1 AND isUsed = 0 AND DATE(FROM_UNIXTIME(datetime)) <= '2014-03-05'
You can do this with conditional aggregation, rather than using a subquery:
SELECT coalesce(SUM(value), 0)) AS total_voucher,
sum(case when is_used = 1 then value else 0 end) as total_used,
sum(case when is_used = 1 then 0 else value end) as total_left
FROM voucher_history
WHERE idUser = 1 AND DATE(FROM_UNIXTIME(datetime)) <= '2014-03-05';
Your query has the problem that it is trying to use column aliases (total_voucher and total_used) in the same select statement. SQL does not support this. You would need to use a subquery to get that functionality.
Select * FROM table WHERE a<=9 AND a>=4 AND b<=20 AND b>=16 AND c<=30 AND c>=26
Now I want to end up selecting from query 1 the results in which at least two of the following criteria are met.
Select * FROM table WHERE a<=7 AND a>=5 AND B<=19 AND B>=17 AND C<=29 AND c>=27
Numbers could be anything, although on query 2 these are lower for the less than eqaul and higher for the bigger than equal.
I want to be able to set the number of minimum coincidences to be met at query 2 with a number.
For example 5 coincidences must be met at query 2 to end up making a selection in query 1.
This is about range and matches count, if there is an easier way to achieve this great.
i think this kind of query will do your job
Select
*,
(IF(a<=7 AND a>=5,1,0) + IF(B<=19 AND B>=17,1,0) + IF(C<=29 AND c>=27,1,0))
as totalmatch
FROM table
HAVING totalmatch >=2
Try to use native MySQL function BEETWEN:
SELECT * FROM t1 WHERE key_col BETWEEN '2' AND '3';
Think it is something like this you want for the second query:-
Select *, CASE WHEN a<=7 THEN 1 ELSE 0 END + CASE WHEN a>=5 THEN 1 ELSE 0 END + CASE WHEN B<=19 THEN 1 ELSE 0 END + CASE WHEN B>=17 THEN 1 ELSE 0 END + CASE WHEN C<=29 THEN 1 ELSE 0 END + CASE WHEN c>=27 THEN 1 ELSE 0 END AS MatchCount
FROM table
WHERE a<=7
OR a>=5
OR B<=19
OR B>=17
OR C<=29
OR c>=27
HAVING MatchCount >= 2
Probably using the first query as a subselect instead of table.
EDIT - reading your latest response I think I misunderstood your requirement. However you could possibly find the differences between each values, get the absolute value of each difference and add them together to see how close / far the differences are.