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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:49:33+00:00 2026-06-18T08:49:33+00:00

I am using the Rollup function and I am almost at the perfect point

  • 0

I am using the Rollup function and I am almost at the perfect point of what I need, just need to remove one little thing.

declare @FromDate datetime
declare @ToDate datetime
declare @StoreNumber varchar(10)
declare @AllStores varchar(1)
set @FromDate = '1/1/12'
set @ToDate = '1/1/13'
set @StoreNumber = '01'
set @AllStores = 'y'

If @AllStores = 'Y'
Begin
select 
CASE WHEN (GROUPING(s.StoreID) = 1) THEN 'All Stores' ELSE ISNULL(s.StoreID, 'UNKNOWN') END as 'Store #',
CASE WHEN (GROUPING(b.currencydesc) = 1) THEN 'Store #' + s.StoreID + ' TOTAL' ELSE ISNULL(b.currencydesc, 'UNKNOWN') END as 'Tender Type', SUM(amount) as Total
from RPTrs s 
join rpPay p on s.storeid = p.StoreID and s.ReceiptNO = p.ReceiptNo
join Currencies b on b.POSCurrency = LEFT(p.paytype,1)
where trsdate between @FromDate and @ToDate
group by s.storeid, b.currencydesc with rollup
End
Else
Begin
select 
s.StoreID as 'Store #',
CASE WHEN (GROUPING(b.currencydesc) = 1) THEN 'Store #' + s.StoreID + ' TOTAL' ELSE ISNULL(b.currencydesc, 'UNKNOWN') END as 'Tender Type', SUM(amount) as Total
from RPTrs s 
join rpPay p on s.StoreID = p.StoreID and s.ReceiptNO = p.ReceiptNo
join Currencies b on b.POSCurrency = LEFT(p.paytype,1)
where trsdate between @FromDate and @ToDate
and s.StoreID = @StoreNumber
group by grouping sets((s.StoreID),(s.StoreID, b.CurrencyDesc)) 
End

With this output.

Store # Tender Type Total
01  Amex    250.00
01  C.Card  3138.00
01  Cash    53553.17
01  Gift Card   1595.35
01  MasterCard  813.10
01  Off Line C.Card 247.53
01  Str Cr  -544.45
01  Visa/MC 437.35
01  Store #01 TOTAL 59490.05
02  Cash    238.15
02  Store #02 TOTAL 238.15
All Stores  NULL    59728.20

How can I make the NULL in All Stores total disappear?

  • 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-18T08:49:34+00:00Added an answer on June 18, 2026 at 8:49 am

    One way to do this is to wrap the query in a SELECT and then use COALESCE on the columns you want to replace the null values:

    SELECT [store #], 
           COALESCE([tender type], '') [Tender Type], 
           total 
    FROM   
    (
        SELECT 
            CASE 
                WHEN ( Grouping(s.storeid) = 1 ) 
                THEN 'All Stores' 
                ELSE Isnull(s.storeid, 'UNKNOWN') 
            END         AS [Store #], 
            CASE 
                WHEN ( Grouping(b.currencydesc) = 1 ) 
                THEN 'Store #' + s.storeid + ' TOTAL' 
                ELSE Isnull(b.currencydesc, 'UNKNOWN') 
            END         AS [Tender Type], 
            Sum(amount) AS Total 
        FROM   rptrs s 
        JOIN rppay p 
            ON s.storeid = p.storeid 
            AND s.receiptno = p.receiptno 
        JOIN currencies b 
            ON b.poscurrency = LEFT(p.paytype, 1) 
        WHERE  trsdate BETWEEN @FromDate AND @ToDate 
        GROUP  BY s.storeid, b.currencydesc WITH rollup
    ) src 
    

    Or you can wrap the column in a COALESCE() without the subquery:

    SELECT 
        CASE 
            WHEN ( Grouping(s.storeid) = 1 ) 
            THEN 'All Stores' 
            ELSE Isnull(s.storeid, 'UNKNOWN') 
        END               AS [Store #], 
        COALESCE(CASE 
                    WHEN ( Grouping(b.currencydesc) = 1 ) 
                    THEN 'Store #' + s.storeid + ' TOTAL' 
                    ELSE Isnull(b.currencydesc, 'UNKNOWN') 
                END, '') AS [Tender Type], 
        Sum(amount)       AS Total 
    FROM   rptrs s 
    JOIN rppay p 
        ON s.storeid = p.storeid 
        AND s.receiptno = p.receiptno 
    JOIN currencies b 
        ON b.poscurrency = LEFT(p.paytype, 1) 
    WHERE  trsdate BETWEEN @FromDate AND @ToDate 
    GROUP  BY s.storeid, b.currencydesc WITH rollup 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using group by ID WITH ROLLUP in my mysql query. The thing
I am using Rollup clause and you how it shows aggregation at various levels
Using import datetime in python, is it possible to take a formatted time/date string
I have an Sql Server Query that is using the ROLLUP clause while grouping.
I am running a query for a report and am using rollup to get
I currently have a script below which aggregates some data using rollup: SELECT CASE
I built a nav area using css (it triggers a javascript function to bring
I am using rollup to sum a table containing fields company , sales ,
When I run a query using group by ... with rollup : select a,
Using a CSS image sprite, I'm creating an 'interactive' image where hovering over certain

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.