sql oracle group by subquery












0















I get the same ecommerce number for each date. I am trying to get ecommerce value count depending on the date, which is different for each date as the total number is only 105 for all October, not 391958.
Any idea how to group by the output of a subquery?
Thank you!



SELECT   to_char(wcs1.start_tms,'DD/MM/YYYY') as dates,
(
SELECT count(*)
FROM ft_t_wcs1 wcs1,ft_t_stup stup
WHERE stup.modl_id='ECOMMERC'
AND stup.CROSS_REF_ID=wcs1.acct_id
AND stup.end_tms IS NULL
) AS ecommerce
FROM ft_t_wcs1 wcs1, ft_t_stup stup
WHERE wcs1.scenario='CREATE'
AND wcs1.acct_id IS NOT NULL
AND wcs1.start_tms BETWEEN add_months(TRUNC(SYSDATE,'mm'),-1) AND LAST_DAY(add_months(TRUNC(SYSDATE,'mm'),-1))
GROUP BY to_char(wcs1.start_tms,'DD/MM/YYYY')
ORDER BY to_char(wcs1.start_tms,'DD/MM/YYYY');


OUTPUT
enter image description here










share|improve this question





























    0















    I get the same ecommerce number for each date. I am trying to get ecommerce value count depending on the date, which is different for each date as the total number is only 105 for all October, not 391958.
    Any idea how to group by the output of a subquery?
    Thank you!



    SELECT   to_char(wcs1.start_tms,'DD/MM/YYYY') as dates,
    (
    SELECT count(*)
    FROM ft_t_wcs1 wcs1,ft_t_stup stup
    WHERE stup.modl_id='ECOMMERC'
    AND stup.CROSS_REF_ID=wcs1.acct_id
    AND stup.end_tms IS NULL
    ) AS ecommerce
    FROM ft_t_wcs1 wcs1, ft_t_stup stup
    WHERE wcs1.scenario='CREATE'
    AND wcs1.acct_id IS NOT NULL
    AND wcs1.start_tms BETWEEN add_months(TRUNC(SYSDATE,'mm'),-1) AND LAST_DAY(add_months(TRUNC(SYSDATE,'mm'),-1))
    GROUP BY to_char(wcs1.start_tms,'DD/MM/YYYY')
    ORDER BY to_char(wcs1.start_tms,'DD/MM/YYYY');


    OUTPUT
    enter image description here










    share|improve this question



























      0












      0








      0








      I get the same ecommerce number for each date. I am trying to get ecommerce value count depending on the date, which is different for each date as the total number is only 105 for all October, not 391958.
      Any idea how to group by the output of a subquery?
      Thank you!



      SELECT   to_char(wcs1.start_tms,'DD/MM/YYYY') as dates,
      (
      SELECT count(*)
      FROM ft_t_wcs1 wcs1,ft_t_stup stup
      WHERE stup.modl_id='ECOMMERC'
      AND stup.CROSS_REF_ID=wcs1.acct_id
      AND stup.end_tms IS NULL
      ) AS ecommerce
      FROM ft_t_wcs1 wcs1, ft_t_stup stup
      WHERE wcs1.scenario='CREATE'
      AND wcs1.acct_id IS NOT NULL
      AND wcs1.start_tms BETWEEN add_months(TRUNC(SYSDATE,'mm'),-1) AND LAST_DAY(add_months(TRUNC(SYSDATE,'mm'),-1))
      GROUP BY to_char(wcs1.start_tms,'DD/MM/YYYY')
      ORDER BY to_char(wcs1.start_tms,'DD/MM/YYYY');


      OUTPUT
      enter image description here










      share|improve this question
















      I get the same ecommerce number for each date. I am trying to get ecommerce value count depending on the date, which is different for each date as the total number is only 105 for all October, not 391958.
      Any idea how to group by the output of a subquery?
      Thank you!



      SELECT   to_char(wcs1.start_tms,'DD/MM/YYYY') as dates,
      (
      SELECT count(*)
      FROM ft_t_wcs1 wcs1,ft_t_stup stup
      WHERE stup.modl_id='ECOMMERC'
      AND stup.CROSS_REF_ID=wcs1.acct_id
      AND stup.end_tms IS NULL
      ) AS ecommerce
      FROM ft_t_wcs1 wcs1, ft_t_stup stup
      WHERE wcs1.scenario='CREATE'
      AND wcs1.acct_id IS NOT NULL
      AND wcs1.start_tms BETWEEN add_months(TRUNC(SYSDATE,'mm'),-1) AND LAST_DAY(add_months(TRUNC(SYSDATE,'mm'),-1))
      GROUP BY to_char(wcs1.start_tms,'DD/MM/YYYY')
      ORDER BY to_char(wcs1.start_tms,'DD/MM/YYYY');


      OUTPUT
      enter image description here







      sql oracle correlated-subquery






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 19:44









      msturek

      491514




      491514










      asked Nov 14 '18 at 18:00









      marialaustsenmarialaustsen

      186




      186
























          3 Answers
          3






          active

          oldest

          votes


















          0














          Try below modified queries



          select to_char(wcs1.start_tms,'DD/MM/YYYY') as dates,count(*) AS 
          ecommerce
          from ft_t_wcs1 wcs1, ft_t_stup stup
          where stup.modl_id='ECOMMERC' and stup.CROSS_REF_ID=wcs1.acct_id and stup.end_tms is null wcs1.scenario='CREATE' and wcs1.acct_id is not null and
          wcs1.start_tms between add_months(TRUNC(SYSDATE,'mm'),-1) and
          LAST_DAY(add_months(TRUNC(SYSDATE,'mm'),-1))
          group by to_char(wcs1.start_tms,'DD/MM/YYYY')
          order by to_char(wcs1.start_tms,'DD/MM/YYYY');


          -- Another way using JOIN clause



          select to_char(wcs1.start_tms,'DD/MM/YYYY') as dates,count(*) AS 
          ecommerce
          from ft_t_wcs1 wcs1
          join ft_t_stup stup
          ON stup.CROSS_REF_ID=wcs1.acct_id
          where stup.modl_id='ECOMMERC' and stup.end_tms is null wcs1.scenario='CREATE' and wcs1.acct_id is not null and
          wcs1.start_tms between add_months(TRUNC(SYSDATE,'mm'),-1) and
          LAST_DAY(add_months(TRUNC(SYSDATE,'mm'),-1))
          group by to_char(wcs1.start_tms,'DD/MM/YYYY')
          order by to_char(wcs1.start_tms,'DD/MM/YYYY');





          share|improve this answer































            0














            It's hard to suggest an answer without understanding your table relationship, but I can tell that your problem is there is no relationship between your subquery and your main query. Your subquery simply returns a count where modl_id='ECOMMERC', so that value will always be the same - in your case, 105. You need to add a JOIN criteria to the subquery that ties the unique value to your main query. You'll also want to alias the table names differently to ensure you're joining correctly.






            share|improve this answer































              0














              You are doing unnecessary joins when you just want a correlated subquery:



              SELECT to_char(wcs1.start_tms,'DD/MM/YYYY') as dates,
              (SELECT count(*)
              FROM ft_t_stup stup
              WHERE stup.modl_id= 'ECOMMERC' AND
              stup.CROSS_REF_ID = wcs1.acct_id
              stup.end_tms IS NULL
              ) AS ecommerce
              FROM ft_t_wcs1 wcs1
              WHERE wcs1.scenario = 'CREATE' AND
              wcs1.acct_id IS NOT NULL AND
              wcs1.start_tms BETWEEN add_months(TRUNC(SYSDATE,'mm'),-1) AND LAST_DAY(add_months(TRUNC(SYSDATE,'mm'),-1))
              GROUP BY to_char(wcs1.start_tms, 'DD/MM/YYYY')
              ORDER BY to_char(wcs1.start_tms, 'DD/MM/YYYY');





              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',
                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%2f53306247%2fsql-oracle-group-by-subquery%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









                0














                Try below modified queries



                select to_char(wcs1.start_tms,'DD/MM/YYYY') as dates,count(*) AS 
                ecommerce
                from ft_t_wcs1 wcs1, ft_t_stup stup
                where stup.modl_id='ECOMMERC' and stup.CROSS_REF_ID=wcs1.acct_id and stup.end_tms is null wcs1.scenario='CREATE' and wcs1.acct_id is not null and
                wcs1.start_tms between add_months(TRUNC(SYSDATE,'mm'),-1) and
                LAST_DAY(add_months(TRUNC(SYSDATE,'mm'),-1))
                group by to_char(wcs1.start_tms,'DD/MM/YYYY')
                order by to_char(wcs1.start_tms,'DD/MM/YYYY');


                -- Another way using JOIN clause



                select to_char(wcs1.start_tms,'DD/MM/YYYY') as dates,count(*) AS 
                ecommerce
                from ft_t_wcs1 wcs1
                join ft_t_stup stup
                ON stup.CROSS_REF_ID=wcs1.acct_id
                where stup.modl_id='ECOMMERC' and stup.end_tms is null wcs1.scenario='CREATE' and wcs1.acct_id is not null and
                wcs1.start_tms between add_months(TRUNC(SYSDATE,'mm'),-1) and
                LAST_DAY(add_months(TRUNC(SYSDATE,'mm'),-1))
                group by to_char(wcs1.start_tms,'DD/MM/YYYY')
                order by to_char(wcs1.start_tms,'DD/MM/YYYY');





                share|improve this answer




























                  0














                  Try below modified queries



                  select to_char(wcs1.start_tms,'DD/MM/YYYY') as dates,count(*) AS 
                  ecommerce
                  from ft_t_wcs1 wcs1, ft_t_stup stup
                  where stup.modl_id='ECOMMERC' and stup.CROSS_REF_ID=wcs1.acct_id and stup.end_tms is null wcs1.scenario='CREATE' and wcs1.acct_id is not null and
                  wcs1.start_tms between add_months(TRUNC(SYSDATE,'mm'),-1) and
                  LAST_DAY(add_months(TRUNC(SYSDATE,'mm'),-1))
                  group by to_char(wcs1.start_tms,'DD/MM/YYYY')
                  order by to_char(wcs1.start_tms,'DD/MM/YYYY');


                  -- Another way using JOIN clause



                  select to_char(wcs1.start_tms,'DD/MM/YYYY') as dates,count(*) AS 
                  ecommerce
                  from ft_t_wcs1 wcs1
                  join ft_t_stup stup
                  ON stup.CROSS_REF_ID=wcs1.acct_id
                  where stup.modl_id='ECOMMERC' and stup.end_tms is null wcs1.scenario='CREATE' and wcs1.acct_id is not null and
                  wcs1.start_tms between add_months(TRUNC(SYSDATE,'mm'),-1) and
                  LAST_DAY(add_months(TRUNC(SYSDATE,'mm'),-1))
                  group by to_char(wcs1.start_tms,'DD/MM/YYYY')
                  order by to_char(wcs1.start_tms,'DD/MM/YYYY');





                  share|improve this answer


























                    0












                    0








                    0







                    Try below modified queries



                    select to_char(wcs1.start_tms,'DD/MM/YYYY') as dates,count(*) AS 
                    ecommerce
                    from ft_t_wcs1 wcs1, ft_t_stup stup
                    where stup.modl_id='ECOMMERC' and stup.CROSS_REF_ID=wcs1.acct_id and stup.end_tms is null wcs1.scenario='CREATE' and wcs1.acct_id is not null and
                    wcs1.start_tms between add_months(TRUNC(SYSDATE,'mm'),-1) and
                    LAST_DAY(add_months(TRUNC(SYSDATE,'mm'),-1))
                    group by to_char(wcs1.start_tms,'DD/MM/YYYY')
                    order by to_char(wcs1.start_tms,'DD/MM/YYYY');


                    -- Another way using JOIN clause



                    select to_char(wcs1.start_tms,'DD/MM/YYYY') as dates,count(*) AS 
                    ecommerce
                    from ft_t_wcs1 wcs1
                    join ft_t_stup stup
                    ON stup.CROSS_REF_ID=wcs1.acct_id
                    where stup.modl_id='ECOMMERC' and stup.end_tms is null wcs1.scenario='CREATE' and wcs1.acct_id is not null and
                    wcs1.start_tms between add_months(TRUNC(SYSDATE,'mm'),-1) and
                    LAST_DAY(add_months(TRUNC(SYSDATE,'mm'),-1))
                    group by to_char(wcs1.start_tms,'DD/MM/YYYY')
                    order by to_char(wcs1.start_tms,'DD/MM/YYYY');





                    share|improve this answer













                    Try below modified queries



                    select to_char(wcs1.start_tms,'DD/MM/YYYY') as dates,count(*) AS 
                    ecommerce
                    from ft_t_wcs1 wcs1, ft_t_stup stup
                    where stup.modl_id='ECOMMERC' and stup.CROSS_REF_ID=wcs1.acct_id and stup.end_tms is null wcs1.scenario='CREATE' and wcs1.acct_id is not null and
                    wcs1.start_tms between add_months(TRUNC(SYSDATE,'mm'),-1) and
                    LAST_DAY(add_months(TRUNC(SYSDATE,'mm'),-1))
                    group by to_char(wcs1.start_tms,'DD/MM/YYYY')
                    order by to_char(wcs1.start_tms,'DD/MM/YYYY');


                    -- Another way using JOIN clause



                    select to_char(wcs1.start_tms,'DD/MM/YYYY') as dates,count(*) AS 
                    ecommerce
                    from ft_t_wcs1 wcs1
                    join ft_t_stup stup
                    ON stup.CROSS_REF_ID=wcs1.acct_id
                    where stup.modl_id='ECOMMERC' and stup.end_tms is null wcs1.scenario='CREATE' and wcs1.acct_id is not null and
                    wcs1.start_tms between add_months(TRUNC(SYSDATE,'mm'),-1) and
                    LAST_DAY(add_months(TRUNC(SYSDATE,'mm'),-1))
                    group by to_char(wcs1.start_tms,'DD/MM/YYYY')
                    order by to_char(wcs1.start_tms,'DD/MM/YYYY');






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 14 '18 at 18:25









                    kanagarajkanagaraj

                    35417




                    35417

























                        0














                        It's hard to suggest an answer without understanding your table relationship, but I can tell that your problem is there is no relationship between your subquery and your main query. Your subquery simply returns a count where modl_id='ECOMMERC', so that value will always be the same - in your case, 105. You need to add a JOIN criteria to the subquery that ties the unique value to your main query. You'll also want to alias the table names differently to ensure you're joining correctly.






                        share|improve this answer




























                          0














                          It's hard to suggest an answer without understanding your table relationship, but I can tell that your problem is there is no relationship between your subquery and your main query. Your subquery simply returns a count where modl_id='ECOMMERC', so that value will always be the same - in your case, 105. You need to add a JOIN criteria to the subquery that ties the unique value to your main query. You'll also want to alias the table names differently to ensure you're joining correctly.






                          share|improve this answer


























                            0












                            0








                            0







                            It's hard to suggest an answer without understanding your table relationship, but I can tell that your problem is there is no relationship between your subquery and your main query. Your subquery simply returns a count where modl_id='ECOMMERC', so that value will always be the same - in your case, 105. You need to add a JOIN criteria to the subquery that ties the unique value to your main query. You'll also want to alias the table names differently to ensure you're joining correctly.






                            share|improve this answer













                            It's hard to suggest an answer without understanding your table relationship, but I can tell that your problem is there is no relationship between your subquery and your main query. Your subquery simply returns a count where modl_id='ECOMMERC', so that value will always be the same - in your case, 105. You need to add a JOIN criteria to the subquery that ties the unique value to your main query. You'll also want to alias the table names differently to ensure you're joining correctly.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 14 '18 at 18:29









                            msturekmsturek

                            491514




                            491514























                                0














                                You are doing unnecessary joins when you just want a correlated subquery:



                                SELECT to_char(wcs1.start_tms,'DD/MM/YYYY') as dates,
                                (SELECT count(*)
                                FROM ft_t_stup stup
                                WHERE stup.modl_id= 'ECOMMERC' AND
                                stup.CROSS_REF_ID = wcs1.acct_id
                                stup.end_tms IS NULL
                                ) AS ecommerce
                                FROM ft_t_wcs1 wcs1
                                WHERE wcs1.scenario = 'CREATE' AND
                                wcs1.acct_id IS NOT NULL AND
                                wcs1.start_tms BETWEEN add_months(TRUNC(SYSDATE,'mm'),-1) AND LAST_DAY(add_months(TRUNC(SYSDATE,'mm'),-1))
                                GROUP BY to_char(wcs1.start_tms, 'DD/MM/YYYY')
                                ORDER BY to_char(wcs1.start_tms, 'DD/MM/YYYY');





                                share|improve this answer




























                                  0














                                  You are doing unnecessary joins when you just want a correlated subquery:



                                  SELECT to_char(wcs1.start_tms,'DD/MM/YYYY') as dates,
                                  (SELECT count(*)
                                  FROM ft_t_stup stup
                                  WHERE stup.modl_id= 'ECOMMERC' AND
                                  stup.CROSS_REF_ID = wcs1.acct_id
                                  stup.end_tms IS NULL
                                  ) AS ecommerce
                                  FROM ft_t_wcs1 wcs1
                                  WHERE wcs1.scenario = 'CREATE' AND
                                  wcs1.acct_id IS NOT NULL AND
                                  wcs1.start_tms BETWEEN add_months(TRUNC(SYSDATE,'mm'),-1) AND LAST_DAY(add_months(TRUNC(SYSDATE,'mm'),-1))
                                  GROUP BY to_char(wcs1.start_tms, 'DD/MM/YYYY')
                                  ORDER BY to_char(wcs1.start_tms, 'DD/MM/YYYY');





                                  share|improve this answer


























                                    0












                                    0








                                    0







                                    You are doing unnecessary joins when you just want a correlated subquery:



                                    SELECT to_char(wcs1.start_tms,'DD/MM/YYYY') as dates,
                                    (SELECT count(*)
                                    FROM ft_t_stup stup
                                    WHERE stup.modl_id= 'ECOMMERC' AND
                                    stup.CROSS_REF_ID = wcs1.acct_id
                                    stup.end_tms IS NULL
                                    ) AS ecommerce
                                    FROM ft_t_wcs1 wcs1
                                    WHERE wcs1.scenario = 'CREATE' AND
                                    wcs1.acct_id IS NOT NULL AND
                                    wcs1.start_tms BETWEEN add_months(TRUNC(SYSDATE,'mm'),-1) AND LAST_DAY(add_months(TRUNC(SYSDATE,'mm'),-1))
                                    GROUP BY to_char(wcs1.start_tms, 'DD/MM/YYYY')
                                    ORDER BY to_char(wcs1.start_tms, 'DD/MM/YYYY');





                                    share|improve this answer













                                    You are doing unnecessary joins when you just want a correlated subquery:



                                    SELECT to_char(wcs1.start_tms,'DD/MM/YYYY') as dates,
                                    (SELECT count(*)
                                    FROM ft_t_stup stup
                                    WHERE stup.modl_id= 'ECOMMERC' AND
                                    stup.CROSS_REF_ID = wcs1.acct_id
                                    stup.end_tms IS NULL
                                    ) AS ecommerce
                                    FROM ft_t_wcs1 wcs1
                                    WHERE wcs1.scenario = 'CREATE' AND
                                    wcs1.acct_id IS NOT NULL AND
                                    wcs1.start_tms BETWEEN add_months(TRUNC(SYSDATE,'mm'),-1) AND LAST_DAY(add_months(TRUNC(SYSDATE,'mm'),-1))
                                    GROUP BY to_char(wcs1.start_tms, 'DD/MM/YYYY')
                                    ORDER BY to_char(wcs1.start_tms, 'DD/MM/YYYY');






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 14 '18 at 20:06









                                    Gordon LinoffGordon Linoff

                                    777k35306409




                                    777k35306409






























                                        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%2f53306247%2fsql-oracle-group-by-subquery%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