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 8677323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:25:42+00:00 2026-06-12T20:25:42+00:00

Here’s the scenario: I’ve got an orders table and a customers table. I’d like

  • 0

Here’s the scenario: I’ve got an orders table and a customers table. I’d like to calculate the SUM of the column total for specific storeID. The problem is that my company used to store the storeID in the customers table but now it’s being stored in the orders table. There are orders in our database where the storeID is only set in the customers table, only set in the orders table, or set in both tables.

Here’s some schema example data for reference:

Table Orders:

+----+-------+------------+---------+
| id | total | customerID | storeID |
+----+-------+------------+---------+
|  0 |   100 |          1 | 1       |
|  1 |    50 |          1 | NULL    |
|  2 |    75 |          1 | 1       |
+----+-------+------------+---------+

Table Customers:

+----+----------+
| id | storeID  |
+----+----------+
|  1 | 1 | NULL |
+----+----------+

The query I’ve been playing around with trying to calculate the sum of the order total looks like this:

SELECT SUM(total)
FROM orders o
INNER JOIN customers c
        ON c.ID = o.customerID
WHERE ISNULL(c.storeID,o.storeID) = @storeID

This query works, but it’s super slow because we have so many order records in our database. Is there a more efficient way to do this? I’m using SQL Server 2008 R2.

Thanks,
Matt

  • 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-06-12T20:25:43+00:00Added an answer on June 12, 2026 at 8:25 pm
    SELECT SUM(total)
    FROM orders o
    WHERE coalesce(o.storeID, (
                        select storeID
                        from customers
                        where id = o.customerID
                    ) = @storeID
    

    BTW you can just update the orders table and after that query from there only:

    update orders o
    set storeID = (
            select storeID
            from customers
            where id = o.customerID
            )
    where storeID is null
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's what my table TheTable looks like ColA | ColB | ColC ------+-------+------ abc
Here's a coding problem for those that like this kind of thing. Let's see
Here is the code: create table `team`.`User`( `UserID` bigint NOT NULL AUTO_INCREMENT , `Username`
Here is my SQL script CREATE TABLE tracks( track_id int NOT NULL AUTO_INCREMENT, account_id
Here is the scenario. I'm writing my geo-ruby oracle adapter for Ruby On Rails
Here my problem: @Assert\Regex( * pattern=/^[A-Za-z0-9][A-Za-z0-9\]*$/, * groups={creation, creation_logged} * ) I'm using the
Here's an example query: DECLARE @table table (loc varchar(10)) INSERT INTO @table VALUES ('134a'),
Here is the problem that I am trying to solve. I have two folders
Here is my problem : I have a post controller with the action create.
Here is the problem I'm trying to solve for my game. I have this

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.