The easy way would be to use a cursor: create a temporary table, open a
cursor on your table Members and then insert each line in table Members into
the temporary table, alternating the value of ID. Of course, you could
also add this ID field directly to the table Members and update it using the
cursor. On the Internet, you will find numerous examples of using a cursor
in T-SQL. Check also the BOL (OnLine Help for SQL-Server) as well as the
m.p.sqlserver.programming newsgroup.
The second way would be to use a subquery to compute the position of each
member and use the Modulo 2 of this value to divide your members; for
example:
Set NoCount ON
create table #Members (IdMember int Identity (1,1) primary key, firstname
varchar(50), phone varchar (10))
create table #Number (IdNumber int primary key, groupe char (1))
insert into #Members (firstname, phone) values ('deny', '07334')
insert into #Members (firstname, phone) values ('ben', '76865')
insert into #Members (firstname, phone) values ('all', '2435')
insert into #Members (firstname, phone) values ('ren', '736456')
insert into #Members (firstname, phone) values ('bony', '54546')
insert into #Number (IdNumber, groupe) values ('1', 'A')
insert into #Number (IdNumber, groupe) values ('2', 'B')
-- These three queries show first the ranking of each member using a
subquery (starting from 0),
-- then the result of Modulo 2
-- and finally the Join to give you the searched result:
select *, (Select Count (*) from #Members M2 where M2.IdMember < M.IdMember)
from #Members as M
select *, (Select Count (*) from #Members M2 where M2.IdMember < M.IdMember)
% 2 from #Members as M
select * from #Members M Inner Join #Number N on ((1 + ((Select Count (*)
from #Members M2 where M2.IdMember < M.IdMember) % 2)) = N.IdNumber)
Order by M.IdMember
drop table #Members
drop table #Number