Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Flip rows to Columns in MS Access

Posted on 2004-10-15
15
Medium Priority
?
386 Views
Last Modified: 2008-03-10
Hi,

I have data like this:

EMPLID      TITLE          NAME        TITLE2            NAME       TITLE3      NAME
001           Director       John          Sr Director      Alice        VP             Fred
002           Sr Director   Tom          VP                 Simon       NULL          NULL
003           NULL            NULL         Director          John         VP             Simon

and I need the table to be converted into this:

EMPLID         DIRECTOR     SR DIRECTOR    VP
001              John              Alice                 Fred              
002                                  Tom                  Simon
003              John                                       Simon

What sort of algorithm in SQL can I use to do this?


0
Comment
Question by:deckard666
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 4
  • +1
15 Comments
 
LVL 15

Expert Comment

by:will_scarlet7
ID: 12320581
Do you mean that your table currently looks like this:

EMPLID      TITLE          NAME
001           Director       John
001           Sr Director   Alice
001           VP               Fred
002           Sr Director   Tom
002           VP               Simon
003           Director       John
003           VP               Simon

Or do your multible "Name" fields actually have different names?

0
 
LVL 15

Expert Comment

by:will_scarlet7
ID: 12320595
Sorry that should have said:

>> Or do your multiple "Name" fields actually have different names?
0
 

Author Comment

by:deckard666
ID: 12320631
Sorry, it has multiple name fields.

Like this:

EMPLID      TITLE          NAME        TITLE2           NAME2       TITLE3     NAME3
001           Director       John          Sr Director      Alice        VP             Fred
002           Sr Director   Tom          VP                 Simon       NULL          NULL
003           NULL            NULL         Director          John         VP             Simon



0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 51

Expert Comment

by:Gustav Brock
ID: 12320657
Try this:

  SELECT
    EMPLID,
    IIf(TITLE='Director', NAME1, Null) AS DIRECTOR,
    IIf(TITLE='Sr Director', NAME2, Null) AS SR_DIRECTOR,
    IIf(TITLE='VP', NAME3, Null) AS VP
  FROM
    tblYourTable;

/gustav
0
 
LVL 15

Accepted Solution

by:
will_scarlet7 earned 2000 total points
ID: 12320715
Modification on the above:

  SELECT
    EMPLID,
    IIf(TITLE='Director', NAME, IIf(TITLE2='Director', NAME2, IIf(TITLE3='Director', NAME3, Null))) AS DIRECTOR,
    IIf(TITLE='Sr Director', NAME, IIf(TITLE2='Sr Director', NAME2, IIf(TITLE3='Sr Director', NAME3, Null))) AS SR_DIRECTOR,
    IIf(TITLE='VP', NAME, IIf(TITLE2='VP', NAME2, IIf(TITLE3='VP', NAME3, Null))) AS VP
  FROM
    tblYourTable;
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 12320736
Hmmm, that was too easy:

  SELECT
    EMPLID,
    IIf(TITLE='Director', NAME, IIf(TITLE2='Director', NAME2, IIf(TITLE3='Director', NAME3, Null)))
      AS DIRECTOR,
    IIf(TITLE='Sr Director', NAME, IIf(TITLE2='Sr Director', NAME2, IIf(TITLE3='Sr Director', NAME3, Null)))
      AS SR_DIRECTOR,
    IIf(TITLE='VP', NAME, IIf(TITLE2='VP', NAME2, IIf(TITLE3='VP', NAME3, Null)))
      AS VP
  FROM
    tblYourTable;

/gustav
0
 
LVL 6

Expert Comment

by:mcorrente
ID: 12320737
You'll have to add to the query every time you add a different title if you do it like that.
0
 
LVL 6

Expert Comment

by:mcorrente
ID: 12320760
Try this... create two queries... the first is a union query:

SELECT YourTable.EmplID, YourTable.Title, YourTable.Name FROM YourTable WHERE (((YourTable.Title)>""))
UNION SELECT YourTable.EmplID, YourTable.Title2, YourTable.Name2 FROM YourTable WHERE (((YourTable.Title2)>""))
UNION SELECT YourTable.EmplID, YourTable.Title3, YourTable.Name3 FROM YourTable WHERE (((YourTable.Title3)>""));

0
 
LVL 6

Expert Comment

by:mcorrente
ID: 12320767
The second is a crosstab:

TRANSFORM First(FirstQuery.Name) AS FirstOfName
SELECT FirstQuery.EmplID
FROM FirstQuery
GROUP BY FirstQuery.EmplID
PIVOT FirstQuery.Title;
0
 
LVL 6

Expert Comment

by:mcorrente
ID: 12320776
then when you run the second query, it will create a column for every different title listed in your table.
0
 

Author Comment

by:deckard666
ID: 12321099
Thanks. First answer did it fine.
0
 
LVL 15

Expert Comment

by:will_scarlet7
ID: 12321164
Glad I could help!
God bless!
Sam

All fine with you Gustav (since I was modifying your post)?
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 12321792
No problem. I guess God left my side for a moment ...

Have a nice and quiet weekend all of you!

/gustav
0
 
LVL 15

Expert Comment

by:will_scarlet7
ID: 12324000
Gustav,
I'm sure you know this, but God nevel leaves our side. He just spreads the blessings around :)
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 12331476
OK, in that case he was just very modest with my dose! But as I said, no problem, I can handle it.

/gustav
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question