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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:46:24+00:00 2026-06-03T08:46:24+00:00

How do I sort the results into the following example by the sellers name

  • 0

How do I sort the results into the following example by the sellers name while keeping the rollup at the bottom?

Since the the grouping is applied to the nested SELECT I can’t use ORDER BY and since the grouping isn’t applied at the top level I can’t use the GROUPING either.

Click here to see the working example in SQL Fiddle.

CREATE TABLE Sales 
(
        SellerID    INT
    ,   StoreID     INT
    ,   Price       MONEY
);

CREATE TABLE Sellers 
(
        SellerID    INT
    ,   Name        VARCHAR(50)  
)

INSERT INTO Sales VALUES 
    (1, 1, 100),
    (1, 1, 100),
    (1, 1, 100),
    (2, 2, 200),
    (2, 2, 200),
    (3, 2, 250),
    (3, 2, 250),
    (3, 2, 250),
    (3, 2, 250);

INSERT INTO Sellers VALUES
    (1, 'C. Thirdplace'),
    (2, 'A. Firstplace'),
    (3, 'B. Secondplace');

SELECT  s.Name          AS Seller_Name
    ,   x.TotalSales    AS Total_Sales
FROM 
(  
    SELECT      s.SellerID AS SellerID
            ,   SUM(s.Price) AS TotalSales
    FROM        Sales s 
    GROUP BY    s.SellerID 
    WITH ROLLUP
) x
LEFT JOIN   Sellers s 
ON          s.SellerID = x.SellerID;

Which produces the following result:

SELLER_NAME      TOTAL_SALES
---------------  -----------
C. Thirdplace        300
A. Firstplace        400
B. Secondplace      1000
(null)              1700
  • 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-03T08:46:25+00:00Added an answer on June 3, 2026 at 8:46 am
    ORDER BY
      CASE WHEN seller_name IS NULL THEN 1 ELSE 0 END,
      seller_name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to sort results set first based on one column and then based
I need sphinx to sort the results by the sum of an attribute. I
I am using a dropdown to allow the user to sort search results. These
Hi I have the following bubble sort algorithm (generic code taken from another site)
Stuck again. :( I have the following code crammed into a procedure invoked when
Hi I have a question about Batcher's odd-even-merge sort. I have the following code:
I am looking for a way to pivot the following results... ID | Group_Level
The following is a simplified example of a page a user has created at
I don't know what the proper name for this sort of programming, but all
I am following Phil Haack's example on using jQuery Grid with ASP.NET MVC .

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.