Solved

Moving data from one table structure to different table structure

Posted on 2015-01-29
2
77 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SqlDataBase 7 46
Abstract Express Replacement Software 12 22
hyperlink data type in SQL 3 23
SSRS Enable Remote Errors 4 23
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

813 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now