Retrieving Alternating Values I have a column with two values in my database, I want to retrieve the two values alternately, for example I have:

name   itemcode
----------
 A       1
 B       2
 C       2
 D       2
 E       1
 F       1


I want to display them as:

name itemcode
----------
A      1
B      2
E      1
C      2
F      1
D      2
Here's a solution for SQL Server. This does work if you have itemcode 3, 4, etc. and not just 1 and 2.

CREATE TABLE [dbo].[Test]
(
    name char(1) NOT NULL,
    itemcode tinyint NOT NULL
);

INSERT INTO [dbo].[Test](name, itemcode)
    VALUES ('A', 1), ('B', 2), ('C', 2), ('D', 2), ('E', 1), ('F', 1);


SELECT
    t.name,
    t.itemcode
    FROM
    (
        SELECT
            name,
            itemcode,
            ROW_NUMBER() OVER(PARTITION BY itemcode ORDER BY name) AS RowNum
            FROM [dbo].[Test]
    ) t
    ORDER BY
        t.RowNum,
        t.itemcode;