Selecting the same column from Different rows Based on Different CriteriaUsing the name of a table contained in a column of another table?SQL unpivoting multiple rows/columns, but keeping the rows grouped together, and in the same order they were selectedPage Split TimingMoving data from table with VARCHAR(50) fields to table with numeric fields increases table sizeHandling duplicate values using triggerWhy am I getting “Snapshot isolation transaction aborted due to update conflict”?Query runs slowly when a non-indexed column is added to the WHERE clauseProper table design for sparse primary keyProper normalization for an Orders table--whether to split or no

Why do many websites hide input when entering a OTP

Scorched receptacle

In what sense is SL(2,q) "very far from abelian"?

Using 4K Skyrim Textures when running 1920 x 1080 display resolution?

Does every Ubuntu question answer apply to it's derivatives? (Xubuntu, Lubuntu, Kubuntu)

Can I voluntarily exit from the US after a 20 year overstay, or could I be detained at the airport?

Proof of bound on optimal TSP tour length in rectangular region

How do we know for sure a transliteration is lossless?

Why is the time of useful consciousness only seconds at high altitudes, when I can hold my breath much longer at ground level?

Use floats or doubles when writing mobile games

As a girl, how can I voice male characters effectively?

Power Adapter for Traveling to Scotland (I live in the US)

Maintaining distance

Is there any problem with students seeing faculty naked in university gym?

How to change the order of integration when limit is a function?

My second game: War Card game V.1

Found a minor bug, affecting 1% of users. What should QA do?

Is Zhent just the term for any member of the Zhentarim?

Minimum perfect squares needed to sum up to a target

How to explain that the sums of numerators over sums of denominators isn't the same as the mean of ratios?

Would houseruling two or more instances of resistance to the same element as immunity be overly unbalanced?

How are characteristic classes morphisms of infinite loop spaces? (if they are)

Conveying the idea of "down the road" (i.e. in the future)

Why is my vegetable stock bitter, but the chicken stock not?



Selecting the same column from Different rows Based on Different Criteria


Using the name of a table contained in a column of another table?SQL unpivoting multiple rows/columns, but keeping the rows grouped together, and in the same order they were selectedPage Split TimingMoving data from table with VARCHAR(50) fields to table with numeric fields increases table sizeHandling duplicate values using triggerWhy am I getting “Snapshot isolation transaction aborted due to update conflict”?Query runs slowly when a non-indexed column is added to the WHERE clauseProper table design for sparse primary keyProper normalization for an Orders table--whether to split or no






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









2















My table creates a row for each customer name for the unique customer number.



CREATE TABLE #src(Number int, name varchar(32), seq bit);

INSERT #src(Number,name,seq) VALUES
(12345,'Mickey Mouse',0),
(12345,'Minnie Mouse',1),
(45678,'Donald Duck',0),
(45678,'Daphney Duck',1),
(245678,'Pluto Dog',0);


I need to be able to return a single row with multiple name columns based on the "Seq" number. It will always either be a 0 or a 1 and the Seq 1 can sometimes be blank.



enter image description here










share|improve this question
































    2















    My table creates a row for each customer name for the unique customer number.



    CREATE TABLE #src(Number int, name varchar(32), seq bit);

    INSERT #src(Number,name,seq) VALUES
    (12345,'Mickey Mouse',0),
    (12345,'Minnie Mouse',1),
    (45678,'Donald Duck',0),
    (45678,'Daphney Duck',1),
    (245678,'Pluto Dog',0);


    I need to be able to return a single row with multiple name columns based on the "Seq" number. It will always either be a 0 or a 1 and the Seq 1 can sometimes be blank.



    enter image description here










    share|improve this question




























      2












      2








      2








      My table creates a row for each customer name for the unique customer number.



      CREATE TABLE #src(Number int, name varchar(32), seq bit);

      INSERT #src(Number,name,seq) VALUES
      (12345,'Mickey Mouse',0),
      (12345,'Minnie Mouse',1),
      (45678,'Donald Duck',0),
      (45678,'Daphney Duck',1),
      (245678,'Pluto Dog',0);


      I need to be able to return a single row with multiple name columns based on the "Seq" number. It will always either be a 0 or a 1 and the Seq 1 can sometimes be blank.



      enter image description here










      share|improve this question
















      My table creates a row for each customer name for the unique customer number.



      CREATE TABLE #src(Number int, name varchar(32), seq bit);

      INSERT #src(Number,name,seq) VALUES
      (12345,'Mickey Mouse',0),
      (12345,'Minnie Mouse',1),
      (45678,'Donald Duck',0),
      (45678,'Daphney Duck',1),
      (245678,'Pluto Dog',0);


      I need to be able to return a single row with multiple name columns based on the "Seq" number. It will always either be a 0 or a 1 and the Seq 1 can sometimes be blank.



      enter image description here







      sql-server sql-server-2012 pivot






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 16 at 19:47









      Aaron Bertrand

      161k19 gold badges324 silver badges530 bronze badges




      161k19 gold badges324 silver badges530 bronze badges










      asked Apr 16 at 19:32









      Nicole MontezNicole Montez

      111 bronze badge




      111 bronze badge























          2 Answers
          2






          active

          oldest

          votes


















          2
















          Given this table and data:



          CREATE TABLE #src(Number int, name varchar(32), seq bit);

          INSERT #src(Number,name,seq) VALUES
          (12345,'Mickey Mouse',0),
          (12345,'Minnie Mouse',1),
          (45678,'Donald Duck',0),
          (45678,'Daphney Duck',1),
          (245678,'Pluto Dog',0);


          You can apply a simple PIVOT:



          SELECT Number, Owner1 = [0], Owner2 = COALESCE([1],'')
          FROM #src AS c
          PIVOT (MAX(name) FOR seq IN ([0],[1])) AS p
          ORDER BY Number;


          Results:



          number Owner1 Owner2
          ------ ------------ ------------
          12345 Mickey Mouse Minnie Mouse
          45678 Donald Duck Daphney Duck
          245678 Pluto Dog


          I would just ensure that Number, seq is enforced to be unique and that seq is either a bit or has a constraint so that it can only be 0 or 1.






          share|improve this answer


































            1
















            Another method without using pivot



            CREATE TABLE dbo.Customer(Number int, [Name] varchar(255),Seq bit);
            INSERT INTO dbo.Customer(Number,[Name],Seq)
            VALUES
            (12345,'Mickey Mouse',0),
            (12345,'Minnie Mouse',1),
            (45678,'Donald Duck',0),
            (45678,'Daphney Duck',1),
            (245678,'Pluto Dog',0);

            SELECT Number,
            MAX(CASE WHEN Seq = 0 THEN [Name] END) as Owner1,
            MAX(CASE WHEN Seq = 1 Then [Name] END) AS Owner2
            FROM dbo.Customer
            GROUP BY Number;


            Result



            Number Owner1 Owner2
            12345 Mickey Mouse Minnie Mouse
            45678 Donald Duck Daphney Duck
            245678 Pluto Dog NULL



            Update: Issue with many columns



            If the other columns mentioned in the comments are the same based on the number, you can add them to the group by, and cast the TEXT value as varchar(4000) (max 3640 datalength) Otherwise you would have to choose one of the two with MAX / MIN / ...



            SELECT Number, 
            MAX(CASE WHEN Seq = 0 THEN [Name] END) as Owner1,
            MAX(CASE WHEN Seq = 1 Then [Name] END) AS Owner2
            ,[Street1], [Street2], [City] , [State] , [Zipcode] , [HomePhone] ,CAST(JobMemo as varchar(4000))
            FROM dbo.Debtors
            GROUP BY Number,[Street1], [Street2], [City] , [State] , [Zipcode] , [HomePhone] ,CAST(JobMemo as varchar(4000));


            Disclaimer: the performance will probably not be optimal






            share|improve this answer



























            • OK thank you! So there's pretty much no way to run a simle query without creating a table first?

              – Nicole Montez
              Apr 16 at 19:58











            • @NicoleMontez No problem, I am not sure what you mean by the 'without creating a table first'? I could have created a temp table aswell. As far as questions go, giving the sample data + table create statement really helps! We always have to create a table with the same sample data to show a valid resultset as an answer :).

              – Randi Vertongen
              Apr 16 at 20:03












            • Error on Pivot table: Pivot grouping columns must be comparable. The type of column "JobMemo" is "text", which is not comparable. CREATE TABLE [dbo].[Debtors]( [Number] [int] NOT NULL, [Seq] [int] NULL, [Name] [varchar](30) NULL, [Street1] [varchar](30) NULL, [Street2] [varchar](30) NULL, [City] [varchar](30) NULL, [State] [varchar](3) NULL, [Zipcode] [varchar](10) NULL, [HomePhone] [varchar](30) NULL,

              – Nicole Montez
              Apr 16 at 20:07












            • @NicoleMontez does the JobMemo need to be text? What does SELECT MAX(DATALENGTH(JobMemo)) from dbo.Debtors return? Maybe you could change it to varchar or nvarchar datatype? You could also cast it in the query. Could you add the entire table to the question?

              – Randi Vertongen
              Apr 16 at 20:27












            • It's HUGE. I couldn't fit all of the characters. I'm sorry, this is my first question. I normally just wing it on my own. I inherited this table, and it's very invloved.

              – Nicole Montez
              Apr 16 at 20:32












            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/4.0/"u003ecc by-sa 4.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%2f234964%2fselecting-the-same-column-from-different-rows-based-on-different-criteria%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









            2
















            Given this table and data:



            CREATE TABLE #src(Number int, name varchar(32), seq bit);

            INSERT #src(Number,name,seq) VALUES
            (12345,'Mickey Mouse',0),
            (12345,'Minnie Mouse',1),
            (45678,'Donald Duck',0),
            (45678,'Daphney Duck',1),
            (245678,'Pluto Dog',0);


            You can apply a simple PIVOT:



            SELECT Number, Owner1 = [0], Owner2 = COALESCE([1],'')
            FROM #src AS c
            PIVOT (MAX(name) FOR seq IN ([0],[1])) AS p
            ORDER BY Number;


            Results:



            number Owner1 Owner2
            ------ ------------ ------------
            12345 Mickey Mouse Minnie Mouse
            45678 Donald Duck Daphney Duck
            245678 Pluto Dog


            I would just ensure that Number, seq is enforced to be unique and that seq is either a bit or has a constraint so that it can only be 0 or 1.






            share|improve this answer































              2
















              Given this table and data:



              CREATE TABLE #src(Number int, name varchar(32), seq bit);

              INSERT #src(Number,name,seq) VALUES
              (12345,'Mickey Mouse',0),
              (12345,'Minnie Mouse',1),
              (45678,'Donald Duck',0),
              (45678,'Daphney Duck',1),
              (245678,'Pluto Dog',0);


              You can apply a simple PIVOT:



              SELECT Number, Owner1 = [0], Owner2 = COALESCE([1],'')
              FROM #src AS c
              PIVOT (MAX(name) FOR seq IN ([0],[1])) AS p
              ORDER BY Number;


              Results:



              number Owner1 Owner2
              ------ ------------ ------------
              12345 Mickey Mouse Minnie Mouse
              45678 Donald Duck Daphney Duck
              245678 Pluto Dog


              I would just ensure that Number, seq is enforced to be unique and that seq is either a bit or has a constraint so that it can only be 0 or 1.






              share|improve this answer





























                2














                2










                2









                Given this table and data:



                CREATE TABLE #src(Number int, name varchar(32), seq bit);

                INSERT #src(Number,name,seq) VALUES
                (12345,'Mickey Mouse',0),
                (12345,'Minnie Mouse',1),
                (45678,'Donald Duck',0),
                (45678,'Daphney Duck',1),
                (245678,'Pluto Dog',0);


                You can apply a simple PIVOT:



                SELECT Number, Owner1 = [0], Owner2 = COALESCE([1],'')
                FROM #src AS c
                PIVOT (MAX(name) FOR seq IN ([0],[1])) AS p
                ORDER BY Number;


                Results:



                number Owner1 Owner2
                ------ ------------ ------------
                12345 Mickey Mouse Minnie Mouse
                45678 Donald Duck Daphney Duck
                245678 Pluto Dog


                I would just ensure that Number, seq is enforced to be unique and that seq is either a bit or has a constraint so that it can only be 0 or 1.






                share|improve this answer















                Given this table and data:



                CREATE TABLE #src(Number int, name varchar(32), seq bit);

                INSERT #src(Number,name,seq) VALUES
                (12345,'Mickey Mouse',0),
                (12345,'Minnie Mouse',1),
                (45678,'Donald Duck',0),
                (45678,'Daphney Duck',1),
                (245678,'Pluto Dog',0);


                You can apply a simple PIVOT:



                SELECT Number, Owner1 = [0], Owner2 = COALESCE([1],'')
                FROM #src AS c
                PIVOT (MAX(name) FOR seq IN ([0],[1])) AS p
                ORDER BY Number;


                Results:



                number Owner1 Owner2
                ------ ------------ ------------
                12345 Mickey Mouse Minnie Mouse
                45678 Donald Duck Daphney Duck
                245678 Pluto Dog


                I would just ensure that Number, seq is enforced to be unique and that seq is either a bit or has a constraint so that it can only be 0 or 1.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Apr 16 at 19:57

























                answered Apr 16 at 19:45









                Aaron BertrandAaron Bertrand

                161k19 gold badges324 silver badges530 bronze badges




                161k19 gold badges324 silver badges530 bronze badges


























                    1
















                    Another method without using pivot



                    CREATE TABLE dbo.Customer(Number int, [Name] varchar(255),Seq bit);
                    INSERT INTO dbo.Customer(Number,[Name],Seq)
                    VALUES
                    (12345,'Mickey Mouse',0),
                    (12345,'Minnie Mouse',1),
                    (45678,'Donald Duck',0),
                    (45678,'Daphney Duck',1),
                    (245678,'Pluto Dog',0);

                    SELECT Number,
                    MAX(CASE WHEN Seq = 0 THEN [Name] END) as Owner1,
                    MAX(CASE WHEN Seq = 1 Then [Name] END) AS Owner2
                    FROM dbo.Customer
                    GROUP BY Number;


                    Result



                    Number Owner1 Owner2
                    12345 Mickey Mouse Minnie Mouse
                    45678 Donald Duck Daphney Duck
                    245678 Pluto Dog NULL



                    Update: Issue with many columns



                    If the other columns mentioned in the comments are the same based on the number, you can add them to the group by, and cast the TEXT value as varchar(4000) (max 3640 datalength) Otherwise you would have to choose one of the two with MAX / MIN / ...



                    SELECT Number, 
                    MAX(CASE WHEN Seq = 0 THEN [Name] END) as Owner1,
                    MAX(CASE WHEN Seq = 1 Then [Name] END) AS Owner2
                    ,[Street1], [Street2], [City] , [State] , [Zipcode] , [HomePhone] ,CAST(JobMemo as varchar(4000))
                    FROM dbo.Debtors
                    GROUP BY Number,[Street1], [Street2], [City] , [State] , [Zipcode] , [HomePhone] ,CAST(JobMemo as varchar(4000));


                    Disclaimer: the performance will probably not be optimal






                    share|improve this answer



























                    • OK thank you! So there's pretty much no way to run a simle query without creating a table first?

                      – Nicole Montez
                      Apr 16 at 19:58











                    • @NicoleMontez No problem, I am not sure what you mean by the 'without creating a table first'? I could have created a temp table aswell. As far as questions go, giving the sample data + table create statement really helps! We always have to create a table with the same sample data to show a valid resultset as an answer :).

                      – Randi Vertongen
                      Apr 16 at 20:03












                    • Error on Pivot table: Pivot grouping columns must be comparable. The type of column "JobMemo" is "text", which is not comparable. CREATE TABLE [dbo].[Debtors]( [Number] [int] NOT NULL, [Seq] [int] NULL, [Name] [varchar](30) NULL, [Street1] [varchar](30) NULL, [Street2] [varchar](30) NULL, [City] [varchar](30) NULL, [State] [varchar](3) NULL, [Zipcode] [varchar](10) NULL, [HomePhone] [varchar](30) NULL,

                      – Nicole Montez
                      Apr 16 at 20:07












                    • @NicoleMontez does the JobMemo need to be text? What does SELECT MAX(DATALENGTH(JobMemo)) from dbo.Debtors return? Maybe you could change it to varchar or nvarchar datatype? You could also cast it in the query. Could you add the entire table to the question?

                      – Randi Vertongen
                      Apr 16 at 20:27












                    • It's HUGE. I couldn't fit all of the characters. I'm sorry, this is my first question. I normally just wing it on my own. I inherited this table, and it's very invloved.

                      – Nicole Montez
                      Apr 16 at 20:32















                    1
















                    Another method without using pivot



                    CREATE TABLE dbo.Customer(Number int, [Name] varchar(255),Seq bit);
                    INSERT INTO dbo.Customer(Number,[Name],Seq)
                    VALUES
                    (12345,'Mickey Mouse',0),
                    (12345,'Minnie Mouse',1),
                    (45678,'Donald Duck',0),
                    (45678,'Daphney Duck',1),
                    (245678,'Pluto Dog',0);

                    SELECT Number,
                    MAX(CASE WHEN Seq = 0 THEN [Name] END) as Owner1,
                    MAX(CASE WHEN Seq = 1 Then [Name] END) AS Owner2
                    FROM dbo.Customer
                    GROUP BY Number;


                    Result



                    Number Owner1 Owner2
                    12345 Mickey Mouse Minnie Mouse
                    45678 Donald Duck Daphney Duck
                    245678 Pluto Dog NULL



                    Update: Issue with many columns



                    If the other columns mentioned in the comments are the same based on the number, you can add them to the group by, and cast the TEXT value as varchar(4000) (max 3640 datalength) Otherwise you would have to choose one of the two with MAX / MIN / ...



                    SELECT Number, 
                    MAX(CASE WHEN Seq = 0 THEN [Name] END) as Owner1,
                    MAX(CASE WHEN Seq = 1 Then [Name] END) AS Owner2
                    ,[Street1], [Street2], [City] , [State] , [Zipcode] , [HomePhone] ,CAST(JobMemo as varchar(4000))
                    FROM dbo.Debtors
                    GROUP BY Number,[Street1], [Street2], [City] , [State] , [Zipcode] , [HomePhone] ,CAST(JobMemo as varchar(4000));


                    Disclaimer: the performance will probably not be optimal






                    share|improve this answer



























                    • OK thank you! So there's pretty much no way to run a simle query without creating a table first?

                      – Nicole Montez
                      Apr 16 at 19:58











                    • @NicoleMontez No problem, I am not sure what you mean by the 'without creating a table first'? I could have created a temp table aswell. As far as questions go, giving the sample data + table create statement really helps! We always have to create a table with the same sample data to show a valid resultset as an answer :).

                      – Randi Vertongen
                      Apr 16 at 20:03












                    • Error on Pivot table: Pivot grouping columns must be comparable. The type of column "JobMemo" is "text", which is not comparable. CREATE TABLE [dbo].[Debtors]( [Number] [int] NOT NULL, [Seq] [int] NULL, [Name] [varchar](30) NULL, [Street1] [varchar](30) NULL, [Street2] [varchar](30) NULL, [City] [varchar](30) NULL, [State] [varchar](3) NULL, [Zipcode] [varchar](10) NULL, [HomePhone] [varchar](30) NULL,

                      – Nicole Montez
                      Apr 16 at 20:07












                    • @NicoleMontez does the JobMemo need to be text? What does SELECT MAX(DATALENGTH(JobMemo)) from dbo.Debtors return? Maybe you could change it to varchar or nvarchar datatype? You could also cast it in the query. Could you add the entire table to the question?

                      – Randi Vertongen
                      Apr 16 at 20:27












                    • It's HUGE. I couldn't fit all of the characters. I'm sorry, this is my first question. I normally just wing it on my own. I inherited this table, and it's very invloved.

                      – Nicole Montez
                      Apr 16 at 20:32













                    1














                    1










                    1









                    Another method without using pivot



                    CREATE TABLE dbo.Customer(Number int, [Name] varchar(255),Seq bit);
                    INSERT INTO dbo.Customer(Number,[Name],Seq)
                    VALUES
                    (12345,'Mickey Mouse',0),
                    (12345,'Minnie Mouse',1),
                    (45678,'Donald Duck',0),
                    (45678,'Daphney Duck',1),
                    (245678,'Pluto Dog',0);

                    SELECT Number,
                    MAX(CASE WHEN Seq = 0 THEN [Name] END) as Owner1,
                    MAX(CASE WHEN Seq = 1 Then [Name] END) AS Owner2
                    FROM dbo.Customer
                    GROUP BY Number;


                    Result



                    Number Owner1 Owner2
                    12345 Mickey Mouse Minnie Mouse
                    45678 Donald Duck Daphney Duck
                    245678 Pluto Dog NULL



                    Update: Issue with many columns



                    If the other columns mentioned in the comments are the same based on the number, you can add them to the group by, and cast the TEXT value as varchar(4000) (max 3640 datalength) Otherwise you would have to choose one of the two with MAX / MIN / ...



                    SELECT Number, 
                    MAX(CASE WHEN Seq = 0 THEN [Name] END) as Owner1,
                    MAX(CASE WHEN Seq = 1 Then [Name] END) AS Owner2
                    ,[Street1], [Street2], [City] , [State] , [Zipcode] , [HomePhone] ,CAST(JobMemo as varchar(4000))
                    FROM dbo.Debtors
                    GROUP BY Number,[Street1], [Street2], [City] , [State] , [Zipcode] , [HomePhone] ,CAST(JobMemo as varchar(4000));


                    Disclaimer: the performance will probably not be optimal






                    share|improve this answer















                    Another method without using pivot



                    CREATE TABLE dbo.Customer(Number int, [Name] varchar(255),Seq bit);
                    INSERT INTO dbo.Customer(Number,[Name],Seq)
                    VALUES
                    (12345,'Mickey Mouse',0),
                    (12345,'Minnie Mouse',1),
                    (45678,'Donald Duck',0),
                    (45678,'Daphney Duck',1),
                    (245678,'Pluto Dog',0);

                    SELECT Number,
                    MAX(CASE WHEN Seq = 0 THEN [Name] END) as Owner1,
                    MAX(CASE WHEN Seq = 1 Then [Name] END) AS Owner2
                    FROM dbo.Customer
                    GROUP BY Number;


                    Result



                    Number Owner1 Owner2
                    12345 Mickey Mouse Minnie Mouse
                    45678 Donald Duck Daphney Duck
                    245678 Pluto Dog NULL



                    Update: Issue with many columns



                    If the other columns mentioned in the comments are the same based on the number, you can add them to the group by, and cast the TEXT value as varchar(4000) (max 3640 datalength) Otherwise you would have to choose one of the two with MAX / MIN / ...



                    SELECT Number, 
                    MAX(CASE WHEN Seq = 0 THEN [Name] END) as Owner1,
                    MAX(CASE WHEN Seq = 1 Then [Name] END) AS Owner2
                    ,[Street1], [Street2], [City] , [State] , [Zipcode] , [HomePhone] ,CAST(JobMemo as varchar(4000))
                    FROM dbo.Debtors
                    GROUP BY Number,[Street1], [Street2], [City] , [State] , [Zipcode] , [HomePhone] ,CAST(JobMemo as varchar(4000));


                    Disclaimer: the performance will probably not be optimal







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Apr 16 at 20:41

























                    answered Apr 16 at 19:48









                    Randi VertongenRandi Vertongen

                    9,6813 gold badges12 silver badges34 bronze badges




                    9,6813 gold badges12 silver badges34 bronze badges















                    • OK thank you! So there's pretty much no way to run a simle query without creating a table first?

                      – Nicole Montez
                      Apr 16 at 19:58











                    • @NicoleMontez No problem, I am not sure what you mean by the 'without creating a table first'? I could have created a temp table aswell. As far as questions go, giving the sample data + table create statement really helps! We always have to create a table with the same sample data to show a valid resultset as an answer :).

                      – Randi Vertongen
                      Apr 16 at 20:03












                    • Error on Pivot table: Pivot grouping columns must be comparable. The type of column "JobMemo" is "text", which is not comparable. CREATE TABLE [dbo].[Debtors]( [Number] [int] NOT NULL, [Seq] [int] NULL, [Name] [varchar](30) NULL, [Street1] [varchar](30) NULL, [Street2] [varchar](30) NULL, [City] [varchar](30) NULL, [State] [varchar](3) NULL, [Zipcode] [varchar](10) NULL, [HomePhone] [varchar](30) NULL,

                      – Nicole Montez
                      Apr 16 at 20:07












                    • @NicoleMontez does the JobMemo need to be text? What does SELECT MAX(DATALENGTH(JobMemo)) from dbo.Debtors return? Maybe you could change it to varchar or nvarchar datatype? You could also cast it in the query. Could you add the entire table to the question?

                      – Randi Vertongen
                      Apr 16 at 20:27












                    • It's HUGE. I couldn't fit all of the characters. I'm sorry, this is my first question. I normally just wing it on my own. I inherited this table, and it's very invloved.

                      – Nicole Montez
                      Apr 16 at 20:32

















                    • OK thank you! So there's pretty much no way to run a simle query without creating a table first?

                      – Nicole Montez
                      Apr 16 at 19:58











                    • @NicoleMontez No problem, I am not sure what you mean by the 'without creating a table first'? I could have created a temp table aswell. As far as questions go, giving the sample data + table create statement really helps! We always have to create a table with the same sample data to show a valid resultset as an answer :).

                      – Randi Vertongen
                      Apr 16 at 20:03












                    • Error on Pivot table: Pivot grouping columns must be comparable. The type of column "JobMemo" is "text", which is not comparable. CREATE TABLE [dbo].[Debtors]( [Number] [int] NOT NULL, [Seq] [int] NULL, [Name] [varchar](30) NULL, [Street1] [varchar](30) NULL, [Street2] [varchar](30) NULL, [City] [varchar](30) NULL, [State] [varchar](3) NULL, [Zipcode] [varchar](10) NULL, [HomePhone] [varchar](30) NULL,

                      – Nicole Montez
                      Apr 16 at 20:07












                    • @NicoleMontez does the JobMemo need to be text? What does SELECT MAX(DATALENGTH(JobMemo)) from dbo.Debtors return? Maybe you could change it to varchar or nvarchar datatype? You could also cast it in the query. Could you add the entire table to the question?

                      – Randi Vertongen
                      Apr 16 at 20:27












                    • It's HUGE. I couldn't fit all of the characters. I'm sorry, this is my first question. I normally just wing it on my own. I inherited this table, and it's very invloved.

                      – Nicole Montez
                      Apr 16 at 20:32
















                    OK thank you! So there's pretty much no way to run a simle query without creating a table first?

                    – Nicole Montez
                    Apr 16 at 19:58





                    OK thank you! So there's pretty much no way to run a simle query without creating a table first?

                    – Nicole Montez
                    Apr 16 at 19:58













                    @NicoleMontez No problem, I am not sure what you mean by the 'without creating a table first'? I could have created a temp table aswell. As far as questions go, giving the sample data + table create statement really helps! We always have to create a table with the same sample data to show a valid resultset as an answer :).

                    – Randi Vertongen
                    Apr 16 at 20:03






                    @NicoleMontez No problem, I am not sure what you mean by the 'without creating a table first'? I could have created a temp table aswell. As far as questions go, giving the sample data + table create statement really helps! We always have to create a table with the same sample data to show a valid resultset as an answer :).

                    – Randi Vertongen
                    Apr 16 at 20:03














                    Error on Pivot table: Pivot grouping columns must be comparable. The type of column "JobMemo" is "text", which is not comparable. CREATE TABLE [dbo].[Debtors]( [Number] [int] NOT NULL, [Seq] [int] NULL, [Name] [varchar](30) NULL, [Street1] [varchar](30) NULL, [Street2] [varchar](30) NULL, [City] [varchar](30) NULL, [State] [varchar](3) NULL, [Zipcode] [varchar](10) NULL, [HomePhone] [varchar](30) NULL,

                    – Nicole Montez
                    Apr 16 at 20:07






                    Error on Pivot table: Pivot grouping columns must be comparable. The type of column "JobMemo" is "text", which is not comparable. CREATE TABLE [dbo].[Debtors]( [Number] [int] NOT NULL, [Seq] [int] NULL, [Name] [varchar](30) NULL, [Street1] [varchar](30) NULL, [Street2] [varchar](30) NULL, [City] [varchar](30) NULL, [State] [varchar](3) NULL, [Zipcode] [varchar](10) NULL, [HomePhone] [varchar](30) NULL,

                    – Nicole Montez
                    Apr 16 at 20:07














                    @NicoleMontez does the JobMemo need to be text? What does SELECT MAX(DATALENGTH(JobMemo)) from dbo.Debtors return? Maybe you could change it to varchar or nvarchar datatype? You could also cast it in the query. Could you add the entire table to the question?

                    – Randi Vertongen
                    Apr 16 at 20:27






                    @NicoleMontez does the JobMemo need to be text? What does SELECT MAX(DATALENGTH(JobMemo)) from dbo.Debtors return? Maybe you could change it to varchar or nvarchar datatype? You could also cast it in the query. Could you add the entire table to the question?

                    – Randi Vertongen
                    Apr 16 at 20:27














                    It's HUGE. I couldn't fit all of the characters. I'm sorry, this is my first question. I normally just wing it on my own. I inherited this table, and it's very invloved.

                    – Nicole Montez
                    Apr 16 at 20:32





                    It's HUGE. I couldn't fit all of the characters. I'm sorry, this is my first question. I normally just wing it on my own. I inherited this table, and it's very invloved.

                    – Nicole Montez
                    Apr 16 at 20:32


















                    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%2f234964%2fselecting-the-same-column-from-different-rows-based-on-different-criteria%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

                    Tamil (spriik) Luke uk diar | Nawigatjuun

                    Align equal signs while including text over equalitiesAMS align: left aligned text/math plus multicolumn alignmentMultiple alignmentsAligning equations in multiple placesNumbering and aligning an equation with multiple columnsHow to align one equation with another multline equationUsing \ in environments inside the begintabularxNumber equations and preserving alignment of equal signsHow can I align equations to the left and to the right?Double equation alignment problem within align enviromentAligned within align: Why are they right-aligned?

                    Where does the image of a data connector as a sharp metal spike originate from?Where does the concept of infected people turning into zombies only after death originate from?Where does the motif of a reanimated human head originate?Where did the notion that Dragons could speak originate?Where does the archetypal image of the 'Grey' alien come from?Where did the suffix '-Man' originate?Where does the notion of being injured or killed by an illusion originate?Where did the term “sophont” originate?Where does the trope of magic spells being driven by advanced technology originate from?Where did the term “the living impaired” originate?