3 Replies Latest reply: Oct 25, 2017 11:06 AM by Ronald van der Does

# Double counting of values with Group by function

I am trying to group by purchase orders to count the total qty.

The results are double counting.

I have a unique key for each row and have tried autonumber, both have failed in reducing the duplication.

Issue:

result should be Qty 5.

Group by incorrectly return Qty 10.

Raw data:

 POitemKey Qty TrueKey 4500803485|00010 -5 2016|5000200614|0001 4500803485|00010 5 2016|5000200613|0001 4500803485|00010 5 2016|5000208014|0001

Group by code...

NoConcatenate

POitemKey as POitemKey_Open,

Key

From table123

Where [MVT_Type]='101' OR [MVT_Type]='102'; //receipts and returns

NoConcatenate

POitemKey,

Sum(Qty) as QtyFinal

Group by POitemKey;

Results after groupby

 POitemKey QtyFinal 4500803485|00010 10

Math...[=5+5+(-5) = 5]Result should be QtyFinal = 5.

I have also tried autonumber.

## Please any suggestions are greatly appreciated!!!

• ###### Re: Double counting of values with Group by function

Hi Brandon,

I think you're mixing up your keys (or I'm not getting it...)

```table123:
POitemKey, Qty, TrueKey
4500803485|00010, -5, 2016|5000200614|0001
4500803485|00010, 5, 2016|5000200613|0001
4500803485|00010, 5, 2016|5000208014|0001
];

NoConcatenate
POitemKey as POitemKey_Open,
TrueKey as Key
Resident table123;

NoConcatenate
POitemKey_Open,
Group by POitemKey_Open;
```

produces:

which is what I reckon you were trying to achieve, right?

Hope this helps you.

With kind regards,

Ronald

• ###### Re: Double counting of values with Group by function

Hi Ronald,

I agree with your example and it does work with the inline statement, but something weird is happening.  Here is an example.  Every time I try to sum the quantities or group by, it does not add properly.

Things I have tried:

1) Store the table and open in a new session of qlikview.

2) Format Qty field while loading as a num() to ensure that the negative sign is not affecting it.

3) NoConcatenate

The only thing I can get to work is Set Analysis.

sum(Qty) always seems to double the counts which doesn't make sense because the field 'Key' is a perfect Key.

If I group by below and sum qty I would expect to get 5.  But that does not happen.  Very odd.

Yet if I load inline as you show, it does work.

Thanks for the help,

Brandon

• ###### Re: Double counting of values with Group by function

Hi Brandon,

I suspect your source table has duplicate entries.

Can you load them with RecNo() to ensure every row is unique?

With kind regards,

Ronald