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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:53:26+00:00 2026-06-15T15:53:26+00:00

The task is to select the sub category with the minimum profit per year.

  • 0

The task is to select the sub category with the minimum profit per year.
The next query, selects few sub categories per year:

select 
  min (Profit), 
  CalendarYear, 
  EnglishProductSubcategoryName
from (
  select 
    SUM(fis.SalesAmount-fis.TotalProductCost) Profit, 
    t.CalendarYear, 
    sc.EnglishProductSubCategoryName
    from FactInternetSales fis 
    inner join DimProduct p 
            on fis.ProductKey = p.ProductKey
    inner join DimProductSubcategory sc 
            on p.ProductSubcategoryKey = sc.ProductSubcategoryKey
    inner join DimTime t 
            on fis.DueDateKey = t.TimeKey
    group by CalendarYear, EnglishProductSubcategoryName) aa 
    --Order by CalendarYear
) aa
group by CalendarYear, EnglishProductSubcategoryName
order by CalendarYear 
  • 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-15T15:53:28+00:00Added an answer on June 15, 2026 at 3:53 pm

    If you want to find the category with the minimum profit for a given year, you need to rewrite your query:

    select 
        Profit,
        CalendarYear, 
        EnglishProductSubcategoryName
    from 
        (..... ) aa
    where
        CalendarYear = 2011
        AND Profit = (SELECT MIN(Profit) FROM aa WHERE aa.CalendarYear = 2011)
    

    This will find the row(s) – it could be multiple – that have the minimum profit (for 2011) as reported back by the subquery.

    Update: since you need the minimum profit for every year, I would probably totally rewrite this query to something like:

    ;WITH YearlyProfitsByCategory AS
    (
       SELECT
          SUM(fis.SalesAmount - fis.TotalProductCost) Profit, 
          t.CalendarYear, 
          sc.EnglishProductSubCategoryName
       FROM
          dbo.FactInternetSales fis 
       INNER JOIN
          dbo.DimProduct p ON fis.ProductKey = p.ProductKey
       INNER JOIN
          dbo.DimProductSubcategory sc ON p.ProductSubcategoryKey = sc.ProductSubcategoryKey
       INNER JOIN
          dbo.DimTime t ON fis.DueDateKey = t.TimeKey
       GROUP BY
          t.CalendarYear, 
          sc.EnglishProductSubCategoryName
    ),
    YearlyMinProfits AS 
    (
        SELECT
           CalendarYear, 
           EnglishProductSubCategoryName,
           Profit,
           RowNum = ROW_NUMBER() OVER (PARTITION BY CalendarYear ORDER BY Profit)
        FROM YearlyProfitsByCategory 
    )
    SELECT 
       CalendarYear, EnglishProductSubCategoryName, Profit
    FROM YearlyMinProfits
    WHERE RowNum = 1  -- the row with the smallest profit, for every year
    

    This uses a CTE (Common Table Expression) and the ROW_NUMBER() ranking function – both available in SQL Server 2005 and newer (you didn’t mention your version in your question)

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

Sidebar

Related Questions

I've got a query: SELECT taskDeadline, subtasksDeadline, REPLACE( LEAST( COALESCE(t.deadline, 2147483647), COALESCE(sub.deadline, 2147483647) ),
I have a task table, which has a sub category. The sub category is
I'm looking at Task Manager's list of processes, and enabled View > Select columns
I have a simple query: select * from countries with the following results: country_name
i have this query and code if(isset($_REQUEST['main_task']) && ($_REQUEST['main_task'] !='')) { $query_task = SELECT
Trying IE Automation with Powershell. Task: Select multiple option value fields and click submitbutton.
I'm trying to create a sub-query to obtain the latest date from a table,
I had got a task to select search the students those name start with
Task: To provide facility to upgrade the system remotely or add new features. What
Task at hand — I have three versions of some code, developed by different

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.