Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Qlik Open Lakehouse is Now Generally Available! Discover the key highlights and partner resources here.
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

sum function in script

Hello fellow users,

I would like to do a calculation in the script and I don't know how and if it is possible. Here is an excample:

Table 1a:

ContractSub-contractForcastTotalCostForecastActualCostResult
30003000-a40002000GOOD
30003000-b25003000BAD
30013001-a30002000GOOD
30013001-b200400BAD


I would like to calculate the column Result with the following formula:

If(ForcastTotalCost >ForecastActualCost , GOOD, BAD) AS Result.

The Column result gives the following outcome as shown on table 1a above.

The problem now is that the script calculated per row and a row is a Sub-Contract. I would like to do the calculation per Contract, because on a contract total the Colum result should be GOOD and now 3000-a is GOOD and 3000-b is BAD. Does someone know how I can Sum the rows and do the calculation like on table b below??

ContractSub-contractForcastTotalCostForecastActualCostResult
30003000-a40002000GOOD
30003000-b25003000GOOD
30013001-a30002000GOOD
30013001-b200400GOOD


Hope this is understandable for you guys.

Regards,

Aissam

3 Replies
Not applicable
Author

Try the following Script

Table_A:

LOAD

Contract,

[Sub-contract],

ForcastTotalCost,

ForecastActualCost

FROM DB;

Table_B:

NOCONCATENATE LOAD

Contract,

if(sum(ForcastTotalCost)>sum(ForecastActualCost),'GOOD','BAD') as Result

FROM DB

Group By Contract;

Not applicable
Author

Thanks for your reply. But the Group By gives problems. I receive the following error message when I run the script. Do you know what is wrong with this script?

Aggregation expressions required by GROUP BY clause

LOAD

JC.CONTRACT_JOB,

JC.OpenPoTotal,

JC.LABOR_ACTUAL,

JC.LABOR_ACTUAL as LaborActual,

JC.BURDEN_ACTUAL,

JC.LABOR_EST,

JC.BURDEN_EST,

JC.ENG_ACTUAL,

JC.ENG_ACTUAL as EngActual,

JC.EXT_COST_ACTUAL,

JC.ENGINEERING_EST,

JC.MAT_ACTUAL + JC.EXT_COST_ACTUAL as MaterialProjects,

JC.MAT_ACTUAL,

JC.MAT_ACTUAL + JC.EXT_COST_ACTUAL as MatActual,

JC.MAT_ACTUAL + JC.EXT_COST_ACTUAL + JC.LABOR_ACTUAL + JC.ENG_ACTUAL as TotalCostActual,

JC.MATERIAL_EST,

JC.MATERIAL_EST - JC.FREIGHT_EST as MatForcast,

JC.MATERIAL_EST - JC.FREIGHT_EST + JC.LABOR_EST + JC.ENGINEERING_EST as TotalCostForecast,

if((JC.LABOR_ACTUAL+JC.ENG_ACTUAL+JC.MAT_ACTUAL +JC.EXT_COST_ACTUAL+JC.OpenPoTotal) > (JC.MATERIAL_EST - JC.FREIGHT_EST +JC.LABOR_EST + JC.ENGINEERING_EST),

(JC.LABOR_ACTUAL+JC.ENG_ACTUAL+JC.MAT_ACTUAL+ JC.EXT_COST_ACTUAL+JC.OpenPoTotal),

(JC.MATERIAL_EST - JC.FREIGHT_EST +JC.LABOR_EST +JC.ENGINEERING_EST)) as TotalActualCostRedsheetContractLevel

RESIDENT JC

Group By JC.CONTRACT_JOB

johnw
Champion III
Champion III

Remove that group by. It doesn't belong on that load. Add a NEW load after it. Unfortunately, you didn't use the same field names in your example as you do in your actual script, so I'm just guessing at which field names you want in the second table, but you want to do exactly what he said but with your field names:

Table_B:
NOCONCATENATE LOAD
JC.CONTRACT_JOB,
if(sum(TotalCostForecast)>sum(TotalCostActual),'GOOD','BAD') as Result
RESIDENT JC
GROUP BY JC.CONTRACT_JOB;