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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:04:09+00:00 2026-05-30T04:04:09+00:00

I have an easy question. I’m trying to use coalesce in combination with a

  • 0

I have an easy question. I’m trying to use coalesce in combination with a group by clause. I want to get 0 values when the variable is null. Here is my sql server code:

SELECT SUM(COALESCE(NETTOTAL,0)) AS NETTOTAL,DATEPART(MM,RECORD_DATE) MONTH 
FROM ORDERS WHERE ORDER_EMPLOYEE_ID=#attributes.record_emp_id# 
GROUP BY DATEPART(MM,RECORD_DATE) ORDER BY MONTH

.. and my output:

<tr height="20">
    <td>Orders</td>
    <cfoutput query="get_orders"><td style="text-align:center;">#tlformat(nettotal,2)# - #month#</td></cfoutput>
</tr>

This code is just for the orders. There is also the sales row. Anyway here is the screenshot to make it more clear:

https://i.stack.imgur.com/VIAmr.png

To make it more clear I added the number of the month. As you can see the order is broken since there are no zero values for the other months…

P.S.Thank you all for the help! i really appreciate it!

  • 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-30T04:04:10+00:00Added an answer on May 30, 2026 at 4:04 am

    Your query is not at fault. You are trying to select from ORDERS based on each employee_id. You are then looping over it.

    If a given month has no orders, then there will be no row for it within the result set.

    Even if there were only orders for the last 4 months, they would get pushed to the first 4 as you are not checking that the month you are currently outputting matches the column header.

    For a bit of metacode, I would go down this route

    1 – create an array as follows

    arrMonths= [
        {orders=0,sales=0},
        {orders=0,sales=0}....
    ]
    

    This will give you a stc you can iterate over later.

    2 – I would then loop over each query

    <cfoutput query="get_orders">
        <cfset arrMonths[month].orders = nettotal>
    </cfoutput>
    

    3 – I would then iterate over the array

    <tr height="20">
        <td>Orders</td>
        <cfoutput from="1" to="#ArrayLen(arrMonths)#" index="thisMonth">
            <td style="text-align:center;">#tlformat(arrMonths[thisMonth].orders,2)# - #thisMonth#</td>
        </cfoutput>
    </tr>
    

    This way, every month will ALWAYS have a value even if it’s 0. You can also ditch the coalesce as the simple fact that rows with no orders have no records means they default to 0 so your query may become

    SELECT 
        SUM(COALESCE(NETTOTAL)) AS NETTOTAL,
        DATEPART(MM,RECORD_DATE) MONTH 
    FROM ORDERS 
    WHERE ORDER_EMPLOYEE_ID=<cfqueryparam cfsqltype="cf_sql_integer" value="#attributes.record_emp_id#">
    GROUP BY DATEPART(MM,RECORD_DATE)
    

    MONTH is now not necessary as it’s just inserting into the array which deals with ordering

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

Sidebar

Related Questions

Hopefully an easy question. If I have an object and I want to call
Very easy question. I have been using CSS for a while but rarely use
I have a quite easy jquery question that I can't get right, when clicking
I have been going crazy over a seemingly easy question with Python: I want
Just one easy question about default parameter values in the routing. If I have
I have an easy question that I can't seem to get my head around.
quick and hopefully easy question. Let's say I have a variable that is equal
Easy question, I have a stack panel with a toolbar and listbox, I want
seems like an easy question but I can't get this. I have a property
I have an easy question, though I'm not sure if it's doable or not:

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.