Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is count(*) % 2 = 1

Tags:

tsql

I see a query like

select *
from Table1
group by Step
having count(*) % 2 = 1

What is the trick about having count(*) % 2 = 1

Can anyone explain?

edit: What are the common usage areas?

like image 951
Ozan BAYRAM Avatar asked Jan 06 '10 14:01

Ozan BAYRAM


3 Answers

Well % is the modulo operator, which gives the remainder of a division so it would give 0 when the number is exactly divisible by 2 (even) and 1 when not (e.g. it is odd). So the query basically selects elements for which count is odd (as said above).

like image 59
ternaryOperator Avatar answered Nov 26 '22 20:11

ternaryOperator


Would that not be checking if you have an odd number of entries per step?

like image 38
Reverend Gonzo Avatar answered Nov 26 '22 20:11

Reverend Gonzo


It will return all the steps which had odd number of rows.

like image 36
Nitin Midha Avatar answered Nov 26 '22 18:11

Nitin Midha