Skip to main content
Announcements
Have questions about Qlik Connect? Join us live on April 10th, at 11 AM ET: SIGN UP NOW
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Creating a new field from the Sum of an existing field

Hi. I'm new to QV and hope someone here can help me out 🙂

I have a table with two fields with this kind of structure:

Field1 - Field 2

10 - A
100 - A
1000 - A
20 - B
50 - C
500 - C

What I want to do is to take this information and create a new table with the sums of Field1 for represented for each Field2, like so:

1110 - A
20 - B
550 - C

I would like to do this in the script. What's the easiest way?

Thanks!

/M

1 Solution

Accepted Solutions
Not applicable
Author

Data:
LOAD * INLINE [
Field1,Field2
10,A
100,A
1000,A
20,B
50,C
500,C
];
LOAD Field2, Sum(Field1) As Total RESIDENT Data
GROUP BY Field2;


View solution in original post

11 Replies
Not applicable
Author

Data:
LOAD * INLINE [
Field1,Field2
10,A
100,A
1000,A
20,B
50,C
500,C
];
LOAD Field2, Sum(Field1) As Total RESIDENT Data
GROUP BY Field2;


Not applicable
Author

Hi

try this:


Tbl:
Load * inline
[Val, Type
0, A
100, A
1000, A
20, B
50, C
500, C
];

sum:
Load Type,
sum(Val) as Amount
Resident Tbl
Group by Type;

// drop table Tbl;




Not applicable
Author

Hi NMiller,

I am working hard, next time .... Smile

Many Regards

Roland

Not applicable
Author

Thanks!

And if I want to load the data from an external QVD file? I tried FROM at the end but can't get it to work?

Not applicable
Author

Hi,

use the (file-) wizard to create your default load-statement and do the rest like grouping etc. manually.

Regards Roland

Not applicable
Author

Okay thanks! Got that to work.

But I have one further question, a bit trickier I assume

I want to that on every row the sum of that row AND all previous rows should be in the field.

Using the above example that would mean:

A - 1110

B - 1130

c - 1680

How do I accomplish this?

Not applicable
Author

Hi,

sorry, the weekend is waiting.

Not applicable
Author

This is a bit of a guess, but try:

Data:
LOAD * INLINE [
Field1,Field2
10,A
100,A
1000,A
20,B
50,C
500,C
];
Data2:
LOAD Field2, Sum(Field1) As Total RESIDENT Data
GROUP BY Field2;
Data3:
LOAD Field2, Total,
If(Previous(Total) > 0, Previous(Total), Total) As TempTotal
RESIDENT Data2;
Data4:
LOAD Field2,
If(Previous(Total) > 0, Previous(TempTotal) + Total, Total) As RunningTotal
RESIDENT Data3;


I tested it in a sample app, but I admit it is pretty ugly. There's probably a way to clean it up a bit.

Not applicable
Author

Okay yeah I understand how this works thanks! , but the actual table I'm using have thousands of rows Stick out tongue Maybe some sort of looping mechanism would do it? How can I accoplish this?