Transposing repeated rows of datetime into columns in SQL 17SQL Server index / performance help needed (index...

Are small insurances worth it?

Is "cogitate" used appropriately in "I cogitate that success relies on hard work"?

Draw this image in the TIKZ package

I am the light that shines in the dark

Precision notation for voltmeters

What is better: yes / no radio, or simple checkbox?

Do I need a return ticket to Canada if I'm a Japanese National?

How strong is the axiom of well-ordered choice?

Should I file my taxes? No income, unemployed, but paid 2k in student loan interest

Why do phishing e-mails use faked e-mail addresses instead of the real one?

Paper published similar to PhD thesis

How to make sure I'm assertive enough in contact with subordinates?

How to write a chaotic neutral protagonist and prevent my readers from thinking they are evil?

Why restrict private health insurance?

Is there a logarithm base for which the logarithm becomes an identity function?

PTIJ: Sport in the Torah

After Brexit, will the EU recognize British passports that are valid for more than ten years?

What does *dead* mean in *What do you mean, dead?*?

What can I do if someone tampers with my SSH public key?

Why is there an extra space when I type "ls" on the Desktop?

Giving a talk in my old university, how prominently should I tell students my salary?

Why aren't there more Gauls like Obelix?

Unidentified signals on FT8 frequencies

Why would /etc/passwd be used every time someone executes `ls -l` command?



Transposing repeated rows of datetime into columns in SQL 17


SQL Server index / performance help needed (index scan and a sort taking 40 minutes)How to speed up query on table with millions of rowsSQL Query with multiple conditions on dateGrouping and Transposing Rows to Columns in PostgreSQLTwo rows into two columnsCan I lookup my Primary Key in SQL Server from Excel Spreadsheet?Convert Rows into ColumnsTemporary Tables With Nonclustered Indexes Including All ColumnsTier Dimension based on measure valueHow to transposing rows into columns in MySQL?













0















I am working with a dataset of inpatient data in SQL version 17. The fields contain anonymized ID, admission date, discharge date and other relevant fields. Since some of the patients are readmitted, the ID field is not unique. The table looks like this:



ID       AdmDate1      DischDate    
10001 2012-10-16 2012-10-26
10001 2014-06-15 2014-06-18
10001 2014-12-21 2014-12-29
10002 2013-02-14 2013-02-20
10003 2013-01-23 2013-01-31
10004 2012-11-15 2012-11-19
10004 2014-09-26 2014-10-06
10005 2014-12-12 2014-12-23
10006 2013-10-23 2013-10-28


Since I want to calculate readmission rate and intervals between readmissions, I want to create a table like this:



ID       AdmDate1     AdmDate2    AdmDate3   DischDate1 DischDate2 DischDate3   
10001 2012-10-16 2014-06-15 2014-12-21
10002 2013-02-14 None None


Could anyone help me?
Thanks in advance.










share|improve this question
















bumped to the homepage by Community 3 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
















  • The problem for me is that you have no real amount of max adm dates + dischDates. While it is possible through some form of query, it will go really slow when data gets bigger. (looping over all the records). Your dataset returned will also get bigger and bigger horizontically, which is not ideal.

    – Randi Vertongen
    Nov 10 '18 at 20:53











  • It would be easy enough to do using a dynamic pivot... but... based on your use case, I don't think you want to. Having your rows pivoted to columns will make your analysis far more difficult than it needs to be. Use a pivot in the final display if you want but keep in rows to do the actual math.

    – Jason A. Long
    Nov 11 '18 at 3:45













  • Thank you @RandiVertongen. I have counted the rows in the columns of AdmDate1 and found the maximum admission frequency is 35. Since readmissions more than 4 are not many, I can remove the rows with rn>4 for AdmDate1 and DischDate. by doing this, the number of columns to be created will be reduced to manageable numbers.

    – Mel
    Nov 11 '18 at 5:20
















0















I am working with a dataset of inpatient data in SQL version 17. The fields contain anonymized ID, admission date, discharge date and other relevant fields. Since some of the patients are readmitted, the ID field is not unique. The table looks like this:



ID       AdmDate1      DischDate    
10001 2012-10-16 2012-10-26
10001 2014-06-15 2014-06-18
10001 2014-12-21 2014-12-29
10002 2013-02-14 2013-02-20
10003 2013-01-23 2013-01-31
10004 2012-11-15 2012-11-19
10004 2014-09-26 2014-10-06
10005 2014-12-12 2014-12-23
10006 2013-10-23 2013-10-28


Since I want to calculate readmission rate and intervals between readmissions, I want to create a table like this:



ID       AdmDate1     AdmDate2    AdmDate3   DischDate1 DischDate2 DischDate3   
10001 2012-10-16 2014-06-15 2014-12-21
10002 2013-02-14 None None


Could anyone help me?
Thanks in advance.










share|improve this question
















bumped to the homepage by Community 3 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
















  • The problem for me is that you have no real amount of max adm dates + dischDates. While it is possible through some form of query, it will go really slow when data gets bigger. (looping over all the records). Your dataset returned will also get bigger and bigger horizontically, which is not ideal.

    – Randi Vertongen
    Nov 10 '18 at 20:53











  • It would be easy enough to do using a dynamic pivot... but... based on your use case, I don't think you want to. Having your rows pivoted to columns will make your analysis far more difficult than it needs to be. Use a pivot in the final display if you want but keep in rows to do the actual math.

    – Jason A. Long
    Nov 11 '18 at 3:45













  • Thank you @RandiVertongen. I have counted the rows in the columns of AdmDate1 and found the maximum admission frequency is 35. Since readmissions more than 4 are not many, I can remove the rows with rn>4 for AdmDate1 and DischDate. by doing this, the number of columns to be created will be reduced to manageable numbers.

    – Mel
    Nov 11 '18 at 5:20














0












0








0








I am working with a dataset of inpatient data in SQL version 17. The fields contain anonymized ID, admission date, discharge date and other relevant fields. Since some of the patients are readmitted, the ID field is not unique. The table looks like this:



ID       AdmDate1      DischDate    
10001 2012-10-16 2012-10-26
10001 2014-06-15 2014-06-18
10001 2014-12-21 2014-12-29
10002 2013-02-14 2013-02-20
10003 2013-01-23 2013-01-31
10004 2012-11-15 2012-11-19
10004 2014-09-26 2014-10-06
10005 2014-12-12 2014-12-23
10006 2013-10-23 2013-10-28


Since I want to calculate readmission rate and intervals between readmissions, I want to create a table like this:



ID       AdmDate1     AdmDate2    AdmDate3   DischDate1 DischDate2 DischDate3   
10001 2012-10-16 2014-06-15 2014-12-21
10002 2013-02-14 None None


Could anyone help me?
Thanks in advance.










share|improve this question
















I am working with a dataset of inpatient data in SQL version 17. The fields contain anonymized ID, admission date, discharge date and other relevant fields. Since some of the patients are readmitted, the ID field is not unique. The table looks like this:



ID       AdmDate1      DischDate    
10001 2012-10-16 2012-10-26
10001 2014-06-15 2014-06-18
10001 2014-12-21 2014-12-29
10002 2013-02-14 2013-02-20
10003 2013-01-23 2013-01-31
10004 2012-11-15 2012-11-19
10004 2014-09-26 2014-10-06
10005 2014-12-12 2014-12-23
10006 2013-10-23 2013-10-28


Since I want to calculate readmission rate and intervals between readmissions, I want to create a table like this:



ID       AdmDate1     AdmDate2    AdmDate3   DischDate1 DischDate2 DischDate3   
10001 2012-10-16 2014-06-15 2014-12-21
10002 2013-02-14 None None


Could anyone help me?
Thanks in advance.







sql-server pivot sql-server-2017






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 '18 at 19:45









Goncalo Peres

131114




131114










asked Nov 9 '18 at 15:03









MelMel

1




1





bumped to the homepage by Community 3 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 3 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • The problem for me is that you have no real amount of max adm dates + dischDates. While it is possible through some form of query, it will go really slow when data gets bigger. (looping over all the records). Your dataset returned will also get bigger and bigger horizontically, which is not ideal.

    – Randi Vertongen
    Nov 10 '18 at 20:53











  • It would be easy enough to do using a dynamic pivot... but... based on your use case, I don't think you want to. Having your rows pivoted to columns will make your analysis far more difficult than it needs to be. Use a pivot in the final display if you want but keep in rows to do the actual math.

    – Jason A. Long
    Nov 11 '18 at 3:45













  • Thank you @RandiVertongen. I have counted the rows in the columns of AdmDate1 and found the maximum admission frequency is 35. Since readmissions more than 4 are not many, I can remove the rows with rn>4 for AdmDate1 and DischDate. by doing this, the number of columns to be created will be reduced to manageable numbers.

    – Mel
    Nov 11 '18 at 5:20



















  • The problem for me is that you have no real amount of max adm dates + dischDates. While it is possible through some form of query, it will go really slow when data gets bigger. (looping over all the records). Your dataset returned will also get bigger and bigger horizontically, which is not ideal.

    – Randi Vertongen
    Nov 10 '18 at 20:53











  • It would be easy enough to do using a dynamic pivot... but... based on your use case, I don't think you want to. Having your rows pivoted to columns will make your analysis far more difficult than it needs to be. Use a pivot in the final display if you want but keep in rows to do the actual math.

    – Jason A. Long
    Nov 11 '18 at 3:45













  • Thank you @RandiVertongen. I have counted the rows in the columns of AdmDate1 and found the maximum admission frequency is 35. Since readmissions more than 4 are not many, I can remove the rows with rn>4 for AdmDate1 and DischDate. by doing this, the number of columns to be created will be reduced to manageable numbers.

    – Mel
    Nov 11 '18 at 5:20

















The problem for me is that you have no real amount of max adm dates + dischDates. While it is possible through some form of query, it will go really slow when data gets bigger. (looping over all the records). Your dataset returned will also get bigger and bigger horizontically, which is not ideal.

– Randi Vertongen
Nov 10 '18 at 20:53





The problem for me is that you have no real amount of max adm dates + dischDates. While it is possible through some form of query, it will go really slow when data gets bigger. (looping over all the records). Your dataset returned will also get bigger and bigger horizontically, which is not ideal.

– Randi Vertongen
Nov 10 '18 at 20:53













It would be easy enough to do using a dynamic pivot... but... based on your use case, I don't think you want to. Having your rows pivoted to columns will make your analysis far more difficult than it needs to be. Use a pivot in the final display if you want but keep in rows to do the actual math.

– Jason A. Long
Nov 11 '18 at 3:45







It would be easy enough to do using a dynamic pivot... but... based on your use case, I don't think you want to. Having your rows pivoted to columns will make your analysis far more difficult than it needs to be. Use a pivot in the final display if you want but keep in rows to do the actual math.

– Jason A. Long
Nov 11 '18 at 3:45















Thank you @RandiVertongen. I have counted the rows in the columns of AdmDate1 and found the maximum admission frequency is 35. Since readmissions more than 4 are not many, I can remove the rows with rn>4 for AdmDate1 and DischDate. by doing this, the number of columns to be created will be reduced to manageable numbers.

– Mel
Nov 11 '18 at 5:20





Thank you @RandiVertongen. I have counted the rows in the columns of AdmDate1 and found the maximum admission frequency is 35. Since readmissions more than 4 are not many, I can remove the rows with rn>4 for AdmDate1 and DischDate. by doing this, the number of columns to be created will be reduced to manageable numbers.

– Mel
Nov 11 '18 at 5:20










1 Answer
1






active

oldest

votes


















0















I have counted the rows in the columns of AdmDate1 and found the
maximum admission frequency is 35. Since readmissions more than 4 are
not many, I can remove the rows with rn>4 for AdmDate1 and DischDate.
by doing this, the number of columns to be created will be reduced to
manageable numbers




Based on this reply i started looking for a solution which uses 4 of admdates and 4 of Dischdates.



So this works, but I don't dare look at the query plan



Create test data



create table AdmissionDates(id int, AdmDate1 date,  DischDate date)
insert into AdmissionDates(id,AdmDate1,DischDate)
VALUES( 10001 , '2012-10-16' , '2012-10-26'),
(10001 , '2014-06-15' , '2014-06-18') ,
(10001 , '2014-12-21' , '2014-12-29') ,
(10002 , '2013-02-14' , '2013-02-20') ,
(10003 , '2013-01-23' , '2013-01-31') ,
(10004 , '2012-11-15' , '2012-11-19') ,
(10004 , '2014-09-26' , '2014-10-06') ,
(10005 , '2014-12-12' , '2014-12-23') ,
(10006 , '2013-10-23' , '2013-10-28')

select id,row_number() over( partition by id order by admDate1 asc) as rownum, AdmDate1,row_number() over( partition by id order by DischDate asc) as rownum2, DischDate
into #temp
from AdmissionDates;


The Query



select DISTINCT id, 
t2.admDate1 as admDate1,
t3.admDate1 as admDate2,
t4.admDate1 as admDate3,
t5.admDate1 as admDate4,
T6.DischDate as DischDate1,
T7.DischDate as DischDate2,
T8.DischDate as DischDate3,
T9.DischDate as DischDate4
FROM
#temp t1
OUTER APPLY
(select admDate1 from #temp T2 where rownum = 1 and T2.id = T1.id) as T2
OUTER APPLY
(select admDate1 from #temp T2 where rownum = 2 and T2.id = T1.id) as T3
OUTER APPLY
(select admDate1 from #temp T2 where rownum = 3 and T2.id = T1.id) as T4
OUTER APPLY
(select admDate1 from #temp T2 where rownum = 4 and T2.id = T1.id) as T5
OUTER APPLY
(select DischDate from #temp T2 where rownum2 = 1 and T2.id = T1.id) as T6
OUTER APPLY
(select DischDate from #temp T2 where rownum2 = 2 and T2.id = T1.id) as T7
OUTER APPLY
(select DischDate from #temp T2 where rownum2 = 3 and T2.id = T1.id) as T8
OUTER APPLY
(select DischDate from #temp T2 where rownum2 = 4 and T2.id = T1.id) as T9;


DROP TABLE #temp;





share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "182"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f222195%2ftransposing-repeated-rows-of-datetime-into-columns-in-sql-17%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0















    I have counted the rows in the columns of AdmDate1 and found the
    maximum admission frequency is 35. Since readmissions more than 4 are
    not many, I can remove the rows with rn>4 for AdmDate1 and DischDate.
    by doing this, the number of columns to be created will be reduced to
    manageable numbers




    Based on this reply i started looking for a solution which uses 4 of admdates and 4 of Dischdates.



    So this works, but I don't dare look at the query plan



    Create test data



    create table AdmissionDates(id int, AdmDate1 date,  DischDate date)
    insert into AdmissionDates(id,AdmDate1,DischDate)
    VALUES( 10001 , '2012-10-16' , '2012-10-26'),
    (10001 , '2014-06-15' , '2014-06-18') ,
    (10001 , '2014-12-21' , '2014-12-29') ,
    (10002 , '2013-02-14' , '2013-02-20') ,
    (10003 , '2013-01-23' , '2013-01-31') ,
    (10004 , '2012-11-15' , '2012-11-19') ,
    (10004 , '2014-09-26' , '2014-10-06') ,
    (10005 , '2014-12-12' , '2014-12-23') ,
    (10006 , '2013-10-23' , '2013-10-28')

    select id,row_number() over( partition by id order by admDate1 asc) as rownum, AdmDate1,row_number() over( partition by id order by DischDate asc) as rownum2, DischDate
    into #temp
    from AdmissionDates;


    The Query



    select DISTINCT id, 
    t2.admDate1 as admDate1,
    t3.admDate1 as admDate2,
    t4.admDate1 as admDate3,
    t5.admDate1 as admDate4,
    T6.DischDate as DischDate1,
    T7.DischDate as DischDate2,
    T8.DischDate as DischDate3,
    T9.DischDate as DischDate4
    FROM
    #temp t1
    OUTER APPLY
    (select admDate1 from #temp T2 where rownum = 1 and T2.id = T1.id) as T2
    OUTER APPLY
    (select admDate1 from #temp T2 where rownum = 2 and T2.id = T1.id) as T3
    OUTER APPLY
    (select admDate1 from #temp T2 where rownum = 3 and T2.id = T1.id) as T4
    OUTER APPLY
    (select admDate1 from #temp T2 where rownum = 4 and T2.id = T1.id) as T5
    OUTER APPLY
    (select DischDate from #temp T2 where rownum2 = 1 and T2.id = T1.id) as T6
    OUTER APPLY
    (select DischDate from #temp T2 where rownum2 = 2 and T2.id = T1.id) as T7
    OUTER APPLY
    (select DischDate from #temp T2 where rownum2 = 3 and T2.id = T1.id) as T8
    OUTER APPLY
    (select DischDate from #temp T2 where rownum2 = 4 and T2.id = T1.id) as T9;


    DROP TABLE #temp;





    share|improve this answer




























      0















      I have counted the rows in the columns of AdmDate1 and found the
      maximum admission frequency is 35. Since readmissions more than 4 are
      not many, I can remove the rows with rn>4 for AdmDate1 and DischDate.
      by doing this, the number of columns to be created will be reduced to
      manageable numbers




      Based on this reply i started looking for a solution which uses 4 of admdates and 4 of Dischdates.



      So this works, but I don't dare look at the query plan



      Create test data



      create table AdmissionDates(id int, AdmDate1 date,  DischDate date)
      insert into AdmissionDates(id,AdmDate1,DischDate)
      VALUES( 10001 , '2012-10-16' , '2012-10-26'),
      (10001 , '2014-06-15' , '2014-06-18') ,
      (10001 , '2014-12-21' , '2014-12-29') ,
      (10002 , '2013-02-14' , '2013-02-20') ,
      (10003 , '2013-01-23' , '2013-01-31') ,
      (10004 , '2012-11-15' , '2012-11-19') ,
      (10004 , '2014-09-26' , '2014-10-06') ,
      (10005 , '2014-12-12' , '2014-12-23') ,
      (10006 , '2013-10-23' , '2013-10-28')

      select id,row_number() over( partition by id order by admDate1 asc) as rownum, AdmDate1,row_number() over( partition by id order by DischDate asc) as rownum2, DischDate
      into #temp
      from AdmissionDates;


      The Query



      select DISTINCT id, 
      t2.admDate1 as admDate1,
      t3.admDate1 as admDate2,
      t4.admDate1 as admDate3,
      t5.admDate1 as admDate4,
      T6.DischDate as DischDate1,
      T7.DischDate as DischDate2,
      T8.DischDate as DischDate3,
      T9.DischDate as DischDate4
      FROM
      #temp t1
      OUTER APPLY
      (select admDate1 from #temp T2 where rownum = 1 and T2.id = T1.id) as T2
      OUTER APPLY
      (select admDate1 from #temp T2 where rownum = 2 and T2.id = T1.id) as T3
      OUTER APPLY
      (select admDate1 from #temp T2 where rownum = 3 and T2.id = T1.id) as T4
      OUTER APPLY
      (select admDate1 from #temp T2 where rownum = 4 and T2.id = T1.id) as T5
      OUTER APPLY
      (select DischDate from #temp T2 where rownum2 = 1 and T2.id = T1.id) as T6
      OUTER APPLY
      (select DischDate from #temp T2 where rownum2 = 2 and T2.id = T1.id) as T7
      OUTER APPLY
      (select DischDate from #temp T2 where rownum2 = 3 and T2.id = T1.id) as T8
      OUTER APPLY
      (select DischDate from #temp T2 where rownum2 = 4 and T2.id = T1.id) as T9;


      DROP TABLE #temp;





      share|improve this answer


























        0












        0








        0








        I have counted the rows in the columns of AdmDate1 and found the
        maximum admission frequency is 35. Since readmissions more than 4 are
        not many, I can remove the rows with rn>4 for AdmDate1 and DischDate.
        by doing this, the number of columns to be created will be reduced to
        manageable numbers




        Based on this reply i started looking for a solution which uses 4 of admdates and 4 of Dischdates.



        So this works, but I don't dare look at the query plan



        Create test data



        create table AdmissionDates(id int, AdmDate1 date,  DischDate date)
        insert into AdmissionDates(id,AdmDate1,DischDate)
        VALUES( 10001 , '2012-10-16' , '2012-10-26'),
        (10001 , '2014-06-15' , '2014-06-18') ,
        (10001 , '2014-12-21' , '2014-12-29') ,
        (10002 , '2013-02-14' , '2013-02-20') ,
        (10003 , '2013-01-23' , '2013-01-31') ,
        (10004 , '2012-11-15' , '2012-11-19') ,
        (10004 , '2014-09-26' , '2014-10-06') ,
        (10005 , '2014-12-12' , '2014-12-23') ,
        (10006 , '2013-10-23' , '2013-10-28')

        select id,row_number() over( partition by id order by admDate1 asc) as rownum, AdmDate1,row_number() over( partition by id order by DischDate asc) as rownum2, DischDate
        into #temp
        from AdmissionDates;


        The Query



        select DISTINCT id, 
        t2.admDate1 as admDate1,
        t3.admDate1 as admDate2,
        t4.admDate1 as admDate3,
        t5.admDate1 as admDate4,
        T6.DischDate as DischDate1,
        T7.DischDate as DischDate2,
        T8.DischDate as DischDate3,
        T9.DischDate as DischDate4
        FROM
        #temp t1
        OUTER APPLY
        (select admDate1 from #temp T2 where rownum = 1 and T2.id = T1.id) as T2
        OUTER APPLY
        (select admDate1 from #temp T2 where rownum = 2 and T2.id = T1.id) as T3
        OUTER APPLY
        (select admDate1 from #temp T2 where rownum = 3 and T2.id = T1.id) as T4
        OUTER APPLY
        (select admDate1 from #temp T2 where rownum = 4 and T2.id = T1.id) as T5
        OUTER APPLY
        (select DischDate from #temp T2 where rownum2 = 1 and T2.id = T1.id) as T6
        OUTER APPLY
        (select DischDate from #temp T2 where rownum2 = 2 and T2.id = T1.id) as T7
        OUTER APPLY
        (select DischDate from #temp T2 where rownum2 = 3 and T2.id = T1.id) as T8
        OUTER APPLY
        (select DischDate from #temp T2 where rownum2 = 4 and T2.id = T1.id) as T9;


        DROP TABLE #temp;





        share|improve this answer














        I have counted the rows in the columns of AdmDate1 and found the
        maximum admission frequency is 35. Since readmissions more than 4 are
        not many, I can remove the rows with rn>4 for AdmDate1 and DischDate.
        by doing this, the number of columns to be created will be reduced to
        manageable numbers




        Based on this reply i started looking for a solution which uses 4 of admdates and 4 of Dischdates.



        So this works, but I don't dare look at the query plan



        Create test data



        create table AdmissionDates(id int, AdmDate1 date,  DischDate date)
        insert into AdmissionDates(id,AdmDate1,DischDate)
        VALUES( 10001 , '2012-10-16' , '2012-10-26'),
        (10001 , '2014-06-15' , '2014-06-18') ,
        (10001 , '2014-12-21' , '2014-12-29') ,
        (10002 , '2013-02-14' , '2013-02-20') ,
        (10003 , '2013-01-23' , '2013-01-31') ,
        (10004 , '2012-11-15' , '2012-11-19') ,
        (10004 , '2014-09-26' , '2014-10-06') ,
        (10005 , '2014-12-12' , '2014-12-23') ,
        (10006 , '2013-10-23' , '2013-10-28')

        select id,row_number() over( partition by id order by admDate1 asc) as rownum, AdmDate1,row_number() over( partition by id order by DischDate asc) as rownum2, DischDate
        into #temp
        from AdmissionDates;


        The Query



        select DISTINCT id, 
        t2.admDate1 as admDate1,
        t3.admDate1 as admDate2,
        t4.admDate1 as admDate3,
        t5.admDate1 as admDate4,
        T6.DischDate as DischDate1,
        T7.DischDate as DischDate2,
        T8.DischDate as DischDate3,
        T9.DischDate as DischDate4
        FROM
        #temp t1
        OUTER APPLY
        (select admDate1 from #temp T2 where rownum = 1 and T2.id = T1.id) as T2
        OUTER APPLY
        (select admDate1 from #temp T2 where rownum = 2 and T2.id = T1.id) as T3
        OUTER APPLY
        (select admDate1 from #temp T2 where rownum = 3 and T2.id = T1.id) as T4
        OUTER APPLY
        (select admDate1 from #temp T2 where rownum = 4 and T2.id = T1.id) as T5
        OUTER APPLY
        (select DischDate from #temp T2 where rownum2 = 1 and T2.id = T1.id) as T6
        OUTER APPLY
        (select DischDate from #temp T2 where rownum2 = 2 and T2.id = T1.id) as T7
        OUTER APPLY
        (select DischDate from #temp T2 where rownum2 = 3 and T2.id = T1.id) as T8
        OUTER APPLY
        (select DischDate from #temp T2 where rownum2 = 4 and T2.id = T1.id) as T9;


        DROP TABLE #temp;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 22:32









        Randi VertongenRandi Vertongen

        3,293822




        3,293822






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Database Administrators Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f222195%2ftransposing-repeated-rows-of-datetime-into-columns-in-sql-17%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Anexo:Material bélico de la Fuerza Aérea de Chile Índice Aeronaves Defensa...

            Always On Availability groups resolving state after failover - Remote harden of transaction...

            update json value to null Announcing the arrival of Valued Associate #679: Cesar Manara ...