Skip to main content
August 24, 2008
Question

Subtotal SQL Statement

  • August 24, 2008
  • 4 replies
  • 644 views
I am working with a database table that has a list projects. Some of these projects have sub-projects. There is a column with weekending dates with corespond to manhours associated with that week. Here is an example.

JobNum SubJobNum WeekEndingDate Manhours
--------------------------------------------------------------------------
1---------------001---------------8-17-08------------200
1---------------001---------------8-24-08------------300
1---------------001---------------8-31-08------------250
1---------------001---------------9-7-08--------------100
1---------------002---------------8-17-08-------------50
1---------------002---------------8-24-08---------------0
1---------------002---------------8-31-08-------------90
2---------------045---------------8-17-08------------100
2---------------045---------------8-17-08-------------50

I guess you get the point. What I am trying to accomplish is query the running total of manhours by WeekEndingDate and by Job Number. Ultimately I am trying to get a percent complete planned. Each date would have the subtotal of total manhours divided by total manhours.

This is what I would like to output:

JobNum TotalWeekEndingManhours WeekEndingDate %Comp
----------------------------------------------------------------------------------------
1--------------------------250--------------------------8-17-08-------------25%
1--------------------------300--------------------------8-24-08-------------56%
1--------------------------340--------------------------8-31-08-------------90%
1--------------------------100--------------------------9-7-08--------------100%

This the query that I am working with so far.

<!--- Get the raw data from the database. --->
<cfquery name="GetPerc" datasource="db1">
SELECT p.WeekEndingDate,
SUM(p.Planned) AS TotPlanned,
(SUM(p2.Planned*1^(p.WeekEndingDate-p2.WeekEndingDate))) AS TotPercPlanned,
SUM(p.Earned) AS SumEarned,
SUM(p.WeeklyEarned) AS SumWeekly,
(SUM(p.Planned)/#GetTotPlanned.TotPlanned#)*100 AS PercPlanned,
(SUM(p.Earned)/#GetTotPlanned.TotPlanned#)*100 AS PercEarned,
(DAY(p.WeekEndingDate) & '/' & MONTH(p.WeekEndingDate) & '/' & YEAR(p.WeekEndingDate)) AS
formattedDate
FROM Productivity p LEFT JOIN Productivity p2
ON p.WeekEndingDate > p2.WeekEndingDate
WHERE p.JobNumber = '#URL.JobNumber#'
GROUP BY p.WeekEndingDate,
p.JobNumber

Anyone has any suggestions?
This topic has been closed for replies.

4 replies

Inspiring
August 25, 2008
QuerySetCell() would be really useful for this situation. Usage is described in the cfml reference manual. If you don't have one, the internet does.
Inspiring
August 25, 2008
By doing exactly what I said earlier, you can change query results after you run the query and then use it in your graph.
August 25, 2008
I have written some code to give me what I am looking for.

<cfchartseries type="line" serieslabel="Actual">
<cfset PlannedTot = "0">
<cfloop query="GetPerc">
<cfoutput>#DateFormat(WeekEndingDate,'medium')# - #TotPlanned# - #PlannedTot+TotPlanned#
<br /><cfset PlannedTot = "#(TotPlanned+PlannedTot)#">

<cfchartdata item="#j#" value="PlannedTot"></cfoutput></cfloop>
</cfchartseries>
August 25, 2008
Ok. Maybe I am limiting myself. What I am retrieving this info for is for a chart. I know that the cfchart query attribute is available. I could get this info fairly easily by doing the math outside of the query but I am not sure that I can display query and non query info on the same chart. Granted I am very new to cfchart and its abilities. If I could use the query info along with looped data I would click my heals.
Inspiring
August 24, 2008
At least one db allows you to select running totals in your query, but yours might not. The alternative is to select some contstants in your query, then loop through it and do whatever math needs doing.