SQL for specific pattern of output?

L

Leslie Charles

The following example is a simplification to make my
question less complicated.

I have a database of 3 people: Brown, Jones, and
Smith.

Each person in the DB has 2 fields associated with
their name field: Field “A†and “Bâ€. The values in
A and B are either 0 or 1. The following SQL
string extracts a query as shown below.

SELECT tblPeople.LName,tblPeople.A,tblPeople.B FROM tblPeople

Brown 1 0
Jones 1 1
Smith 0 0

But the IT department wants the data as follows:

Brown A 1
Brown B 0
Jones A 1
Jones B 1
Smith A 0
Smith B 0

Is there a way to change the SQL string to provide the needed output?
 
J

Jerry Whittle

SELECT tblPeople.LName, "A", tblPeople.A
FROM tblPeople
UNION
SELECT tblPeople.LName, "B", tblPeople.B
FROM tblPeople
ORDER BY 1, 2 ;
 
J

John W. Vinson

The following example is a simplification to make my
question less complicated.

I have a database of 3 people: Brown, Jones, and
Smith.

Each person in the DB has 2 fields associated with
their name field: Field “A” and “B”. The values in
A and B are either 0 or 1. The following SQL
string extracts a query as shown below.

SELECT tblPeople.LName,tblPeople.A,tblPeople.B FROM tblPeople

Brown 1 0
Jones 1 1
Smith 0 0

But the IT department wants the data as follows:

Brown A 1
Brown B 0
Jones A 1
Jones B 1
Smith A 0
Smith B 0

Is there a way to change the SQL string to provide the needed output?

A UNION query will do this:

SELECT tblPeople.LName, "A" AS FromWhat, tblPeople.A FROM tblPeople
UNION ALL
SELECT tblPeople.LName, "B", tblPeople.B FROM tblPeople
ORDER BY 1, 2;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top