Solved

Moving data from one table structure to different table structure

Posted on 2015-01-29
2
80 Views
Last Modified: 2015-02-02
Hi,

We currently have the following table with the following columns:

AnimalID OrderNo, TestA TestA_Result TestB TestB_Result TestC TestC_Result

I would like to be able to move the data to a table that has one row per test, as opposed to the current one where there is one row per animal
 Like so

AnimalID OrderNo   TestA        testAResult
12345        09890      TestA        Neg
12345        09890      TestC        Pos
12345        09890      TestC       IC

All fields are VARCHAR or CHAR

Any assistance would be appreciated

Thanks
0
Comment
Question by:Morpheus7
2 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40576981
Select AnimalID, OrderNo, TestA, TestA_Result
From myTable
UNION ALL
Select AnimalID, OrderNo, TestB, TestB_Result
From myTable
UNION ALL
Select AnimalID, OrderNo, TestC, TestC_Result
From myTable
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 40577730
SELECT at.AnimalID, at.OrderNo,
    CASE whichTest
        WHEN 'A' THEN TestA
        WHEN 'B' THEN TestB
        WHEN 'C' THEN TestC
    END AS Test,
    CASE whichTest
        WHEN 'A' THEN TestA_Result
        WHEN 'B' THEN TestB_Result
        WHEN 'C' THEN TestC_Result
    END AS TestResult
FROM animal_tests at
CROSS JOIN (
    SELECT 'A' AS whichTest UNION ALL
    SELECT 'B' UNION ALL
    SELECT 'C'
) AS whichTests
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

789 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