How to combine monthly results into a single row?









up vote
0
down vote

favorite












If have a sales table with data like:



SALES
-----
seller_id month amount
1 1 10
1 2 15


I would like to retrieve all seller's sales in a single row. How can I combine the results from the table into a result like this?



seller_id jan_amount feb_amount 
1 10 15


EDIT:



I can retrieve the monthly sales with this query:



select seller_id, month, sum(amount)
from sales
group by sellerd_id, month


But it gives me results like:



1,1,10
1,2,15


I'm hoping to transform this into:



1, 10 as JAN_AMOUNT, 15 as FEB_AMONT









share|improve this question























  • What have you tried? Did you try to get the sum by seller and month first? Thats a two step process what do you have? What SQL are you using?
    – Brad
    Nov 9 at 19:23










  • Do you try something like this "SELECT SUM(amount) AS total_amount FROM SALES WHERE seller_id = 1"
    – Bast
    Nov 9 at 19:25











  • @Brad I added the sql I've tried, I hadn't included it initially because it doesn't produce the result I'm looking for
    – ab11
    Nov 9 at 19:28






  • 1




    You need to look into PIVOT
    – Brad
    Nov 9 at 19:32






  • 1




    Have you ever heard of PIVOT?
    – Eric
    Nov 9 at 19:36














up vote
0
down vote

favorite












If have a sales table with data like:



SALES
-----
seller_id month amount
1 1 10
1 2 15


I would like to retrieve all seller's sales in a single row. How can I combine the results from the table into a result like this?



seller_id jan_amount feb_amount 
1 10 15


EDIT:



I can retrieve the monthly sales with this query:



select seller_id, month, sum(amount)
from sales
group by sellerd_id, month


But it gives me results like:



1,1,10
1,2,15


I'm hoping to transform this into:



1, 10 as JAN_AMOUNT, 15 as FEB_AMONT









share|improve this question























  • What have you tried? Did you try to get the sum by seller and month first? Thats a two step process what do you have? What SQL are you using?
    – Brad
    Nov 9 at 19:23










  • Do you try something like this "SELECT SUM(amount) AS total_amount FROM SALES WHERE seller_id = 1"
    – Bast
    Nov 9 at 19:25











  • @Brad I added the sql I've tried, I hadn't included it initially because it doesn't produce the result I'm looking for
    – ab11
    Nov 9 at 19:28






  • 1




    You need to look into PIVOT
    – Brad
    Nov 9 at 19:32






  • 1




    Have you ever heard of PIVOT?
    – Eric
    Nov 9 at 19:36












up vote
0
down vote

favorite









up vote
0
down vote

favorite











If have a sales table with data like:



SALES
-----
seller_id month amount
1 1 10
1 2 15


I would like to retrieve all seller's sales in a single row. How can I combine the results from the table into a result like this?



seller_id jan_amount feb_amount 
1 10 15


EDIT:



I can retrieve the monthly sales with this query:



select seller_id, month, sum(amount)
from sales
group by sellerd_id, month


But it gives me results like:



1,1,10
1,2,15


I'm hoping to transform this into:



1, 10 as JAN_AMOUNT, 15 as FEB_AMONT









share|improve this question















If have a sales table with data like:



SALES
-----
seller_id month amount
1 1 10
1 2 15


I would like to retrieve all seller's sales in a single row. How can I combine the results from the table into a result like this?



seller_id jan_amount feb_amount 
1 10 15


EDIT:



I can retrieve the monthly sales with this query:



select seller_id, month, sum(amount)
from sales
group by sellerd_id, month


But it gives me results like:



1,1,10
1,2,15


I'm hoping to transform this into:



1, 10 as JAN_AMOUNT, 15 as FEB_AMONT






sql oracle pivot






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 20:00









Barbaros Özhan

11k71430




11k71430










asked Nov 9 at 19:20









ab11

9,0583286171




9,0583286171











  • What have you tried? Did you try to get the sum by seller and month first? Thats a two step process what do you have? What SQL are you using?
    – Brad
    Nov 9 at 19:23










  • Do you try something like this "SELECT SUM(amount) AS total_amount FROM SALES WHERE seller_id = 1"
    – Bast
    Nov 9 at 19:25











  • @Brad I added the sql I've tried, I hadn't included it initially because it doesn't produce the result I'm looking for
    – ab11
    Nov 9 at 19:28






  • 1




    You need to look into PIVOT
    – Brad
    Nov 9 at 19:32






  • 1




    Have you ever heard of PIVOT?
    – Eric
    Nov 9 at 19:36
















  • What have you tried? Did you try to get the sum by seller and month first? Thats a two step process what do you have? What SQL are you using?
    – Brad
    Nov 9 at 19:23










  • Do you try something like this "SELECT SUM(amount) AS total_amount FROM SALES WHERE seller_id = 1"
    – Bast
    Nov 9 at 19:25











  • @Brad I added the sql I've tried, I hadn't included it initially because it doesn't produce the result I'm looking for
    – ab11
    Nov 9 at 19:28






  • 1




    You need to look into PIVOT
    – Brad
    Nov 9 at 19:32






  • 1




    Have you ever heard of PIVOT?
    – Eric
    Nov 9 at 19:36















What have you tried? Did you try to get the sum by seller and month first? Thats a two step process what do you have? What SQL are you using?
– Brad
Nov 9 at 19:23




What have you tried? Did you try to get the sum by seller and month first? Thats a two step process what do you have? What SQL are you using?
– Brad
Nov 9 at 19:23












Do you try something like this "SELECT SUM(amount) AS total_amount FROM SALES WHERE seller_id = 1"
– Bast
Nov 9 at 19:25





Do you try something like this "SELECT SUM(amount) AS total_amount FROM SALES WHERE seller_id = 1"
– Bast
Nov 9 at 19:25













@Brad I added the sql I've tried, I hadn't included it initially because it doesn't produce the result I'm looking for
– ab11
Nov 9 at 19:28




@Brad I added the sql I've tried, I hadn't included it initially because it doesn't produce the result I'm looking for
– ab11
Nov 9 at 19:28




1




1




You need to look into PIVOT
– Brad
Nov 9 at 19:32




You need to look into PIVOT
– Brad
Nov 9 at 19:32




1




1




Have you ever heard of PIVOT?
– Eric
Nov 9 at 19:36




Have you ever heard of PIVOT?
– Eric
Nov 9 at 19:36












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










You may use conditional aggregation to pivot your data with the following SQL statement as :



select seller_id,
max(case when month = 1 then amount end ) as jan_amount,
max(case when month = 2 then amount end ) as feb_amount
from sales
where seller_id = 1
group by seller_id;

SELLER_ID JAN_AMOUNT FEB_AMOUNT
--------- ---------- ----------
1 10 15


As an Oracle user, you may use the following SQL statement with pivot keyword as :



select *
from sales
pivot(
sum(amount)
for(month)
in (1 as jan_amount ,2 as feb_amount )
);

SELLER_ID JAN_AMOUNT FEB_AMOUNT
--------- ---------- ----------
1 10 15


Rextester Demo






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%2f53232055%2fhow-to-combine-monthly-results-into-a-single-row%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








    up vote
    2
    down vote



    accepted










    You may use conditional aggregation to pivot your data with the following SQL statement as :



    select seller_id,
    max(case when month = 1 then amount end ) as jan_amount,
    max(case when month = 2 then amount end ) as feb_amount
    from sales
    where seller_id = 1
    group by seller_id;

    SELLER_ID JAN_AMOUNT FEB_AMOUNT
    --------- ---------- ----------
    1 10 15


    As an Oracle user, you may use the following SQL statement with pivot keyword as :



    select *
    from sales
    pivot(
    sum(amount)
    for(month)
    in (1 as jan_amount ,2 as feb_amount )
    );

    SELLER_ID JAN_AMOUNT FEB_AMOUNT
    --------- ---------- ----------
    1 10 15


    Rextester Demo






    share|improve this answer


























      up vote
      2
      down vote



      accepted










      You may use conditional aggregation to pivot your data with the following SQL statement as :



      select seller_id,
      max(case when month = 1 then amount end ) as jan_amount,
      max(case when month = 2 then amount end ) as feb_amount
      from sales
      where seller_id = 1
      group by seller_id;

      SELLER_ID JAN_AMOUNT FEB_AMOUNT
      --------- ---------- ----------
      1 10 15


      As an Oracle user, you may use the following SQL statement with pivot keyword as :



      select *
      from sales
      pivot(
      sum(amount)
      for(month)
      in (1 as jan_amount ,2 as feb_amount )
      );

      SELLER_ID JAN_AMOUNT FEB_AMOUNT
      --------- ---------- ----------
      1 10 15


      Rextester Demo






      share|improve this answer
























        up vote
        2
        down vote



        accepted







        up vote
        2
        down vote



        accepted






        You may use conditional aggregation to pivot your data with the following SQL statement as :



        select seller_id,
        max(case when month = 1 then amount end ) as jan_amount,
        max(case when month = 2 then amount end ) as feb_amount
        from sales
        where seller_id = 1
        group by seller_id;

        SELLER_ID JAN_AMOUNT FEB_AMOUNT
        --------- ---------- ----------
        1 10 15


        As an Oracle user, you may use the following SQL statement with pivot keyword as :



        select *
        from sales
        pivot(
        sum(amount)
        for(month)
        in (1 as jan_amount ,2 as feb_amount )
        );

        SELLER_ID JAN_AMOUNT FEB_AMOUNT
        --------- ---------- ----------
        1 10 15


        Rextester Demo






        share|improve this answer














        You may use conditional aggregation to pivot your data with the following SQL statement as :



        select seller_id,
        max(case when month = 1 then amount end ) as jan_amount,
        max(case when month = 2 then amount end ) as feb_amount
        from sales
        where seller_id = 1
        group by seller_id;

        SELLER_ID JAN_AMOUNT FEB_AMOUNT
        --------- ---------- ----------
        1 10 15


        As an Oracle user, you may use the following SQL statement with pivot keyword as :



        select *
        from sales
        pivot(
        sum(amount)
        for(month)
        in (1 as jan_amount ,2 as feb_amount )
        );

        SELLER_ID JAN_AMOUNT FEB_AMOUNT
        --------- ---------- ----------
        1 10 15


        Rextester Demo







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 10 at 8:57

























        answered Nov 9 at 19:27









        Barbaros Özhan

        11k71430




        11k71430



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232055%2fhow-to-combine-monthly-results-into-a-single-row%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

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo