0

I have a table called Dateinfo and the data looks like below:

Cntr_Date   Appv_Date   Prop_Date     ID    Lvl
------------------------------------------------
2014-04-11  2014-03-21  2014-07-29    4867  1
2014-04-21  2014-04-29  2014-04-21    4867  1a

I want my output to look like this:

ID      Lvl    DT_Type      Dates_Hist
---------------------------------------
4867    1      Cntr         2014-04-11
4867    1      Appv         2014-03-21
4867    1      Prop         2014-07-29
4867    1a     Cntr         2014-04-21
4867    1a     Appv         2014-04-29
4867    1a     Prop         2014-04-21  
1
  • Where is the DT_Type and Dates_Hist columns coming from? Are they part of the same table? Commented Aug 13, 2014 at 19:18

2 Answers 2

2

Test Data

DECLARE @TABLE 
   TABLE(Cntr_Date DATE, Appv_Date DATE, Prop_Date DATE, ID INT, Lvl VARCHAR(10))

INSERT INTO @TABLE VALUES 
('2014-04-11','2014-03-21','2014-07-29',4867,'1'),
('2014-04-21','2014-04-29','2014-04-21',4867,'1a')

Query

SELECT  ID
      , Lvl
      , LEFT(DT_Type, CHARINDEX('_',DT_Type)-1) AS DT_Type 
      , Date_Hist
FROM @TABLE
 UNPIVOT ( Date_Hist FOR 
           DT_Type IN (Cntr_Date, Appv_Date, Prop_Date) 
         ) UP

Result

╔══════╦═════╦═════════╦════════════╗
║  ID  ║ Lvl ║ DT_Type ║ Date_Hist  ║
╠══════╬═════╬═════════╬════════════╣
║ 4867 ║ 1   ║ Cntr    ║ 2014-04-11 ║
║ 4867 ║ 1   ║ Appv    ║ 2014-03-21 ║
║ 4867 ║ 1   ║ Prop    ║ 2014-07-29 ║
║ 4867 ║ 1a  ║ Cntr    ║ 2014-04-21 ║
║ 4867 ║ 1a  ║ Appv    ║ 2014-04-29 ║
║ 4867 ║ 1a  ║ Prop    ║ 2014-04-21 ║
╚══════╩═════╩═════════╩════════════╝

Update

For column names with or without Underscore you can hardcode the values inside a case statement, something as follows

SELECT  ID
      , Lvl
      , CASE DT_Type
          WHEN 'Cntr_Date' THEN 'Cntr' 
          WHEN 'Appv_Date' THEN 'Appv'
          WHEN 'Prop_Date' THEN 'Prop'
         END AS DT_Type
      , Date_Hist
FROM @TABLE
 UNPIVOT ( Date_Hist FOR 
           DT_Type IN (Cntr_Date, Appv_Date, Prop_Date) 
         ) UP
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you Ali Just one question what can we do to have custom value or name in DT_type like in above example i want cntr_date to be cntr, appv_date to be appv
as long as the format is same it works but if were to give alias it doesn't work right?
I have just done exactly what you asked for, what alias you are asking about?
Op is saying that even though in this case the values for DT_Type are the ones s/he wants, it's only because the column names have a specific format, but s/he couldn't give just any value to those if s/he wanted
@peter Alright ok, well as long as a column name has an undersocre it will pull the first Part of the column names before underscore, with this specific query.
|
2

Another option aside of UNPIVOT and using UNION ALL is CROSS APPLY:

SELECT  t.id,
        t.Lvl,
        x.*
FROM YourTable t
CROSS APPLY 
(
    VALUES
        ('Cntr', t.Cntr_Date),
        ('Appv', t.Appv_Date),
        ('Prop', t.Prop_Date)
) x (DT_Type, Dates_Hist);

Here is a sqlfiddle with a demo.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.