I have a list of Employee names and Salaries in the following order
I need to create the output table in the below format. ie, whenever the accumulated salary-total crosses 3000 I have to detect that and mark that row.
I have tried to do row_cumsum and reset the Term once it crossed 3000 but it didn't work for the second iteration.
datatable (name:string, month:int, salary:long)
[
"Alice", 1, 1000,
"Alice", 2, 2000,
"Alice", 3, 1400,
"Alice", 3, 1400,
"Alice", 3, 1400,
]
| order by name asc, month asc
| extend total=row_cumsum(salary)
| extend total=iff(total >=3000,total-prev(total),total)
This is now possible with scan operator:
datatable (name:string, salary:long)
[
"Alice", 1000,
"Alice", 2000,
"Alice", 1400,
"Alice", 1400,
"Alice", 1400,
"Alice", 1000,
"Bob", 2400,
"Bob", 1000,
"Bob", 1000
]
| sort by name asc
| scan declare (total:long) with
(
step s: true => total = iff(isnull(s.total) or name != s.name, salary, iff(s.total < 3000, s.total + salary, salary));
)
| extend boundary_detected = iff(total >= 3000, 1, long(null))
name
salary
total
boundary_detected
Alice
1000
1000
Alice
2000
3000
1
Alice
1400
1400
Alice
1400
2800
Alice
1400
4200
1
Alice
1000
1000
Bob
2400
2400
Bob
1000
3400
1
Bob
1000
1000
Related
Here is my data set in kusto and I am trying to generate "releaseRank" column based on the release column value.
Input Dataset:
let T = datatable(release:string, metric:long)
[
"22.05", 20,
"22.04", 40,
"22.03", 50,
"22.01", 560
];
T
|take 100;
desired output :
found that there is serialize and row_number kusto
T
|serialize
|extend releaseRank = row_number()
|take 100;
But if the release value is repeated, i need the releaseRank to be same for eg. given the data set, i am not getting the desired output
T = datatable(release:string, metric:long)
[
"22.05", 20,
"22.05", 21,
"22.04", 40,
"22.03", 50,
"22.01", 560
];
T
|serialize
|extend releaseRank = row_number()
|take 100;
expected output
This should do what we want.
let T = datatable(release:string, metric:long)
[
"22.05", 20,
"22.05", 21,
"22.04", 40,
"22.03", 50,
"22.01", 560
];
T
| sort by release desc , metric asc
| extend Rank=row_rank(release)
22.05 20 1
22.05 21 1
22.04 40 2
22.03 50 3
22.01 560 4
I have a table called MyTable that contains the following column headers
MachineID
RunID
Time
The table looks something kind of like this
What I want to do with my query is to group by RunID but still show MachineID. That way I can get something like this
I have tried something like this
MyTable
| distinct RunID
But that only shows the distinct runId, I also want to show the MachineID associated with it and I'm not sure of how to do that.
you can add the machine ID as another aggregation key, e.g.
datatable(machine_id:int, run_id:int, timestamp:datetime)
[
1, 4321, datetime(2021-04-13 01:00),
1, 4321, datetime(2021-04-13 01:01),
1, 7654, datetime(2021-04-13 12:00),
1, 7654, datetime(2021-04-13 12:01),
2, 5667, datetime(2021-04-13 02:30),
2, 5667, datetime(2021-04-13 02:31),
3, 4867, datetime(2021-04-13 04:30),
4, 2430, datetime(2021-04-13 05:00),
4, 2430, datetime(2021-04-13 05:01),
4, 2430, datetime(2021-04-13 05:02),
]
| distinct machine_id, run_id
machine_id run_id
---------- ------
1 4321
1 7654
2 5667
3 4867
4 2430
I am stuck with a Kusto query.
This is what I want to do - I would like to show day wise sales amount with the previous month's sales amount on the same day.
datatable(DateStamp:datetime, OrderId:string, SalesAmount:int)
[
"02-01-2019", "I01", 100,
"02-01-2019", "I02", 200,
"02-02-2019", "I03", 250,
"02-02-2019", "I04", 150,
"02-03-2019", "I13", 110,
"01-01-2019", "I10", 20,
"01-02-2019", "I11", 50,
"01-02-2019", "I12", 30,
]
| extend SalesDate = format_datetime(DateStamp, 'MM/dd/yyyy')
| summarize AmountOfSales = sum(SalesAmount) by SalesDate
This is what I see.
And, instead this is what I want to show as result --
I couldn't figure out how to add multiple summarize operator in one query.
Here's an option:
datatable(DateStamp:datetime, OrderId:string, SalesAmount:int)
[
"02-01-2019", "I01", 100,
"02-01-2019", "I02", 200,
"02-02-2019", "I03", 250,
"02-02-2019", "I04", 150,
"02-03-2019", "I13", 110,
"01-01-2019", "I10", 20,
"01-02-2019", "I11", 50,
"01-02-2019", "I12", 30,
]
| summarize AmountOfSales = sum(SalesAmount) by bin(DateStamp, 1d)
| as hint.materialized = true T
| extend prev_month = datetime_add("Month", -1, DateStamp)
| join kind=leftouter T on $left.prev_month == $right.DateStamp
| project SalesDate = format_datetime(DateStamp, 'MM/dd/yyyy'), AmountOfSales, AmountOfSalesPrevMonth = coalesce(AmountOfSales1, 0)
SalesDate
AmountOfSales
AmountOfSalesPrevMonth
01/01/2019
20
0
01/02/2019
80
0
02/01/2019
300
20
02/02/2019
400
80
02/03/2019
110
0
I start with a list of failures that take place in locations
failureName, failureLocation
failure a, location 1
failure b, location 1
failure a, location 2
failure a, location 1
<etc>
I can transform that into this table by using summarize count() by location
failureName, failureLocation, count
failure a, location 1, 100
failure a, location 2, 50
failure b, location 1, 10
<etc>
I'd like to transform the counts into percent on a per. failure basis, so I need to add a sum per failure name. My goal is to end up with this table:
failureName, failureLocation, count, sumPerFailureName
failure a, location 1, 100, 150
failure a, location 2, 50, 150
failure b, location 1, 10, 10
<etc>
Suggestions?
Try this, to take you from your 2nd table to the 3rd (and extend a calculated column of the percentage):
let T =
datatable(failureName:string, failureLocation:string, ['count']:long)
[
'failure a', 'location 1', 100,
'failure a', 'location 2', 50,
'failure b', 'location 1', 10,
]
;
T
| summarize sumPerFailureName = sum(['count']) by failureName
| join
(
T
) on failureName
| project failureName, failureLocation, ['count'], sumPerFailureName, percentage = round(100.0 * ['count'] / sumPerFailureName, 2)
I am using groovy and I have a collection :
person 1: age - 1, weight - 25
person 2: age - 2, weight - 20
person 3: age - 3, weight - 25
I need to find all persons whose age or weight is in the list of valid age/weight returned by a method called getValidAgeForSchool() or getValidWeightForSchool() ex. ages [2,3] or weight [20,25]
I know there is something like this (not working too)
persons.findAll{ it.age == 2 || it.weight == 20}
but how I can say (like the IN Clause)
persons.findAll {it.age in [2,3] || it.weight in [20,25]}.
I also tried this (ignoring the weight for now) but not returning the list when it is supposed to
persons.age.findAll{ it == 2 || it == 3}
thanks.
The code you have works:
def people = [
[ id: 1, age: 1, weight: 25 ],
[ id: 2, age: 2, weight: 20 ],
[ id: 3, age: 3, weight: 25 ]
]
// This will find everyone (as everyone matches your criteria)
assert people.findAll {
it.age in [ 2, 3 ] || it.weight in [ 20, 25 ]
}.id == [ 1, 2, 3 ]
It also works if you have a list of instances like so:
class Person {
int id
int age
int weight
}
def people = [
new Person( id: 1, age: 1, weight: 25 ),
new Person( id: 2, age: 2, weight: 20 ),
new Person( id: 3, age: 3, weight: 25 )
]
I'm assuming your problem is that you have weight as a double or something?
If weight is a double, you'd need to do:
people.findAll { it.age in [ 2, 3 ] || it.weight in [ 20d, 25d ] }.id
But beware, this is doing double equality comparisons, so if you are doing any arithmetic on the weight, you may fall victim to rounding and accuracy errors