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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:15:07+00:00 2026-05-23T14:15:07+00:00

I have the following code that displays strange output, I’ve previously thought MIN/MAX SHOULD

  • 0

I have the following code that displays strange output,
I’ve previously thought MIN/MAX SHOULD display same results when there is only one record in the given group .
The result for MONTH_2 and MONTH_4 is 0,0 for MIN 2,5 for MAX a clarification for this behavior would be appreciated

CREATE TABLE #CUST_ITEM (CUSTNMBR CHAR(31),ITEM CHAR(20),[MONTH] TINYINT,TOT_QTY DECIMAL(10,2))
INSERT INTO #CUST_ITEM VALUES('BA000057','BKNGPDR100',2,200)
INSERT INTO #CUST_ITEM VALUES('BA000057','BKNGPDR100',4,5000)
--QUERY_1 WITH AGGREGATE MIN
SELECT  CUSTNMBR,ITEM, 
MIN(CASE WHEN [MONTH] =2 THEN TOT_QTY ELSE 0 END) AS MONTH_2,
MIN(CASE WHEN [MONTH] =4 THEN TOT_QTY ELSE 0 END) AS MONTH_4,
MIN(CASE WHEN [MONTH] =5 THEN TOT_QTY ELSE 0 END) AS MONTH_5,
SUM(TOT_QTY) AS TOT_QTY_YEAR
FROM 
#CUST_ITEM
GROUP BY CUSTNMBR,ITEM
--QUERY_2 WITH AGGREGATE MAX
SELECT  CUSTNMBR,ITEM, 
MAX(CASE WHEN [MONTH] =2 THEN TOT_QTY ELSE 0 END) AS MONTH_2,
MAX(CASE WHEN [MONTH] =4 THEN TOT_QTY ELSE 0 END) AS MONTH_4,
MAX(CASE WHEN [MONTH] =5 THEN TOT_QTY ELSE 0 END) AS MONTH_5,
SUM(TOT_QTY) AS TOT_QTY_YEAR
FROM 
#CUST_ITEM
GROUP BY CUSTNMBR,ITEM

DROP TABLE #CUST_ITEM

Thanks in advance

  • 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-23T14:15:07+00:00Added an answer on May 23, 2026 at 2:15 pm

    It’s your case statements.

    When you write

    MIN(CASE WHEN [MONTH] =2 THEN TOT_QTY ELSE 0 END) AS MONTH_2,

    It takes the MINIMUM value of either 0 or tot_qty, which is gonna be 0 unless tot_qty is negative.

    Use NULL instead of 0, like:

    SELECT  CUSTNMBR,ITEM, 
    MIN(CASE WHEN [MONTH] =2 THEN TOT_QTY ELSE NULL END) AS MONTH_2,
    MIN(CASE WHEN [MONTH] =4 THEN TOT_QTY ELSE NULL END) AS MONTH_4,
    MIN(CASE WHEN [MONTH] =5 THEN TOT_QTY ELSE NULL END) AS MONTH_5,
    SUM(TOT_QTY) AS TOT_QTY_YEAR
    FROM 
    #CUST_ITEM
    GROUP BY CUSTNMBR, ITEM
    

    NULL is ignored for aggregate functions which I think is what you want.

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

Sidebar

Related Questions

I have the following code that fails to display object images. But displays normal
I have following code that displays an Image with letters, public class MainActivity extends
I have the following code that successfully displays an image in its column based
I have the following code that displays a JDialog , it shows a text
I have the following code that displays all folders and files within each folder.
I have the following code that displays the alerts depending on the result: How
I have the following code that shows a panel. It displays a button on
I have the following code that I use to display detail rows in a
I have following code that does not work due to a being a value
I have following code that does not work: I never get to goToFoodDetail .

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.