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;
Related
I have a table that keeps track of transactions.
The table is setup as:
transactions:
id, account_id, budget_id, points, type
I need to return each budget_id's sum of points where type = 'allocation' and sum of points where type = 'issue'
I know how to do each, but not both in one query.
expected result set:
budget_id allocated issued
434 200000 100
242 100000 5020
621 45000 3940
SELECT budget_id,
SUM(IF(type = 'allocation', points, 0)) AS allocated,
SUM(IF(type = 'issue', points, 0)) AS issued
FROM transactions
GROUP BY budget_id
select budget_ID,
sum(case when type = 'allocated' then points else 0 end) as allocated,
sum(case when type = 'issued' then points else 0 end) as issued
..rest of your query...
group by budget_ID
Cases can be used to sum only when a certain criteria is met.
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.
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)
I have a table that keeps track of transactions.
The table is setup as:
transactions:
id, account_id, budget_id, points, type
I need to return each budget_id's sum of points where type = 'allocation' and sum of points where type = 'issue'
I know how to do each, but not both in one query.
expected result set:
budget_id allocated issued
434 200000 100
242 100000 5020
621 45000 3940
SELECT budget_id,
SUM(IF(type = 'allocation', points, 0)) AS allocated,
SUM(IF(type = 'issue', points, 0)) AS issued
FROM transactions
GROUP BY budget_id
select budget_ID,
sum(case when type = 'allocated' then points else 0 end) as allocated,
sum(case when type = 'issued' then points else 0 end) as issued
..rest of your query...
group by budget_ID
Cases can be used to sum only when a certain criteria is met.
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.