SQL - How to get columns from row values in the same column (SQL Server 2016)











up vote
0
down vote

favorite












I need to derive columns from the row values of one column.



Here's the row data.



CustomerID  Activity    Date
10001 Active 2018-06-21
10001 Inactive 2018-06-25
10001 Active 2018-08-22
10001 Inactive 2018-10-06


And here's the output that I am trying to get to:



CustomerID  ActiveDate      InactiveDate
10001 2018-06-21 2018-06-25
10001 2018-08-22 2018-10-06


Please help! Thanks!










share|improve this question
























  • please provide a minimal, complete, and verifiable example
    – landru27
    Nov 10 at 0:47










  • Are you certain that for every "active" there is an associated "inactive" that is the row that immediately follows (based on date)? Don't assume, go look. And don't just look at the first set of rows that you see in the results window of a SSMS query (or the results of the "select rows" menu).
    – SMor
    Nov 10 at 2:08















up vote
0
down vote

favorite












I need to derive columns from the row values of one column.



Here's the row data.



CustomerID  Activity    Date
10001 Active 2018-06-21
10001 Inactive 2018-06-25
10001 Active 2018-08-22
10001 Inactive 2018-10-06


And here's the output that I am trying to get to:



CustomerID  ActiveDate      InactiveDate
10001 2018-06-21 2018-06-25
10001 2018-08-22 2018-10-06


Please help! Thanks!










share|improve this question
























  • please provide a minimal, complete, and verifiable example
    – landru27
    Nov 10 at 0:47










  • Are you certain that for every "active" there is an associated "inactive" that is the row that immediately follows (based on date)? Don't assume, go look. And don't just look at the first set of rows that you see in the results window of a SSMS query (or the results of the "select rows" menu).
    – SMor
    Nov 10 at 2:08













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I need to derive columns from the row values of one column.



Here's the row data.



CustomerID  Activity    Date
10001 Active 2018-06-21
10001 Inactive 2018-06-25
10001 Active 2018-08-22
10001 Inactive 2018-10-06


And here's the output that I am trying to get to:



CustomerID  ActiveDate      InactiveDate
10001 2018-06-21 2018-06-25
10001 2018-08-22 2018-10-06


Please help! Thanks!










share|improve this question















I need to derive columns from the row values of one column.



Here's the row data.



CustomerID  Activity    Date
10001 Active 2018-06-21
10001 Inactive 2018-06-25
10001 Active 2018-08-22
10001 Inactive 2018-10-06


And here's the output that I am trying to get to:



CustomerID  ActiveDate      InactiveDate
10001 2018-06-21 2018-06-25
10001 2018-08-22 2018-10-06


Please help! Thanks!







sql sql-server






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 13:01









Gordon Linoff

748k34285391




748k34285391










asked Nov 10 at 0:35









user10631408

11




11












  • please provide a minimal, complete, and verifiable example
    – landru27
    Nov 10 at 0:47










  • Are you certain that for every "active" there is an associated "inactive" that is the row that immediately follows (based on date)? Don't assume, go look. And don't just look at the first set of rows that you see in the results window of a SSMS query (or the results of the "select rows" menu).
    – SMor
    Nov 10 at 2:08


















  • please provide a minimal, complete, and verifiable example
    – landru27
    Nov 10 at 0:47










  • Are you certain that for every "active" there is an associated "inactive" that is the row that immediately follows (based on date)? Don't assume, go look. And don't just look at the first set of rows that you see in the results window of a SSMS query (or the results of the "select rows" menu).
    – SMor
    Nov 10 at 2:08
















please provide a minimal, complete, and verifiable example
– landru27
Nov 10 at 0:47




please provide a minimal, complete, and verifiable example
– landru27
Nov 10 at 0:47












Are you certain that for every "active" there is an associated "inactive" that is the row that immediately follows (based on date)? Don't assume, go look. And don't just look at the first set of rows that you see in the results window of a SSMS query (or the results of the "select rows" menu).
– SMor
Nov 10 at 2:08




Are you certain that for every "active" there is an associated "inactive" that is the row that immediately follows (based on date)? Don't assume, go look. And don't just look at the first set of rows that you see in the results window of a SSMS query (or the results of the "select rows" menu).
– SMor
Nov 10 at 2:08












2 Answers
2






active

oldest

votes

















up vote
1
down vote













You can try to make row number in subquery group by CustomerID,Activity, then do condition aggregate function.



SELECT CustomerID,
MAX(CASE WHEN Activity = 'Active' THEN Date END) ActiveDate,
MAX(CASE WHEN Activity = 'Inactive' THEN Date END) InactiveDate
FROM (
SELECT *,ROW_NUMBER() OVER(PARTITION BY CustomerID,Activity ORDER BY Date ) rn
FROM T
)t1
group by CustomerID,rn


sqlfiddle






share|improve this answer





















  • I dont see any need for row number here.
    – Martin Smith
    Nov 10 at 1:03










  • if didn't make row number the pivot will get only one row? or Could you post your another solution? I am looking forward :)
    – D-Shih
    Nov 10 at 1:11












  • Did you mean this? dbfiddle.uk/…
    – D-Shih
    Nov 10 at 1:14










  • Ah sorry just realised I missed that it was the same Customerid throughout. Thought they were 2 different customers
    – Martin Smith
    Nov 10 at 1:14










  • Yep the problem is sample data have same Customerid, therefore I need to create a row number for it :)
    – D-Shih
    Nov 10 at 1:15




















up vote
0
down vote













You logic is a little unclear. If you want the next "inactive" date:



select CustomerID, date as active_date, inactive_date
from (select t.*,
min(case when activity = 'Inactive' then date end) over (partition by CustomerID order by date desc) as inactive_date
from t
) t
where activity = 'Active';





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    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',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    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%2fstackoverflow.com%2fquestions%2f53234979%2fsql-how-to-get-columns-from-row-values-in-the-same-column-sql-server-2016%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    You can try to make row number in subquery group by CustomerID,Activity, then do condition aggregate function.



    SELECT CustomerID,
    MAX(CASE WHEN Activity = 'Active' THEN Date END) ActiveDate,
    MAX(CASE WHEN Activity = 'Inactive' THEN Date END) InactiveDate
    FROM (
    SELECT *,ROW_NUMBER() OVER(PARTITION BY CustomerID,Activity ORDER BY Date ) rn
    FROM T
    )t1
    group by CustomerID,rn


    sqlfiddle






    share|improve this answer





















    • I dont see any need for row number here.
      – Martin Smith
      Nov 10 at 1:03










    • if didn't make row number the pivot will get only one row? or Could you post your another solution? I am looking forward :)
      – D-Shih
      Nov 10 at 1:11












    • Did you mean this? dbfiddle.uk/…
      – D-Shih
      Nov 10 at 1:14










    • Ah sorry just realised I missed that it was the same Customerid throughout. Thought they were 2 different customers
      – Martin Smith
      Nov 10 at 1:14










    • Yep the problem is sample data have same Customerid, therefore I need to create a row number for it :)
      – D-Shih
      Nov 10 at 1:15

















    up vote
    1
    down vote













    You can try to make row number in subquery group by CustomerID,Activity, then do condition aggregate function.



    SELECT CustomerID,
    MAX(CASE WHEN Activity = 'Active' THEN Date END) ActiveDate,
    MAX(CASE WHEN Activity = 'Inactive' THEN Date END) InactiveDate
    FROM (
    SELECT *,ROW_NUMBER() OVER(PARTITION BY CustomerID,Activity ORDER BY Date ) rn
    FROM T
    )t1
    group by CustomerID,rn


    sqlfiddle






    share|improve this answer





















    • I dont see any need for row number here.
      – Martin Smith
      Nov 10 at 1:03










    • if didn't make row number the pivot will get only one row? or Could you post your another solution? I am looking forward :)
      – D-Shih
      Nov 10 at 1:11












    • Did you mean this? dbfiddle.uk/…
      – D-Shih
      Nov 10 at 1:14










    • Ah sorry just realised I missed that it was the same Customerid throughout. Thought they were 2 different customers
      – Martin Smith
      Nov 10 at 1:14










    • Yep the problem is sample data have same Customerid, therefore I need to create a row number for it :)
      – D-Shih
      Nov 10 at 1:15















    up vote
    1
    down vote










    up vote
    1
    down vote









    You can try to make row number in subquery group by CustomerID,Activity, then do condition aggregate function.



    SELECT CustomerID,
    MAX(CASE WHEN Activity = 'Active' THEN Date END) ActiveDate,
    MAX(CASE WHEN Activity = 'Inactive' THEN Date END) InactiveDate
    FROM (
    SELECT *,ROW_NUMBER() OVER(PARTITION BY CustomerID,Activity ORDER BY Date ) rn
    FROM T
    )t1
    group by CustomerID,rn


    sqlfiddle






    share|improve this answer












    You can try to make row number in subquery group by CustomerID,Activity, then do condition aggregate function.



    SELECT CustomerID,
    MAX(CASE WHEN Activity = 'Active' THEN Date END) ActiveDate,
    MAX(CASE WHEN Activity = 'Inactive' THEN Date END) InactiveDate
    FROM (
    SELECT *,ROW_NUMBER() OVER(PARTITION BY CustomerID,Activity ORDER BY Date ) rn
    FROM T
    )t1
    group by CustomerID,rn


    sqlfiddle







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 10 at 0:50









    D-Shih

    24.4k61431




    24.4k61431












    • I dont see any need for row number here.
      – Martin Smith
      Nov 10 at 1:03










    • if didn't make row number the pivot will get only one row? or Could you post your another solution? I am looking forward :)
      – D-Shih
      Nov 10 at 1:11












    • Did you mean this? dbfiddle.uk/…
      – D-Shih
      Nov 10 at 1:14










    • Ah sorry just realised I missed that it was the same Customerid throughout. Thought they were 2 different customers
      – Martin Smith
      Nov 10 at 1:14










    • Yep the problem is sample data have same Customerid, therefore I need to create a row number for it :)
      – D-Shih
      Nov 10 at 1:15




















    • I dont see any need for row number here.
      – Martin Smith
      Nov 10 at 1:03










    • if didn't make row number the pivot will get only one row? or Could you post your another solution? I am looking forward :)
      – D-Shih
      Nov 10 at 1:11












    • Did you mean this? dbfiddle.uk/…
      – D-Shih
      Nov 10 at 1:14










    • Ah sorry just realised I missed that it was the same Customerid throughout. Thought they were 2 different customers
      – Martin Smith
      Nov 10 at 1:14










    • Yep the problem is sample data have same Customerid, therefore I need to create a row number for it :)
      – D-Shih
      Nov 10 at 1:15


















    I dont see any need for row number here.
    – Martin Smith
    Nov 10 at 1:03




    I dont see any need for row number here.
    – Martin Smith
    Nov 10 at 1:03












    if didn't make row number the pivot will get only one row? or Could you post your another solution? I am looking forward :)
    – D-Shih
    Nov 10 at 1:11






    if didn't make row number the pivot will get only one row? or Could you post your another solution? I am looking forward :)
    – D-Shih
    Nov 10 at 1:11














    Did you mean this? dbfiddle.uk/…
    – D-Shih
    Nov 10 at 1:14




    Did you mean this? dbfiddle.uk/…
    – D-Shih
    Nov 10 at 1:14












    Ah sorry just realised I missed that it was the same Customerid throughout. Thought they were 2 different customers
    – Martin Smith
    Nov 10 at 1:14




    Ah sorry just realised I missed that it was the same Customerid throughout. Thought they were 2 different customers
    – Martin Smith
    Nov 10 at 1:14












    Yep the problem is sample data have same Customerid, therefore I need to create a row number for it :)
    – D-Shih
    Nov 10 at 1:15






    Yep the problem is sample data have same Customerid, therefore I need to create a row number for it :)
    – D-Shih
    Nov 10 at 1:15














    up vote
    0
    down vote













    You logic is a little unclear. If you want the next "inactive" date:



    select CustomerID, date as active_date, inactive_date
    from (select t.*,
    min(case when activity = 'Inactive' then date end) over (partition by CustomerID order by date desc) as inactive_date
    from t
    ) t
    where activity = 'Active';





    share|improve this answer

























      up vote
      0
      down vote













      You logic is a little unclear. If you want the next "inactive" date:



      select CustomerID, date as active_date, inactive_date
      from (select t.*,
      min(case when activity = 'Inactive' then date end) over (partition by CustomerID order by date desc) as inactive_date
      from t
      ) t
      where activity = 'Active';





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        You logic is a little unclear. If you want the next "inactive" date:



        select CustomerID, date as active_date, inactive_date
        from (select t.*,
        min(case when activity = 'Inactive' then date end) over (partition by CustomerID order by date desc) as inactive_date
        from t
        ) t
        where activity = 'Active';





        share|improve this answer












        You logic is a little unclear. If you want the next "inactive" date:



        select CustomerID, date as active_date, inactive_date
        from (select t.*,
        min(case when activity = 'Inactive' then date end) over (partition by CustomerID order by date desc) as inactive_date
        from t
        ) t
        where activity = 'Active';






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 13:03









        Gordon Linoff

        748k34285391




        748k34285391






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • 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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2fstackoverflow.com%2fquestions%2f53234979%2fsql-how-to-get-columns-from-row-values-in-the-same-column-sql-server-2016%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

            Schultheiß

            Verwaltungsgliederung Dänemarks

            Liste der Kulturdenkmale in Wilsdruff