0

I have this table (vol_week) with four columns. Example:

date_ent  | ped_kg | prd_kg | ref_kg
12/9/2017 | 345,22 | 321,23 | 12,56

I'm trying to calculate (ped_kg - prd_kg + ref_kg), but the query returns erratic values. Sometimes correct, sometimes close values.

SELECT date_ent, iif((Sum(ped_kg)-Sum(prd_kg)+Sum(ref_kg))<0,0,(Sum(ped_kg)-Sum(prd_kg)+Sum(ref_kg))) AS sld_kg
FROM vol_week
WHERE date_ent Is Not Null
GROUP BY date_ent
ORDER BY date_ent

I'm using MS Query in MSExcel.

EDIT: Problem solved :) The query was calculating using negative values. This way the query consider zeros from <0,0 condition:

SELECT date_ent, Sum(iif(((ped_kg-prd_kg)+ref_kg)<0,0,(ped_kg-prd_kg)+ref_kg)) AS sld_kg
FROM vol_week
WHERE date_ent Is Not Null
GROUP BY date_ent
ORDER BY date_ent

1 Answer 1

1

Your query is ok. Perhaps a problem with the data (format). Meybe incorrectly interpret the data returned by mquery Just a large single value for prd_kg so that the aggregate function returns 0 for the entire population (see example below) see example.

Try to re-prepare mquery by pasting the sql query into the dedicated field.

2
  • Thank you. You helped me see the problem. It was math. What I need is to calculate each row (ped_kg - prd_kg + ref_kg), returning zero for the negatives and then sum the results of the lines, grouping by date. I need help building the query. Thanks again!
    – Diogenes
    Commented Sep 8, 2017 at 15:58
  • @Diogenes please vote if I help you. Thanks.
    – adarti
    Commented Sep 8, 2017 at 16:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .