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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:56:46+00:00 2026-05-30T11:56:46+00:00

I am working with Northwind Data Base(Microsoft Example) and i want to know total

  • 0

I am working with Northwind Data Base(Microsoft Example) and i want to know total Sale of each Region.
tables for this example are (Region,Territories,EmployeeTerritories,Employees,Orders and OrderDetails)
And relations are:

  1. Region on to many Territories
  2. Territories on to many EmployeeTerritories
  3. EmployeeTerritories many to one Employees
  4. Employees on to many Orders
  5. Orders one to many OrderDetails

i know that i should to use Sub query but i dont know how.

select
      * 
from
   Region R
   inner join Territories T
       on R.RegionID=T.RegionID
   inner join EmployeeTerritories ET
       on T.TerritoryID=ET.TerritoryID
   inner join
      (
         select
            E.EmployeeID,
            E.FirstName,
            E.LastName,
            sum(OD.Quantity*OD.UnitPrice) as TotalEmployeeSale
          from
             Employees E
             inner join Orders O
                on E.EmployeeID=o.EmployeeID
             inner join [Order Details] OD
                on o.OrderID=OD.OrderID
             Group by
                E.EmployeeID,
                E.FirstName,
                E.LastName
      )as  ES on ET.EmployeeID=ES.EmployeeID 

I use this sql query to calculate Total sale for Each Employee but how can i Caclulate is for Each Region.

  • 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-30T11:56:48+00:00Added an answer on May 30, 2026 at 11:56 am

    Haven’t got access to Northwind right now, so this is untested, but you should get the idea…

    There’s no need to get data about employees if all you want is sales per region. Your subquery is therefore redundant…

    select
          r.RegionDescription, 
          sum(OD.Quantity*OD.UnitPrice)
    from
       Region R
       inner join Territories T
           on R.RegionID=T.RegionID
       inner join EmployeeTerritories ET
           on T.TerritoryID=ET.TerritoryID
       inner join  Employees E
           on ET.EmployeeID=E.EmployeeID
       inner join Orders O
           on E.EmployeeID=o.EmployeeID
       inner join [Order Details] OD
                    on o.OrderID=OD.OrderID
      Group by r.RegionDescription
    

    As discussed in the comments, this “double counts” sales where an employee is assigned to more than one region. In many cases, this is desired behaviour – if you want to know how well a region is doing, you need to know how many sales came from that region, and if an employee is assigned to more than one region, that doesn’t affect the region’s performance.

    However, it means you overstate the sales if you add up all the regions.

    There are two strategies to avoid this. One is to assign the sale to just one region; in the comments, you say there’s no data on which to make that decision, so you could do it on the “lowest regionID) – something like:

    select
          r.RegionDescription, 
          sum(OD.Quantity*OD.UnitPrice)
    from
       Region R
       inner join Territories T
           on R.RegionID=T.RegionID
       inner join EmployeeTerritories ET
           on T.TerritoryID=ET.TerritoryID
       inner join  Employees E
           on ET.EmployeeID=E.EmployeeID
       inner join Orders O
           on E.EmployeeID=o.EmployeeID
       inner join [Order Details] OD
                    on o.OrderID=OD.OrderID
      Group by r.RegionDescription
      having et.TerritoryID = min(territoryID)
    

    (again, no access to DB, so can’t test – but this should filter out duplicates).

    Alternatively, you can assign a proportion of the sale to each region – though then rounding may cause the totals not to add up properly. That’s a query I’d like to try before posting though!

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

Sidebar

Related Questions

Working through this for fun: http://www.diku.dk/hjemmesider/ansatte/torbenm/Basics/ Example calculation of nullable and first uses a
Why is this query not working? Select temp.CompanyName from ( SELECT c.CompanyName, o.OrderID, YEAR(o.OrderDate)
Working on a website http://www.ArenaText.com written in asp.net with Microsoft AJAX control toolkit. iPad
Working on an old Kohana 2 project and I want to link two models.
Working with stl:list and stl::vector types under interrupt handlers I want to avoid malloc()
Working through C++ Primer Plus and am trying to cin data to a dynamically
Working on dom html . I want to convert node value to string: $html
Working SQL The following code works as expected, returning two columns of data (a
Working on this EF tutorial , I've difficult to understand the meaning of the
Working on a legacy application at the moment and we are replacing the data

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.