Query for customer quotes by date range

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





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







up vote
1
down vote

favorite












I have a table (Oracle):



customer_quote
------
int id
int part_key (fk)
int condition_code (fk)
number unit_price
int qty_quoted
date entry_date


With a query that selects data from the past year:



SELECT cq.part_key 
, cq.condition_code
, COUNT(*) AS cq_count
, SUM(cq.unit_price * cq.qty_quoted) AS cq_total
, SUM(cq.qty_quoted) cq_qty
FROM customer_quote cq
WHERE cq.entry_date > TO_DATE('09-Jun-2017', 'dd-mm-yyyy')
GROUP BY cq.part_key
, cq.condition_code


But I needed the data to include parts with activity from the past two years but data older than 1 year shouldn't calculate into the totals. So I wrote this query:



SELECT cq.part_key 
, cq.condition_code
, COUNT(case when cq.entry_date > TO_DATE('09-Jun-2017', 'dd-mm-yyyy') then 1 END) AS cq_count
, SUM(case when cq.entry_date > TO_DATE('09-Jun-2017', 'dd-mm-yyyy') then (cq.unit_price * cq.qty_quoted) END) AS cq_total
, SUM(case when cq.entry_date > TO_DATE('09-Jun-2017', 'dd-mm-yyyy') then cq.qty_quoted END) cq_qty
FROM customer_quote cq
WHERE cq.entry_date > TO_DATE('09-Jun-2016', 'dd-mm-yyyy')
GROUP BY cq.part_key
, cq.condition_code


The where date is 2 years ago but the case when dates are all 1 year ago. This query works, but is it optimal? I think there's probably a way to merge the case when statements into 1.



Performance is my main concern, readability is only marginally important.



Worth noting: the final SQL query will be a parameterized SQL Java object so the date will be a CASE WHEN cq.entry_date > ? THEN and inserted using PreparedStatement::setDate() so with my solution I will need to do setDate() x 4.







share|improve this question



























    up vote
    1
    down vote

    favorite












    I have a table (Oracle):



    customer_quote
    ------
    int id
    int part_key (fk)
    int condition_code (fk)
    number unit_price
    int qty_quoted
    date entry_date


    With a query that selects data from the past year:



    SELECT cq.part_key 
    , cq.condition_code
    , COUNT(*) AS cq_count
    , SUM(cq.unit_price * cq.qty_quoted) AS cq_total
    , SUM(cq.qty_quoted) cq_qty
    FROM customer_quote cq
    WHERE cq.entry_date > TO_DATE('09-Jun-2017', 'dd-mm-yyyy')
    GROUP BY cq.part_key
    , cq.condition_code


    But I needed the data to include parts with activity from the past two years but data older than 1 year shouldn't calculate into the totals. So I wrote this query:



    SELECT cq.part_key 
    , cq.condition_code
    , COUNT(case when cq.entry_date > TO_DATE('09-Jun-2017', 'dd-mm-yyyy') then 1 END) AS cq_count
    , SUM(case when cq.entry_date > TO_DATE('09-Jun-2017', 'dd-mm-yyyy') then (cq.unit_price * cq.qty_quoted) END) AS cq_total
    , SUM(case when cq.entry_date > TO_DATE('09-Jun-2017', 'dd-mm-yyyy') then cq.qty_quoted END) cq_qty
    FROM customer_quote cq
    WHERE cq.entry_date > TO_DATE('09-Jun-2016', 'dd-mm-yyyy')
    GROUP BY cq.part_key
    , cq.condition_code


    The where date is 2 years ago but the case when dates are all 1 year ago. This query works, but is it optimal? I think there's probably a way to merge the case when statements into 1.



    Performance is my main concern, readability is only marginally important.



    Worth noting: the final SQL query will be a parameterized SQL Java object so the date will be a CASE WHEN cq.entry_date > ? THEN and inserted using PreparedStatement::setDate() so with my solution I will need to do setDate() x 4.







    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have a table (Oracle):



      customer_quote
      ------
      int id
      int part_key (fk)
      int condition_code (fk)
      number unit_price
      int qty_quoted
      date entry_date


      With a query that selects data from the past year:



      SELECT cq.part_key 
      , cq.condition_code
      , COUNT(*) AS cq_count
      , SUM(cq.unit_price * cq.qty_quoted) AS cq_total
      , SUM(cq.qty_quoted) cq_qty
      FROM customer_quote cq
      WHERE cq.entry_date > TO_DATE('09-Jun-2017', 'dd-mm-yyyy')
      GROUP BY cq.part_key
      , cq.condition_code


      But I needed the data to include parts with activity from the past two years but data older than 1 year shouldn't calculate into the totals. So I wrote this query:



      SELECT cq.part_key 
      , cq.condition_code
      , COUNT(case when cq.entry_date > TO_DATE('09-Jun-2017', 'dd-mm-yyyy') then 1 END) AS cq_count
      , SUM(case when cq.entry_date > TO_DATE('09-Jun-2017', 'dd-mm-yyyy') then (cq.unit_price * cq.qty_quoted) END) AS cq_total
      , SUM(case when cq.entry_date > TO_DATE('09-Jun-2017', 'dd-mm-yyyy') then cq.qty_quoted END) cq_qty
      FROM customer_quote cq
      WHERE cq.entry_date > TO_DATE('09-Jun-2016', 'dd-mm-yyyy')
      GROUP BY cq.part_key
      , cq.condition_code


      The where date is 2 years ago but the case when dates are all 1 year ago. This query works, but is it optimal? I think there's probably a way to merge the case when statements into 1.



      Performance is my main concern, readability is only marginally important.



      Worth noting: the final SQL query will be a parameterized SQL Java object so the date will be a CASE WHEN cq.entry_date > ? THEN and inserted using PreparedStatement::setDate() so with my solution I will need to do setDate() x 4.







      share|improve this question













      I have a table (Oracle):



      customer_quote
      ------
      int id
      int part_key (fk)
      int condition_code (fk)
      number unit_price
      int qty_quoted
      date entry_date


      With a query that selects data from the past year:



      SELECT cq.part_key 
      , cq.condition_code
      , COUNT(*) AS cq_count
      , SUM(cq.unit_price * cq.qty_quoted) AS cq_total
      , SUM(cq.qty_quoted) cq_qty
      FROM customer_quote cq
      WHERE cq.entry_date > TO_DATE('09-Jun-2017', 'dd-mm-yyyy')
      GROUP BY cq.part_key
      , cq.condition_code


      But I needed the data to include parts with activity from the past two years but data older than 1 year shouldn't calculate into the totals. So I wrote this query:



      SELECT cq.part_key 
      , cq.condition_code
      , COUNT(case when cq.entry_date > TO_DATE('09-Jun-2017', 'dd-mm-yyyy') then 1 END) AS cq_count
      , SUM(case when cq.entry_date > TO_DATE('09-Jun-2017', 'dd-mm-yyyy') then (cq.unit_price * cq.qty_quoted) END) AS cq_total
      , SUM(case when cq.entry_date > TO_DATE('09-Jun-2017', 'dd-mm-yyyy') then cq.qty_quoted END) cq_qty
      FROM customer_quote cq
      WHERE cq.entry_date > TO_DATE('09-Jun-2016', 'dd-mm-yyyy')
      GROUP BY cq.part_key
      , cq.condition_code


      The where date is 2 years ago but the case when dates are all 1 year ago. This query works, but is it optimal? I think there's probably a way to merge the case when statements into 1.



      Performance is my main concern, readability is only marginally important.



      Worth noting: the final SQL query will be a parameterized SQL Java object so the date will be a CASE WHEN cq.entry_date > ? THEN and inserted using PreparedStatement::setDate() so with my solution I will need to do setDate() x 4.









      share|improve this question












      share|improve this question




      share|improve this question








      edited Jun 9 at 19:47









      200_success

      123k14143399




      123k14143399









      asked Jun 9 at 19:04









      ryvantage

      1064




      1064




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          I think the query is optimal enough. The truth is query can retrieve a result only by accessing 2 years data, so given WHERE condition is must. And the query access rows only once. At performance aspect, table access method (through index or by full scan) is critical, but Oracle Optimizer will choose it by referring statistics information.






          share|improve this answer





















            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            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: "196"
            ;
            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: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f196184%2fquery-for-customer-quotes-by-date-range%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote













            I think the query is optimal enough. The truth is query can retrieve a result only by accessing 2 years data, so given WHERE condition is must. And the query access rows only once. At performance aspect, table access method (through index or by full scan) is critical, but Oracle Optimizer will choose it by referring statistics information.






            share|improve this answer

























              up vote
              1
              down vote













              I think the query is optimal enough. The truth is query can retrieve a result only by accessing 2 years data, so given WHERE condition is must. And the query access rows only once. At performance aspect, table access method (through index or by full scan) is critical, but Oracle Optimizer will choose it by referring statistics information.






              share|improve this answer























                up vote
                1
                down vote










                up vote
                1
                down vote









                I think the query is optimal enough. The truth is query can retrieve a result only by accessing 2 years data, so given WHERE condition is must. And the query access rows only once. At performance aspect, table access method (through index or by full scan) is critical, but Oracle Optimizer will choose it by referring statistics information.






                share|improve this answer













                I think the query is optimal enough. The truth is query can retrieve a result only by accessing 2 years data, so given WHERE condition is must. And the query access rows only once. At performance aspect, table access method (through index or by full scan) is critical, but Oracle Optimizer will choose it by referring statistics information.







                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered Aug 3 at 9:12









                kangbu

                1111




                1111






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f196184%2fquery-for-customer-quotes-by-date-range%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Python Lists

                    Aion

                    JavaScript Array Iteration Methods