Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 967179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:16:28+00:00 2026-05-16T02:16:28+00:00

I need to query the orders table to get a count of all orders

  • 0

I need to query the orders table to get a count of all orders for yesterdays transactions, grouped by the ship date. Then I need to have an additional column to give the total orders for the ship date for all transactions. When I added this second column, time of processing grew exponentially (which was expected) to 109s. Is there any way I could improve this SQL? I just want to know if I’m missing anything fundamental here.

SELECT t.shipping_date
       , t.net_orders
       , count(*) as total_orders
FROM (
    SELECT   s.store_num
             , s.store_cd
             , to_char(o.shipping_date, 'MM/DD/YYYY') as shipping_date
             , COUNT (*) as net_orders
    FROM order o left 
           join store s 
                on ( s.store_num = o.store_num )
    WHERE TRUNC (o.order_date) = TRUNC (SYSDATE - 1)
    AND s.store_cd = 'ZZZ'
    AND o.status in ('A', 'B')
    GROUP BY s.store_num
             , s.store_cd
             , to_char(shipping_date, 'MM/DD/YYYY')
) t
LEFT JOIN order o ON 
          ( TRUNC (o.shipping_date) = to_date(t.shipping_date, 'MM/DD/YYYY') 
            and o.store_num = t.store_num )
WHERE o.status in ('A', 'B')
GROUP BY t.shipping_date, t.net_orders;

I have indexes on all of these columns in addition to the following expressions: TRUNC(order_date) and TRUNC(shipping_date).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T02:16:29+00:00Added an answer on May 16, 2026 at 2:16 am

    I re-wrote your query as:

       SELECT t.shipping_date, 
              t.net_orders, 
              COUNT(*) as total_orders 
         FROM (SELECT s.store_num, 
                      s.store_cd, 
                      o.status,
                      TRUNC(o.shipping_date) AS shipping_date, 
                      COUNT (*) as net_orders
                 FROM STORE s
                 JOIN ORDER o ON o.store_num = s.store_num
                             AND o.status IN ('A', 'B')
                WHERE s.store_cd = 'ZZZ'
                  AND TRUNC(order_date) = TRUNC (SYSDATE - 1)
      GROUP BY s.store_num, s.store_cd, TRUNC(shipping_date)) t
    LEFT JOIN ORDER o ON TRUNC(o.shipping_date) = t.shipping_date
                     AND o.store_num = t.store_num
                     AND o.status = t.status
     GROUP BY t.shipping_date, t.net_orders;
    

    Some minor re-arrangement, but I did get rid of the TO_CHAR(shipping_date) which is later converted back to a DATE. TRUNC(shipping_date) is the same, and simplifies the operation.

    Joining on criteria using functions won’t use an index – you’ll need to create a function based index matching the JOIN criteria.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get the count of surveys that need to be taken which
I have a table with the following columns source_title, country, language, source_url I need
I have the following query: SELECT location, step, COUNT(*), AVG(foo), YEAR(start), MONTH(start), DAY(start) FROM
I'm new to MySql and to databases in general. I have a query, which
The table looks like this: CREATE TABLE `tweet_tweet` ( `id` int(11) NOT NULL AUTO_INCREMENT,
I have the following script which runs fine but I think a) it is
My issue is that I have a Select statement that has a where clause
Given this table: Order custName description to_char(price) A desa $14 B desb $14 C
I am trying to merge 2 databases with the same schema together, and this
I would like to be able to output a timeline containing the number of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.