How to get my required record with defined table





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















Hi my table structure is shown as below



enter image description here



and with sql query I want to make it as below structure format



enter image description here



I have to make this with single sql query.



Currently I had made this with excel feature
Can I get any suggestion?



Questionid  Response        Response
1 HighlyEngaged HighlyEngaged
2 VeryPrepared VeryPrepared
2 VeryPrepared1 VeryPrepared1

to

RowLabels Count of Response
1 1
HighlyEngaged 1
2 2
VeryPrepared 1
VeryPrepared1 1









share|improve this question

























  • Insted of posting pictures is better to copy/paste the files info. Could you do that?

    – jalazbe
    Nov 16 '18 at 12:00











  • @jalazbe i had updated please review

    – mitesh jain
    Nov 16 '18 at 12:17


















1















Hi my table structure is shown as below



enter image description here



and with sql query I want to make it as below structure format



enter image description here



I have to make this with single sql query.



Currently I had made this with excel feature
Can I get any suggestion?



Questionid  Response        Response
1 HighlyEngaged HighlyEngaged
2 VeryPrepared VeryPrepared
2 VeryPrepared1 VeryPrepared1

to

RowLabels Count of Response
1 1
HighlyEngaged 1
2 2
VeryPrepared 1
VeryPrepared1 1









share|improve this question

























  • Insted of posting pictures is better to copy/paste the files info. Could you do that?

    – jalazbe
    Nov 16 '18 at 12:00











  • @jalazbe i had updated please review

    – mitesh jain
    Nov 16 '18 at 12:17














1












1








1








Hi my table structure is shown as below



enter image description here



and with sql query I want to make it as below structure format



enter image description here



I have to make this with single sql query.



Currently I had made this with excel feature
Can I get any suggestion?



Questionid  Response        Response
1 HighlyEngaged HighlyEngaged
2 VeryPrepared VeryPrepared
2 VeryPrepared1 VeryPrepared1

to

RowLabels Count of Response
1 1
HighlyEngaged 1
2 2
VeryPrepared 1
VeryPrepared1 1









share|improve this question
















Hi my table structure is shown as below



enter image description here



and with sql query I want to make it as below structure format



enter image description here



I have to make this with single sql query.



Currently I had made this with excel feature
Can I get any suggestion?



Questionid  Response        Response
1 HighlyEngaged HighlyEngaged
2 VeryPrepared VeryPrepared
2 VeryPrepared1 VeryPrepared1

to

RowLabels Count of Response
1 1
HighlyEngaged 1
2 2
VeryPrepared 1
VeryPrepared1 1






sql sql-server sql-server-2012 sql-server-2008-r2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 12:16







mitesh jain

















asked Nov 16 '18 at 11:59









mitesh jainmitesh jain

356




356













  • Insted of posting pictures is better to copy/paste the files info. Could you do that?

    – jalazbe
    Nov 16 '18 at 12:00











  • @jalazbe i had updated please review

    – mitesh jain
    Nov 16 '18 at 12:17



















  • Insted of posting pictures is better to copy/paste the files info. Could you do that?

    – jalazbe
    Nov 16 '18 at 12:00











  • @jalazbe i had updated please review

    – mitesh jain
    Nov 16 '18 at 12:17

















Insted of posting pictures is better to copy/paste the files info. Could you do that?

– jalazbe
Nov 16 '18 at 12:00





Insted of posting pictures is better to copy/paste the files info. Could you do that?

– jalazbe
Nov 16 '18 at 12:00













@jalazbe i had updated please review

– mitesh jain
Nov 16 '18 at 12:17





@jalazbe i had updated please review

– mitesh jain
Nov 16 '18 at 12:17












3 Answers
3






active

oldest

votes


















1














I have prepared following query, I think it can help you :



DROP TABLE QA
GO
CREATE TABLE QA
(
Questionid INT
,Response VARCHAR(100)
)

INSERT INTO QA
VALUES(1,'HighlyEngaged')
,(2,'VeryPrepared' )
,(5,'Asked' )
,(5,'Priority' )
,(5,'Explained' )
,(8,'Yes' )
,(9,'Set Agenda' )
,(9,'Take Atten' )
,(11,'Assigned')
,(11,'Individual')
,(12,'Predict')
,(12,'Questions')

SELECT
CASE
WHEN Response = '' THEN CAST(QuestionId AS VARCHAR)
ELSE ''
END QId
,Response
,ResponseTotal
FROM (SELECT
QuestionId
,'' Response
,COUNT(Response) ResponseTotal
FROM QA
GROUP BY QuestionId
UNION ALL
SELECT
QuestionId
,Response
,COUNT(1)
FROM QA
GROUP BY QuestionId
,Response) a
ORDER BY QuestionId, CASE
WHEN Response = '' THEN 0
ELSE 1
END





share|improve this answer
























  • This looks promising..

    – kirtan
    Nov 16 '18 at 13:33



















1














drop table #teee
CREATE TABLE #teee
([Questionid] int, [Response] varchar(13), [Response1] varchar(13))
;

INSERT INTO #teee
([Questionid], [Response], [Response1])
VALUES
(1, 'HighlyEngaged', 'HighlyEngaged'),
(2, 'VeryPrepared', 'VeryPrepared'),
(2, 'VeryPrepared1', 'VeryPrepared1')
;

select res,cnt from (select [Questionid],cast([Questionid]as varchar(100)) res ,count([Response]) cnt from #teee
group by [Questionid]
union all
select [Questionid],cast([Response]as varchar(100)) res,count([Response]) r1 from #teee
group by [Questionid],[Response])a
order by [Questionid],res


the following is an update for the answer given by Yogesh Sharma



select isnull([Response],[Questionid]),total from (select [Questionid], [Response], count(*) total
from #teee t
group by [Questionid], [Response] with rollup) a
where isnull([Response],[Questionid]) is not null
order by [Questionid],1





share|improve this answer

































    0














    You can use roll up with aggregation :



    select questionid, Response, count(*)
    from table t
    group by questionid, Response with roll up;





    share|improve this answer
























    • No its not populating the record as I needed, and I need only 2 rows i.e. Row Label and Count of Response

      – mitesh jain
      Nov 16 '18 at 12:11













    • @miteshjain. . . I know that's not same, but you should know excel <> SQL Server. SQL Server is Database tool not a data presentation tool.

      – Yogesh Sharma
      Nov 16 '18 at 12:13











    • please review my updated question

      – mitesh jain
      Nov 16 '18 at 12:17












    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',
    autoActivateHeartbeat: false,
    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%2f53337485%2fhow-to-get-my-required-record-with-defined-table%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    I have prepared following query, I think it can help you :



    DROP TABLE QA
    GO
    CREATE TABLE QA
    (
    Questionid INT
    ,Response VARCHAR(100)
    )

    INSERT INTO QA
    VALUES(1,'HighlyEngaged')
    ,(2,'VeryPrepared' )
    ,(5,'Asked' )
    ,(5,'Priority' )
    ,(5,'Explained' )
    ,(8,'Yes' )
    ,(9,'Set Agenda' )
    ,(9,'Take Atten' )
    ,(11,'Assigned')
    ,(11,'Individual')
    ,(12,'Predict')
    ,(12,'Questions')

    SELECT
    CASE
    WHEN Response = '' THEN CAST(QuestionId AS VARCHAR)
    ELSE ''
    END QId
    ,Response
    ,ResponseTotal
    FROM (SELECT
    QuestionId
    ,'' Response
    ,COUNT(Response) ResponseTotal
    FROM QA
    GROUP BY QuestionId
    UNION ALL
    SELECT
    QuestionId
    ,Response
    ,COUNT(1)
    FROM QA
    GROUP BY QuestionId
    ,Response) a
    ORDER BY QuestionId, CASE
    WHEN Response = '' THEN 0
    ELSE 1
    END





    share|improve this answer
























    • This looks promising..

      – kirtan
      Nov 16 '18 at 13:33
















    1














    I have prepared following query, I think it can help you :



    DROP TABLE QA
    GO
    CREATE TABLE QA
    (
    Questionid INT
    ,Response VARCHAR(100)
    )

    INSERT INTO QA
    VALUES(1,'HighlyEngaged')
    ,(2,'VeryPrepared' )
    ,(5,'Asked' )
    ,(5,'Priority' )
    ,(5,'Explained' )
    ,(8,'Yes' )
    ,(9,'Set Agenda' )
    ,(9,'Take Atten' )
    ,(11,'Assigned')
    ,(11,'Individual')
    ,(12,'Predict')
    ,(12,'Questions')

    SELECT
    CASE
    WHEN Response = '' THEN CAST(QuestionId AS VARCHAR)
    ELSE ''
    END QId
    ,Response
    ,ResponseTotal
    FROM (SELECT
    QuestionId
    ,'' Response
    ,COUNT(Response) ResponseTotal
    FROM QA
    GROUP BY QuestionId
    UNION ALL
    SELECT
    QuestionId
    ,Response
    ,COUNT(1)
    FROM QA
    GROUP BY QuestionId
    ,Response) a
    ORDER BY QuestionId, CASE
    WHEN Response = '' THEN 0
    ELSE 1
    END





    share|improve this answer
























    • This looks promising..

      – kirtan
      Nov 16 '18 at 13:33














    1












    1








    1







    I have prepared following query, I think it can help you :



    DROP TABLE QA
    GO
    CREATE TABLE QA
    (
    Questionid INT
    ,Response VARCHAR(100)
    )

    INSERT INTO QA
    VALUES(1,'HighlyEngaged')
    ,(2,'VeryPrepared' )
    ,(5,'Asked' )
    ,(5,'Priority' )
    ,(5,'Explained' )
    ,(8,'Yes' )
    ,(9,'Set Agenda' )
    ,(9,'Take Atten' )
    ,(11,'Assigned')
    ,(11,'Individual')
    ,(12,'Predict')
    ,(12,'Questions')

    SELECT
    CASE
    WHEN Response = '' THEN CAST(QuestionId AS VARCHAR)
    ELSE ''
    END QId
    ,Response
    ,ResponseTotal
    FROM (SELECT
    QuestionId
    ,'' Response
    ,COUNT(Response) ResponseTotal
    FROM QA
    GROUP BY QuestionId
    UNION ALL
    SELECT
    QuestionId
    ,Response
    ,COUNT(1)
    FROM QA
    GROUP BY QuestionId
    ,Response) a
    ORDER BY QuestionId, CASE
    WHEN Response = '' THEN 0
    ELSE 1
    END





    share|improve this answer













    I have prepared following query, I think it can help you :



    DROP TABLE QA
    GO
    CREATE TABLE QA
    (
    Questionid INT
    ,Response VARCHAR(100)
    )

    INSERT INTO QA
    VALUES(1,'HighlyEngaged')
    ,(2,'VeryPrepared' )
    ,(5,'Asked' )
    ,(5,'Priority' )
    ,(5,'Explained' )
    ,(8,'Yes' )
    ,(9,'Set Agenda' )
    ,(9,'Take Atten' )
    ,(11,'Assigned')
    ,(11,'Individual')
    ,(12,'Predict')
    ,(12,'Questions')

    SELECT
    CASE
    WHEN Response = '' THEN CAST(QuestionId AS VARCHAR)
    ELSE ''
    END QId
    ,Response
    ,ResponseTotal
    FROM (SELECT
    QuestionId
    ,'' Response
    ,COUNT(Response) ResponseTotal
    FROM QA
    GROUP BY QuestionId
    UNION ALL
    SELECT
    QuestionId
    ,Response
    ,COUNT(1)
    FROM QA
    GROUP BY QuestionId
    ,Response) a
    ORDER BY QuestionId, CASE
    WHEN Response = '' THEN 0
    ELSE 1
    END






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 16 '18 at 13:17









    Zeki GumusZeki Gumus

    1,445313




    1,445313













    • This looks promising..

      – kirtan
      Nov 16 '18 at 13:33



















    • This looks promising..

      – kirtan
      Nov 16 '18 at 13:33

















    This looks promising..

    – kirtan
    Nov 16 '18 at 13:33





    This looks promising..

    – kirtan
    Nov 16 '18 at 13:33













    1














    drop table #teee
    CREATE TABLE #teee
    ([Questionid] int, [Response] varchar(13), [Response1] varchar(13))
    ;

    INSERT INTO #teee
    ([Questionid], [Response], [Response1])
    VALUES
    (1, 'HighlyEngaged', 'HighlyEngaged'),
    (2, 'VeryPrepared', 'VeryPrepared'),
    (2, 'VeryPrepared1', 'VeryPrepared1')
    ;

    select res,cnt from (select [Questionid],cast([Questionid]as varchar(100)) res ,count([Response]) cnt from #teee
    group by [Questionid]
    union all
    select [Questionid],cast([Response]as varchar(100)) res,count([Response]) r1 from #teee
    group by [Questionid],[Response])a
    order by [Questionid],res


    the following is an update for the answer given by Yogesh Sharma



    select isnull([Response],[Questionid]),total from (select [Questionid], [Response], count(*) total
    from #teee t
    group by [Questionid], [Response] with rollup) a
    where isnull([Response],[Questionid]) is not null
    order by [Questionid],1





    share|improve this answer






























      1














      drop table #teee
      CREATE TABLE #teee
      ([Questionid] int, [Response] varchar(13), [Response1] varchar(13))
      ;

      INSERT INTO #teee
      ([Questionid], [Response], [Response1])
      VALUES
      (1, 'HighlyEngaged', 'HighlyEngaged'),
      (2, 'VeryPrepared', 'VeryPrepared'),
      (2, 'VeryPrepared1', 'VeryPrepared1')
      ;

      select res,cnt from (select [Questionid],cast([Questionid]as varchar(100)) res ,count([Response]) cnt from #teee
      group by [Questionid]
      union all
      select [Questionid],cast([Response]as varchar(100)) res,count([Response]) r1 from #teee
      group by [Questionid],[Response])a
      order by [Questionid],res


      the following is an update for the answer given by Yogesh Sharma



      select isnull([Response],[Questionid]),total from (select [Questionid], [Response], count(*) total
      from #teee t
      group by [Questionid], [Response] with rollup) a
      where isnull([Response],[Questionid]) is not null
      order by [Questionid],1





      share|improve this answer




























        1












        1








        1







        drop table #teee
        CREATE TABLE #teee
        ([Questionid] int, [Response] varchar(13), [Response1] varchar(13))
        ;

        INSERT INTO #teee
        ([Questionid], [Response], [Response1])
        VALUES
        (1, 'HighlyEngaged', 'HighlyEngaged'),
        (2, 'VeryPrepared', 'VeryPrepared'),
        (2, 'VeryPrepared1', 'VeryPrepared1')
        ;

        select res,cnt from (select [Questionid],cast([Questionid]as varchar(100)) res ,count([Response]) cnt from #teee
        group by [Questionid]
        union all
        select [Questionid],cast([Response]as varchar(100)) res,count([Response]) r1 from #teee
        group by [Questionid],[Response])a
        order by [Questionid],res


        the following is an update for the answer given by Yogesh Sharma



        select isnull([Response],[Questionid]),total from (select [Questionid], [Response], count(*) total
        from #teee t
        group by [Questionid], [Response] with rollup) a
        where isnull([Response],[Questionid]) is not null
        order by [Questionid],1





        share|improve this answer















        drop table #teee
        CREATE TABLE #teee
        ([Questionid] int, [Response] varchar(13), [Response1] varchar(13))
        ;

        INSERT INTO #teee
        ([Questionid], [Response], [Response1])
        VALUES
        (1, 'HighlyEngaged', 'HighlyEngaged'),
        (2, 'VeryPrepared', 'VeryPrepared'),
        (2, 'VeryPrepared1', 'VeryPrepared1')
        ;

        select res,cnt from (select [Questionid],cast([Questionid]as varchar(100)) res ,count([Response]) cnt from #teee
        group by [Questionid]
        union all
        select [Questionid],cast([Response]as varchar(100)) res,count([Response]) r1 from #teee
        group by [Questionid],[Response])a
        order by [Questionid],res


        the following is an update for the answer given by Yogesh Sharma



        select isnull([Response],[Questionid]),total from (select [Questionid], [Response], count(*) total
        from #teee t
        group by [Questionid], [Response] with rollup) a
        where isnull([Response],[Questionid]) is not null
        order by [Questionid],1






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 16 '18 at 13:14

























        answered Nov 16 '18 at 12:55









        Smart003Smart003

        7171923




        7171923























            0














            You can use roll up with aggregation :



            select questionid, Response, count(*)
            from table t
            group by questionid, Response with roll up;





            share|improve this answer
























            • No its not populating the record as I needed, and I need only 2 rows i.e. Row Label and Count of Response

              – mitesh jain
              Nov 16 '18 at 12:11













            • @miteshjain. . . I know that's not same, but you should know excel <> SQL Server. SQL Server is Database tool not a data presentation tool.

              – Yogesh Sharma
              Nov 16 '18 at 12:13











            • please review my updated question

              – mitesh jain
              Nov 16 '18 at 12:17
















            0














            You can use roll up with aggregation :



            select questionid, Response, count(*)
            from table t
            group by questionid, Response with roll up;





            share|improve this answer
























            • No its not populating the record as I needed, and I need only 2 rows i.e. Row Label and Count of Response

              – mitesh jain
              Nov 16 '18 at 12:11













            • @miteshjain. . . I know that's not same, but you should know excel <> SQL Server. SQL Server is Database tool not a data presentation tool.

              – Yogesh Sharma
              Nov 16 '18 at 12:13











            • please review my updated question

              – mitesh jain
              Nov 16 '18 at 12:17














            0












            0








            0







            You can use roll up with aggregation :



            select questionid, Response, count(*)
            from table t
            group by questionid, Response with roll up;





            share|improve this answer













            You can use roll up with aggregation :



            select questionid, Response, count(*)
            from table t
            group by questionid, Response with roll up;






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 16 '18 at 12:02









            Yogesh SharmaYogesh Sharma

            34.6k51440




            34.6k51440













            • No its not populating the record as I needed, and I need only 2 rows i.e. Row Label and Count of Response

              – mitesh jain
              Nov 16 '18 at 12:11













            • @miteshjain. . . I know that's not same, but you should know excel <> SQL Server. SQL Server is Database tool not a data presentation tool.

              – Yogesh Sharma
              Nov 16 '18 at 12:13











            • please review my updated question

              – mitesh jain
              Nov 16 '18 at 12:17



















            • No its not populating the record as I needed, and I need only 2 rows i.e. Row Label and Count of Response

              – mitesh jain
              Nov 16 '18 at 12:11













            • @miteshjain. . . I know that's not same, but you should know excel <> SQL Server. SQL Server is Database tool not a data presentation tool.

              – Yogesh Sharma
              Nov 16 '18 at 12:13











            • please review my updated question

              – mitesh jain
              Nov 16 '18 at 12:17

















            No its not populating the record as I needed, and I need only 2 rows i.e. Row Label and Count of Response

            – mitesh jain
            Nov 16 '18 at 12:11







            No its not populating the record as I needed, and I need only 2 rows i.e. Row Label and Count of Response

            – mitesh jain
            Nov 16 '18 at 12:11















            @miteshjain. . . I know that's not same, but you should know excel <> SQL Server. SQL Server is Database tool not a data presentation tool.

            – Yogesh Sharma
            Nov 16 '18 at 12:13





            @miteshjain. . . I know that's not same, but you should know excel <> SQL Server. SQL Server is Database tool not a data presentation tool.

            – Yogesh Sharma
            Nov 16 '18 at 12:13













            please review my updated question

            – mitesh jain
            Nov 16 '18 at 12:17





            please review my updated question

            – mitesh jain
            Nov 16 '18 at 12:17


















            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53337485%2fhow-to-get-my-required-record-with-defined-table%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

            Florida Star v. B. J. F.

            Error while running script in elastic search , gateway timeout

            Adding quotations to stringified JSON object values