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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:50:18+00:00 2026-05-23T19:50:18+00:00

As I was advised by a good man and programmer I should simplify my

  • 0

As I was advised by a good man and programmer I should simplify my table. So far I have made a new table (x-month,y-cities,value-Nettotal) it works, but still I didn’t understand why it can’t group the values (nettotal) by cities. It’s OK with month, but the values just come starting from left to right without any 0 left behind. Anyway I hope you will understand everything from the source:

here are the queries:

<cfquery name="GET_SALES_TOTAL" datasource="#dsn#">
SELECT
    SUM(COALESCE(nettotal,0)) nettotal,
    SC.CITY_ID,
    DATEPART(MM,INVOICE_DATE) AY,
    C.FULLNAME,
    SC.CITY_NAME
FROM    
    #DSN2_ALIAS#.INVOICE I,
    SETUP_CITY SC,
    COMPANY C
WHERE
    C.COMPANY_ID=I.COMPANY_ID
    AND SC.CITY_ID=C.CITY
    AND PURCHASE_SALES=1
GROUP BY
    DATEPART(MM,INVOICE_DATE),
    SC.CITY_ID,
    C.FULLNAME,
    SC.CITY_NAME
ORDER BY 
      AY,CITY_ID
</cfquery>

<cfquery name="GET_SALES_GRAND_TOTAL" datasource="#dsn#">
SELECT SUM(NETTOTAL) NETTOTAL,SC.CITY_ID,SC.CITY_NAME
FROM #DSN2_ALIAS#.INVOICE I,SETUP_CITY SC,COMPANY C
WHERE C.COMPANY_ID=I.COMPANY_ID AND SC.CITY_ID=C.CITY AND PURCHASE_SALES=1
GROUP BY SC.CITY_ID, SC.CITY_NAME
ORDER BY CITY_ID,CITY_NAME
</cfquery>

and here is the table itself:

<table cellpadding="3" cellspacing="1" class="color-border">
<tr class="color-header">
    <td></td>
    <cfoutput query="GET_SALES_GRAND_TOTAL" group="city_id">
        <td>#city_name#</td>
    </cfoutput>
</tr>
<cfoutput query="GET_SALES_TOTAL" group="AY"><!--- months first --->
    <tr class="color-row"><!--- month-specific stuff goes here --->
        <td>#ay#</td>
        <cfoutput group="city_id"><!--- city-specific stuff --->
            <td>#tlformat(nettotal,2)#<!--- format NETTOTAL however you want here ---></td>
        </cfoutput>
    </tr>
</cfoutput>
<tr class="color-header">
    <td>City Overal</td>
    <cfoutput query="GET_SALES_GRAND_TOTAL" group="city_id">
        <td>#tlformat(nettotal,2)#<!--- format NETTOTAL here ---></td>
    </cfoutput>
</tr>
</table>

Here is the screenshot to make it more clear:

screenshot

The other stuff except grouping the cities works perfectly!

  • 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-23T19:50:20+00:00Added an answer on May 23, 2026 at 7:50 pm

    It looks like the problem that you’re having is that there is not a record for every combination of month and city. The setup as it is now is fine if every city has sales every month, but we can improve this code so that it handles “missing” entries properly.

    Some databases have specific syntax that will help you make a table like this: for example, Access has TRANSFORM, Oracle and SQL server have CUBE and ROLLUP, MySQL has WITH ROLLUP. However, you still might have to tweak your CF even after adjusting the query, so let’s try working with what you have in a more general form.

    Instead of an inner join, we want an outer join: all cities plus sales for any city that has sales. However, we also need all months. In this particular instance, we can use a Cartesian product to get months and cities. (Be very careful about these: you should only use them in situations where you really do want one record for every combination of field A and field B. It’s easy to use one in the wrong place and get 10,000 records instead of 100.)

    Let’s take Johan’s suggestion and modify it some:

    SELECT SUM(COALESCE(NETTOTAL,0)) NETTOTAL
      ,SC.CITY_ID
      ,SC.CITY_NAME
      ,M.INVOICE_MONTH                     
    FROM SETUP_CITY SC
      LEFT OUTER JOIN COMPANY C ON SC.CITY_ID = C.CITY                   
      LEFT OUTER JOIN #DSN2_ALIAS#.INVOICE I ON C.COMPANY_ID = I.COMPANY_ID
      , (SELECT DISTINCT DATEPART(MM,INVOICE_DATE) INVOICE_MONTH FROM #DSN2_ALIAS#.INVOICE) M
    WHERE PURCHASE_SALES = 1   
      AND DATEPART(MM,I.INVOICE_DATE) = M.INVOICE_MONTH                  
    GROUP BY SC.CITY_ID, SC.CITY_NAME, M.INVOICE_MONTH
    ORDER BY SC.CITY_NAME, M.INVOICE_MONTH 
    

    I’m a little fuzzy on the format for a Cartesian product with other inner joins present, so give this a try and we can modify it as needed.

    That should give you a query with a value for every city for each month with sales. The exact form of the outer join may need to change, depending on the database you’re using, but that should at least move you in the right direction.

    Note that you will need to change your CF to match the revised query, but just this part, I think:

    <cfoutput query="GET_SALES_TOTAL" group="AY"><!--- months first --->
        <tr class="color-row"><!--- month-specific stuff goes here --->
            <td>#ay#</td>
    

    becomes

    <cfoutput query="GET_SALES_TOTAL" group="INVOICE_MONTH"><!--- months first --->
        <tr class="color-row"><!--- month-specific stuff goes here --->
            <td>#invoice_month#</td>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It is advised to use override instead of new key word in C#. Why
People always advised me that if I am doing some application that should use
Good day. I have a PHP & MySql background, and I'm just starting with
I have a simple mvc project consisting of one table named Persons and that
I have a table of data in MS Access 2007. There are 6 fields
I was wondering if anyone could advise on a good pattern for loading textures
After asking this question I was advised not to check in binaries into subversion.
As I've read somewhere it is advised to use !== and === instead.
My understanding is that it's advised testers are separate from developers, i.e you obviously
In GWT javadoc, we are advised If you only need a simple label (text,

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.