Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 
0li5a3a
Creator III
Creator III

Generate sequence numbers(1,2,3,etc) and reset it to 0 when is not in target

Hello,

I need to create a new column where I generate a sequence number if the Target is met and if not I need to put 0 and start again to generate a sequence number.

id, Date, Target
1, 01/01/2020, 10
1, 02/01/2020, 0
1, 03/01/2020, 6
1, 04/01/2020, 10
1, 05/01/2020, 10
2, 01/01/2020, 0
2, 02/01/2020, 0
2, 03/01/2020, 10
2, 04/01/2020, 10
2, 05/01/2020, 10

......

I need something like this 

id, Date, Target, New Col
1, 01/01/2020, 10, 1
1, 02/01/2020, 0, 0
1, 03/01/2020, 6, 0
1, 04/01/2020, 10, 1
1, 05/01/2020, 10, 2
2, 01/01/2020, 0, 0
2, 02/01/2020, 0, 0
2, 03/01/2020, 10, 1
2, 04/01/2020, 10,2
2, 05/01/2020, 10, 3

.......

Thanks in Advance!

C

Labels (1)
2 Solutions

Accepted Solutions
Taoufiq_Zarra

@0li5a3a  so if target is always 10 its ok if not you can replace 10 in script with Value or your target column:

Input:

LOAD * INLINE [
id, Date, Target
1, 01/01/2020, 10
1, 02/01/2020, 0
1, 03/01/2020, 6
1, 04/01/2020, 10
1, 05/01/2020, 10
2, 01/01/2020, 0
2, 02/01/2020, 0
2, 03/01/2020, 10
2, 04/01/2020, 10
2, 05/01/2020, 10
];

Final:
noconcatenate

load *,if(rowno()=1,1,if(peek(id)=id and Target=10,peek(New_Col)+1,0)) as New_Col resident Input order by id,Date;


drop table Input;

 

output:

Capture.PNG

Regards,
Taoufiq ZARRA

"Please LIKE posts and "Accept as Solution" if the provided solution is helpful "

(you can mark up to 3 "solutions") 😉

View solution in original post

Taoufiq_Zarra

@0li5a3a  you can change it to

Input:

LOAD * INLINE [
id, Date, Target
1, 01/01/2020, 10
1, 02/01/2020, 0
1, 03/01/2020, 6
1, 04/01/2020, 10
1, 05/01/2020, 10
2, 01/01/2020, 0
2, 02/01/2020, 0
2, 03/01/2020, 10
2, 04/01/2020, 10
2, 05/01/2020, 10
3,01/02/2020,10
3,02/02/2020,10
3,03/02/2020,10
3,04/02/2020,10
3,05/02/2020,10

];

Final:
noconcatenate

load *,if(rowno()=1,1,if(peek(id)=id and Target=10,peek(New_Col)+1,if(Target=10,1,0))) as New_Col resident Input order by id,Date;


drop table Input;

output:

Capture.PNG

or can you share the sample in Excel format

Regards,
Taoufiq ZARRA

"Please LIKE posts and "Accept as Solution" if the provided solution is helpful "

(you can mark up to 3 "solutions") 😉

View solution in original post

6 Replies
Taoufiq_Zarra

@0li5a3a  target is awlays 10 ?

Regards,
Taoufiq ZARRA

"Please LIKE posts and "Accept as Solution" if the provided solution is helpful "

(you can mark up to 3 "solutions") 😉
0li5a3a
Creator III
Creator III
Author

yes the target is 10 or above. thanks 

Taoufiq_Zarra

@0li5a3a  so if target is always 10 its ok if not you can replace 10 in script with Value or your target column:

Input:

LOAD * INLINE [
id, Date, Target
1, 01/01/2020, 10
1, 02/01/2020, 0
1, 03/01/2020, 6
1, 04/01/2020, 10
1, 05/01/2020, 10
2, 01/01/2020, 0
2, 02/01/2020, 0
2, 03/01/2020, 10
2, 04/01/2020, 10
2, 05/01/2020, 10
];

Final:
noconcatenate

load *,if(rowno()=1,1,if(peek(id)=id and Target=10,peek(New_Col)+1,0)) as New_Col resident Input order by id,Date;


drop table Input;

 

output:

Capture.PNG

Regards,
Taoufiq ZARRA

"Please LIKE posts and "Accept as Solution" if the provided solution is helpful "

(you can mark up to 3 "solutions") 😉
0li5a3a
Creator III
Creator III
Author

Many thanks !!!

0li5a3a
Creator III
Creator III
Author

@Taoufiq_Zarra , I mark the solution like is working but is not, the reason is if I have a new "id", e.g id= 3 and I have the target 10 the code is not putting an 1 in the New_Col.

constantin_olar_0-1602495138205.png

I believe if I restrict the rowno() = 1 to put 1 is ignoring all the other ids with are new .

  

Taoufiq_Zarra

@0li5a3a  you can change it to

Input:

LOAD * INLINE [
id, Date, Target
1, 01/01/2020, 10
1, 02/01/2020, 0
1, 03/01/2020, 6
1, 04/01/2020, 10
1, 05/01/2020, 10
2, 01/01/2020, 0
2, 02/01/2020, 0
2, 03/01/2020, 10
2, 04/01/2020, 10
2, 05/01/2020, 10
3,01/02/2020,10
3,02/02/2020,10
3,03/02/2020,10
3,04/02/2020,10
3,05/02/2020,10

];

Final:
noconcatenate

load *,if(rowno()=1,1,if(peek(id)=id and Target=10,peek(New_Col)+1,if(Target=10,1,0))) as New_Col resident Input order by id,Date;


drop table Input;

output:

Capture.PNG

or can you share the sample in Excel format

Regards,
Taoufiq ZARRA

"Please LIKE posts and "Accept as Solution" if the provided solution is helpful "

(you can mark up to 3 "solutions") 😉