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

  • SEARCH
  • Home
  • 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 5986765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:43:07+00:00 2026-05-22T22:43:07+00:00

I am creating a report of Shops Order. SQL Query does work as expected.

  • 0

I am creating a report of Shops Order. SQL Query does work as expected.

It calulate the numbers of orders from each shop, the total cost of orders and our commission and the outstanding fields.

If the customer paid by Cash at the delivery, then it show OutstandingComm field
If the customer paid by Card from Online, then it show outstanding_shop field

SELECT T.ShopID, T.company, O.order_id, count(*) as NumOfOrders, 
       sum(O.shop_remaining) as ShopEarnings, 
       sum(O.comm_grandtotal) as OurComm, 
       SUM(CASE WHEN payment_method = 'PayCash' AND status = 1 THEN O.comm_grandtotal ELSE 0 END) as OutstandingComm,
       SUM(CASE WHEN payment_method = 'PayCard' AND status = 1 THEN O.shop_remaining ELSE 0 END) as outstanding_shop
FROM Shops as T 
      JOIN orders O ON O.ShopID = T.ShopID 
Group by ShopID

Can this SQL query could be improved or is there alternative better way?

  • 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-22T22:43:08+00:00Added an answer on May 22, 2026 at 10:43 pm

    Few things I can think of:
    Here are some things to consider, try them if you feel like it, but be sure to time the effect of each change, it might not work or it might make things slower:

    1. The case when looks at a string (PayCash/PayCard). If you make that field a number, put an index on it and check the numeric value, it might run faster. The index might not get used though because it has a low cardinality.
    2. If you use InnoDB, try and put an index on shops.company. This way MySQL can use the covering index for shops.company to retrieve the companyname and doesn’t need to do a full table read on table shops.
    3. You might consider partitioning the tables by field status. Status has low cardinality, so an index will probably not be used, but if you partition, MySQL will only read from the status=1 version of the table, only do this when there are a lot of status=0 fields.
    4. You are currently selecting all transactions, this is a bit unlikely. Partition the table by year or month and select only the current year or month.
    5. I’m sure you already have a primary autoincrement keys on shops.shopid.
    6. You should also have an index on orders.shopid
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to change order of records/groups in a result-set from a query
I am creating a web shop and have to calculate how much to charge
I have used Spreadsheet::WriteExcel for creating spreadsheet report. I tried to merge some cells
I'm creating some reports in Visual Studio BIDS, out of our SQL Analysis Server
I created a report using wizard and while creating I selected 4 columns for
I'm interested in creating a custom Export to Excel option for my Report in
We are tasked with creating a reporting frameworks that does the following. Clients can
While learning about creating MS SQL Reporting and deploying reports to SQL Reporting Server,
An iPhone app which I am creating generates reports from a Core Data database
When creating reports using BIRT 2.3.1, I don't want page breaks inside tables or

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.