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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:38:56+00:00 2026-06-18T10:38:56+00:00

I have following sample database schema https://i.stack.imgur.com/EKNxY.jpg I want: A) All sales, group by

  • 0

I have following sample database schema

https://i.stack.imgur.com/EKNxY.jpg

enter image description here

I want:
A) All sales, group by Users.
B) Discounts, group by users.

I am using following queries:
FOR A:

    SELECT u.UserID, u.UserName, Sum(d.Quantity * i.Price) AS 'Sales'
FROM OrderDetails d
INNER JOIN Orders o ON o.OrderID = d.OrderID
INNER JOIN Item i ON i.ItemID = d.ItemID
INNER JOIN [User] u ON u.UserID = o.UserID
GROUP BY u.UserID, u.UserName

RESULT:

UserID UserName Sales
1 Mobeen 11060
2 Cashier 25960

FOR B:

    SELECT u.UserID, u.UserName, Sum(r.DiscountAmount) AS Discounts
FROM Receipt r
INNER JOIN Orders o ON o.OrderID = r.OrderID
INNER JOIN [User] u ON u.UserID = o.UserID
GROUP BY u.UserID, u.UserName

RESULT:

UserID UserName Discounts
1 Mobeen 50
2 Cashier 310

BUT Then I decided to combine both queries but now the problem is that I am getting different results.
The query is as below:

    SELECT u.UserID,
    u.UserName,
    Sum(d.Quantity * i.Price) AS 'Sales',
    Sum(r.DiscountAmount) AS 'Discounts'
FROM OrderDetails d
INNER JOIN Orders o ON o.OrderID = d.OrderID
INNER JOIN Item i ON i.ItemID = d.ItemID
INNER JOIN [User] u ON u.UserID = o.UserID
INNER JOIN Receipt r ON r.OrderID = o.OrderID
GROUP BY u.UserID, u.UserName

Result:

UserID UserName Sales Discounts
1 Mobeen 2270 50
2 Cashier 25760 430

  • 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-18T10:38:57+00:00Added an answer on June 18, 2026 at 10:38 am

    The problem is that you are summing along multiple dimensions. If you want to combine them, use subqueries:

    select s.userID, s.userName, s.Sales, d.Discounts
    from (SELECT u.UserID, u.UserName, Sum(d.Quantity * i.Price) AS 'Sales'
          FROM OrderDetails d
               INNER JOIN Orders o ON o.OrderID = d.OrderID
               INNER JOIN Item i ON i.ItemID = d.ItemID
               INNER JOIN [User] u ON u.UserID = o.UserID
               GROUP BY u.UserID, u.UserName
         ) s join
         (SELECT u.UserID, u.UserName, Sum(r.DiscountAmount) AS Discounts
          FROM Receipt r
               INNER JOIN Orders o ON o.OrderID = r.OrderID
               INNER JOIN [User] u ON u.UserID = o.UserID
           GROUP BY u.UserID, u.UserName
         ) d
         on s.userId = d.userId and s.userName = d.userName
    

    I think what is happening is that you are multiplying the receipt records for each item. You need to be very careful when bringing together data naively using joins. For such aggregations, subqueries are your friend.

    If you need to get all the rows (i.e., there may be mismatches on either side), switch the join to a full outer join and use:

    select coalesce(s.userId, d.userId) as userId, coalesce(s.userName, d.userName) as userName, . . .
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following sample code which doesn't seem to want to run. import
I have the following test sample: <Window x:Class=WpfScrollTest.Window1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml Title=Window1 Height=200 Width=200> <Border>
I have a database with the following schema: fund(fundID, fundName,totalShares) price(fundID,priceDate,price) trans(sin,fundID,transDate,shares) customer(sin,name,address,phone) rep(repid,repName)
I have a sample database like below, and I want to display only the
I have the following database tables : user int unsigned id varchar login group
I have the following sample database set up - CREATE TABLE IF NOT EXISTS
Consider the following scenario We have a simple database that involves two entities: user
I have the following task: there are a simple table from mysql database: <html>
I have the following sample code and noticed that if I attempt to use
I have the following sample data: Id Name Quantity 1 Red 1 2 Red

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.