Skip to main content
Inspiring
May 31, 2008
Question

Rolling 12 month data

  • May 31, 2008
  • 2 replies
  • 378 views
I use the query below to give me summary data for three categories, for each supplier number, base on a month and year selection :

<cfquery name="qryGet_NORS" datasource="datasource">
select suplier_number,table2.supplier_name,
sum(case when date_sent is NULL and send_flag is NULL then 1 else 0 end) as cannot_send,
sum(case when date_sent is not NULL then 1 else 0 end) as total_send,
sum(case send_flag when 'No' then 1 else 0 end) as total_disapproved
FROM
table1 LEFT JOIN Master_Buyer_Table ON table1.buyer_number = Master_Buyer_Table.buyer_number
LEFT JOIN table2 ON table1.suplier_number = table2.supplier_number
where substring(convert(varchar,date_resolved,101), 1, 2) = '#form.selectmonth#'
and substring(convert(varchar,date_resolved,101), 7, 4) = '#form.selectyear#'
group by suplier_number,table2.supplier_name
</cfquery>

I then use this QoQ to give me the grand totals for each supplier number.

<cfquery dbtype="query" name="qryGet_Nors_sum">
select sum(cannot_send) as total_cannot_send,
sum(total_send) as total_total_send,
sum(total_disapproved) as total_total_disapproved
from qryGet_NORS
</cfquery>

What I need to do now is to get and output data for 12 months, starting with the input month and year. For example, if they enter March 2008, then the above query would give me the three totals for the period. But now, I need the totals for the next month, April 2008, then the totals for May 2008, etc, all the way to March 2009.

How would I modify the query to get all this information ?
    This topic has been closed for replies.

    2 replies

    Inspiring
    June 3, 2008
    First thing is I don't know why you do the following

    where substring(convert(varchar,date_resolved,101), 1, 2) = '#form.selectmonth#'
    and substring(convert(varchar,date_resolved,101), 7, 4) = '#form.selectyear#'


    Should be

    where Month(date_resolved) = #form.selectmonth#
    and Year(date_resolved) = #form.selectyear#


    Now for the modification

    <!---
    So we create a date for the first of the month selected and return everything that is greater
    --->
    where date_resolved > #createDate(form.selectYear, form.selectMonth, 1)#

    <!---
    To only return one year of data
    creatre another date that is one year and one month greater then return everything smaller
    --->
    and date_resolved < #createDate(form.selectYear+1, form.selectMonth+1, 1)#

    Ken
    Known Participant
    June 1, 2008
    This sounds too obvious so I'm assuming there's a catch somewhere but...

    <cfloop from="1" TO = "12" STEP="1" INDEX="monthNo">
    <code>
    </cfloop>

    with a little bit of magic to extract the monthNo from fields submitted

    Michael
    trojnfnAuthor
    Inspiring
    June 2, 2008
    I use this to extract the monthno : substring(convert(varchar,date_resolved,101), 1, 2)=monthno

    Applying your method, it gives me multiple lines per supplier number, based on the different months. What I need to show is one line per supplier number, with the three totals under one month. For example, for the first supplier number, the March heading would have the three totals for March, then the April would have the three totals for that month, etc.