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: 
femidadzie
Contributor II
Contributor II

Aggr function count distinct in a script

Dear Community,

iam facing a problem here and i couldn't find a proper solution in the community, so i hoped that maybe someone could help me.

Iam trying to create a field in the script which counts the distinct material for each order with the status 'mp'.

Iam not gettin the 'distinct' charackter into the script.

I will add a file, including the wished result in hope this helps a bit.

I would be really thankful if someone could help me.

Many thanks in advance.

/////////////////////////////////////////////////////////////

Basis:
LOAD
    OrderID,
    MaterialID,
    MaterialStatus,
    If(MaterialStatus='mp',1,0) as Statusmp
FROM Desktop;

 Qualify ;
Unqualify OrderID;

temp:
 Load
    OrderID,
    MaterialID,
    Sum(Statusmp) as Numbermp

Resident Basis
Group By [OrderID], [MaterialID];

////////////////////////////////////////////////////////////////////

 

 

 

1 Solution

Accepted Solutions
sunny_talwar

Try this

Basis:
LOAD
    OrderID,
    MaterialID,
    MaterialStatus
FROM Desktop;

Qualify ;
Unqualify OrderID;

temp:
 Load OrderID,
    Count(DISTINCT If(MaterialStatus = 'mp', MaterialID)) as Numbermp
Resident Basis
Group By [OrderID];

View solution in original post

8 Replies
sunny_talwar

Based on the data provided what do you expect to see as your final output?
lironbaram
Partner - Master III
Partner - Master III

hi 

i would do something like this :

Basis:
LOAD
    OrderID,
    MaterialID,
    MaterialStatus

FROM Desktop;

Basis2:

load *,
    If(MaterialStatus='mp' and previous(MaterialID)<>MaterialID ,1,0) as Statusmp
resident Basis

order by OrderID,MaterialID;

drop table Basis;

 

temp:
 Load
    OrderID,
    Sum(Statusmp) as Numbermp

Resident Basis2
Group By [OrderID];

zoltann14
Contributor II
Contributor II

You can just add DISTINCT to your Basis table load and then sum everything Grouping by OrderID:
Basis:
LOAD distinct
OrderID,
MaterialID,
MaterialStatus,
IF(MaterialStatus='mp',1,0) as Statusmp
FROM Desktop;

temp:
LOad OrderID,
SUM(Statusmp) as Numbermp
Resident Basis
Group by OrderID;
femidadzie
Contributor II
Contributor II
Author

Hello Stalwar1

 

I would like to see based on the basis table,

Basis  
OrderIDMaterialIDMaterialStatus
A1wo
A1mp
A2mp
A2mp
B1wo
B1wo
B2wo
B2wo
B3mp
C2wo
C3wo
C3wo
D2mp
D2wo
D3mp
D3mp
D1mp
D1wo

 

the following result:

 

OrderIDNumberOfMP
A2
B1
C0
D3
sunny_talwar

Try this

Basis:
LOAD
    OrderID,
    MaterialID,
    MaterialStatus
FROM Desktop;

Qualify ;
Unqualify OrderID;

temp:
 Load OrderID,
    Count(DISTINCT If(MaterialStatus = 'mp', MaterialID)) as Numbermp
Resident Basis
Group By [OrderID];
femidadzie
Contributor II
Contributor II
Author

Thank´you very much that worked.
BS
Femi
femidadzie
Contributor II
Contributor II
Author

Thanks!
femidadzie
Contributor II
Contributor II
Author

Thank you!