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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:44:08+00:00 2026-05-19T22:44:08+00:00

The following SQL query that displays products sold sorted by cost and number of

  • 0

The following SQL query that displays products sold sorted by cost and number of orders have to be sorted in a partitioned manner. Namely, products with the cost of under $100 should go first and then everything else that is > $100 should follow it. Adding HAVING TS.TotalSold < 100 to the query would accomplish this for the first partition, but would filter out other products. The operation should be atomic, so that this query can be executed only once.

NOTE: cost by which the query has to be partitioned is calculated as a max of two cost columns, which makes things a bit more complicated (the proposed solutions of CASE WHEN won’t work as HighestCost is not a column)


SELECT PS.ProductName, TS.TotalSold, 
  ((PS.Cost1 + PS.Cost2 + ABS(PS.Cost1-PS.Cost2)) / 2) as HighestCost
FROM Products as PS
   CROSS APPLY 
      (SELECT 
         (SELECT COUNT(OrderId) 
          FROM Orders as OS 
          WHERE OS.ProductId=PS.ProductId) 
       as TotalSold) TS
ORDER BY HighestCost ASC, TS.TotalSold

EDIT: modified the query to include calculated cost by which the query has to be partitioned.

  • 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-19T22:44:09+00:00Added an answer on May 19, 2026 at 10:44 pm

    EDITED

    SELECT *
    FROM
    (
    SELECT PS.ProductName, TS.TotalSold, 
      ((PS.Cost1 + PS.Cost2 + ABS(PS.Cost1-PS.Cost2)) / 2) as HighestCost
    FROM Products as PS
       CROSS APPLY 
          (SELECT COUNT(OrderId) as TotalSold
           FROM Orders as OS 
           WHERE OS.ProductId=PS.ProductId) TS
    ) SQ
    ORDER BY CASE WHEN HighestCost > 100 THEN 1 END ASC, TotalSold
    

    original below

    SELECT PS.ProductName, TS.TotalSold
    FROM Products as PS
       CROSS APPLY 
          (SELECT COUNT(OrderId) as TotalSold
           FROM Orders as OS 
           WHERE OS.ProductId=PS.ProductId) TS
    ORDER BY
     CASE WHEN TS.TotalSold > 100 THEN 1 END, PS.Cost ASC, TS.TotalSold
    

    You may notice I removed a subquery level since it was extraneous.

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

Sidebar

Related Questions

I currently have the following SQL query that lists all the names of all
I have the following sql query that fetches images from an establishment table. The
Please suppose that in SQL Server 2005, if you launch the following query: SELECT
I have the following SQL query: SELECT r.BEZEICHNUNG AS BEZEICHNUNG, r.ID AS ID, ra.BEZEICHNUNG
I have the following SQL query SELECT r.BEZEICHNUNG AS BEZEICHNUNG, r.ID AS ID, ra.BEZEICHNUNG
I have the following sql query and I want to filter the results where
I have the following SQL query I was given, but I am not sure
I have following SQL query SELECT TOP 10000 AVG(DailyNodeAvailability.Availability) AS AVERAGE_of_Availability FROM Nodes INNER
I have the following query that I need to convert to rails like syntax.
I have the following SQL query which I use to fetch some rows from

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.