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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:39:05+00:00 2026-05-22T18:39:05+00:00

I have a query similar to the following: SELECT CASE WHEN (GROUPING(Name) = 1)

  • 0

I have a query similar to the following:

SELECT  CASE WHEN (GROUPING(Name) = 1) THEN 'All' ELSE Name END AS Name, 
        CASE WHEN (GROUPING(Type) = 1) THEN 'All' ELSE Type END AS Type,
        sum(quantity) AS [Quantity],
        CAST(sum(quantity) * (SELECT QuantityMultiplier FROM QuantityMultipliers WHERE a = t.b) AS DECIMAL(18,2)) AS Multiplied Quantity
FROM @Table t
GROUP BY Name, Type WITH ROLLUP

I’m trying to return a list of Names, Types, a summed Quantity and a summed quantity multiplied by an arbitrary number. All fine so far. I also need to return a sub-total row per Name and per Type, such as the following

Name        Type        Quantity        Multiplied Quantity
-------     ---------   -----------     -------------------
a           1           2               4
a           2           3               3
a           ALL         5               7
b           1           6               12
b           2           1               1
b           ALL         7               13
ALL         ALL         24              40

The first 3 columns are fine. I’m getting null values in the rollup rows for the multiplied quantity though. The only reason I can think this is happening is because SQL doesn’t recognize the last column as an aggregate now that I’ve multiplied it by something.

Can I somehow work around this without things getting too convoluted?

I will be falling back onto temporary tables if this can’t be done.

  • 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-22T18:39:06+00:00Added an answer on May 22, 2026 at 6:39 pm

    In your sub-query to acquire the multiplier, you have WHERE a=b. Are either a or b from the tables in your main query?

    If these values are static (nothing to do with the main query), it looks like it should be fine…

    If the a or b values are the name or type field, they can be NULL for the rollup records. If so, you can change to something similiar to…

    CAST(sum(quantity * (<multiplie_query>)) AS DECIMAL(18,2)).

    If a or b are other field from your main query, you’d be getting multiple records back, not just a single multiplier. You could change to something like…

    CAST(sum(quantity) * (SELECT MAX(multiplier) FROM ...)) AS DECIMAL(18,2))

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

Sidebar

Related Questions

We have a Union Query. Here's a basic (similar) example: SELECT a.Name, b.Info FROM
I have the following query:- select dbo.table1.service, dbo.table1.level_3_structure, Sum(table1.Reduced) as Total_Reduced from dbo.table1 where
I have a query similar to the following: SELECT users.id FROM users LEFT JOIN
I currently have a query similar to: SELECT users.fname, users.lname, sales.date FROM users LEFT
I am totally new to SQL . I have a simple select query similar
I have a multi-table query, similar to this (simplified version) SELECT columns, count(table2.rev_id) As
I have the following query That gets the city name, city id, the region
I have the following query SELECT * FROM (`user_profiles`) WHERE `user_id` = $user_id LIMIT
I'm using a SQL query that is similar to the following form: SELECT col1,
I have an SQL query similar to the following: create procedure test ( @param1

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.