What is being counted (*)% 2 = 1

I see a request like

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

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

Can anyone explain?

edit: What are the common uses?

+6
tsql
source share
8 answers

Well% is the modulo operator, which gives the remainder of the division, so it will give 0 when the number is exactly divided by 2 (even) and 1, if not (for example, odd). Thus, the query basically selects elements for which the counter is odd (as mentioned above).

+21
source share

If you had an odd number of entries for each step?

+15
source share

It will return all steps with an odd number of rows.

+4
source share

just check it out

 declare @t1 table (step char(1)) insert into @t1(step) select 'a' union all select 'b' union all select 'b' union all select 'c' union all select 'c' union all select 'c' union all select 'd' union all select 'd' union all select 'd' union all select 'd' select * from @t1 group by step having count(*)%2 = 1 

which will return the column values ​​that exist, add the number of times

in this example it will return

 'a' 'c' 

select * gets confused here, and I'd rather write it as

 select step from @t1 group by step having count(*)%2 = 1 

or even for greater visibility

 select step, count(*) from @t1 group by step having count(*)%2 = 1 
+2
source share

COUNT (*) will read all rows in the database. The% symbol is a module symbol that will give you the rest of the separation problem. So this is dividing all the lines into two and returning those that have 1 left (which means an odd number of lines).

As Eric noted, these will not be all lines, but rather those that are grouped by step, which means that these are all odd lines for each step.

+1
source share

The reason for this:

Suppose you want to divide the odd and even entries into two columns. You could use one for one of them and odd for the other.

I also put this in a comment, but received no response.

+1
source share

It is impossible to answer your question without knowing what tables are used for.

For this “step”, it may happen that you need to have an equal amount of “something”, and this will lead to a list of elements that will be displayed in some interface, where it is not.

Example: Let's forget the “Steps” for a moment and assume that it was a table of students, and that the “Step” was instead of the “Groups” in which the students participated. The requirement of the group is that there is an even number of students, because the students will work in pairs. For an administrative tool, you can write such a query to see a list of groups where this is not true.

Group: Count A, 10 B, 9 C, 17 D, 8 E, 4 F, 5

And the request will return groups B, C, F

+1
source share

Thanks to everyone. All of you said that the query returns grouped strings with an odd number.

but that is not the point! I will continue to test this case and write the reason in the mind of the programmer (if I find who wrote it)

Lessons learned: programmers should write comments about such stupid logic ...

0
source share

All Articles