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
Related
Is there a way to make an Application Insights kusto query sort on performanceBucket 'correctly', i.e. on bucket duration? When I summarize or sort using performanceBucket and don't specify a sort I get something like this (note for example that 1-3sec is not adjacent to 3-7sec):
If I add a sort by performanceBucket it's done alphanumerically:
I want it to be in this order (or the reverse of it)
<250ms
250ms-500ms
500ms-1sec
1sec-3sec
3sec-7sec
7sec-15sec
15sec-30sec
30sec-1min
1min-2min
You need to artificially add a column that indicates your preferred sorting order, then sort by it, and project it away:
// Synthetic data - don't copy this
let YourResult = datatable(perfBucket:string, count_:long) [
"250ms-500ms", 14000,
"7sec-15sec", 600,
"1sec-3sec", 9700
];
// This is the actual query
YourResult
| extend sortOrder =
case(perfBucket == "<250ms", 1,
perfBucket == "250ms-500ms", 2,
perfBucket == "500ms-1sec", 3,
perfBucket == "1sec-3sec", 4,
perfBucket == "3sec-7sec", 5,
perfBucket == "7sec-15sec", 6,
perfBucket == "15sec-30sec", 7,
perfBucket == "30sec-1min" ,8,
perfBucket == "1min-2min", 9,
10)
| order by sortOrder asc
| project-away sortOrder
Result:
perfBucket
count_
250ms-500ms
14000
1sec-3sec
9700
7sec-15sec
600
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
Input file is:
2, cornflakes, Regular,General Mills, 12
3, cornflakes, Mixed Nuts, Post, 14
4, chocolate syrup, Regular, Hersheys, 5
5, chocolate syrup, No High Fructose, Hersheys, 8
6, chocolate syrup, Regular, Ghirardeli, 6
7, chocolate syrup, Strawberry Flavor, Ghirardeli, 7
filter3 = LOAD 'location_of_file' using PigStorage('\t') as (item_sl : int, item : chararray, type: chararray, manufacturer: chararray, price : int);
SPLIT filter3 INTO filter4 IF (FOREACH (filter3 GROUP BY item) GENERATE group, COUNT(item < 3)), filter6_pass OTHERWISE;
It is like having a SQL with a group by on item having count(*) < 3
The desired output is:
4, chocolate syrup, Regular, Hersheys, 5
5, chocolate syrup, No High Fructose, Hersheys, 8
6, chocolate syrup, Regular, Ghirardeli, 6
7, chocolate syrup, Strawberry Flavor, Ghirardeli, 7
Group by item, get the count and then use filter on the count
A = LOAD 'location_of_file' using PigStorage('\t') as (item_sl : int, item : chararray, type: chararray, manufacturer: chararray, price : int);
B = GROUP A BY item;
C = FOREACH B GENERATE group,COUNT(A.item) AS Total;
D = FILTER C BY Total > 3;
E = JOIN A BY item,D BY $0;
F = FOREACH E GENERATE $0..$4;
DUMP F;
I currently have a QListWidget that displays many items that are user selectable (and dragable). In my application, when an item is checked it will be reordered above the unchecked items. The user can also drag/drop to adjust the order of the checked items.
The problem the users have is that there are a TON of these check boxes and they are not grouped logically on the screen. Thus, I'd like to introduce grouping of some kind. Here is an example of how it currently works.
from PyQt4 import QtGui, QtCore
import sys
rows = [
{'text': 'Row1', 'value': 1, 'group': 1},
{'text': 'Row2', 'value': 2, 'group': 1},
{'text': 'Row3', 'value': 3, 'group': 1},
{'text': 'Row4', 'value': 4, 'group': 2},
{'text': 'Row5', 'value': 5, 'group': 2},
{'text': 'Row6', 'value': 6, 'group': 3},
{'text': 'Row7', 'value': 7, 'group': 3},
{'text': 'Row8', 'value': 8, 'group': 3},
{'text': 'Row9', 'value': 9, 'group': 2},
{'text': 'Row10', 'value': 10, 'group': 'testing'}
]
class MyList(QtGui.QListWidget):
def __init__(self):
QtGui.QListWidget.__init__(self)
for row in rows:
item = QtGui.QListWidgetItem(row['text'])
# These are utilizing the ItemDataRole; 33 and 34 are among the first user defined values
# http://pyqt.sourceforge.net/Docs/PyQt4/qt.html#ItemDataRole-enum
item.setData(33, row['value'])
item.setData(34, row['group'])
item.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable)
item.setCheckState(QtCore.Qt.Unchecked)
self.addItem(item)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
my_list = MyList()
my_list.show()
sys.exit(app.exec_())
This produces an application like this:
What I want to do is group the items that have similar groups under a similar heading. When an item is checked, it'd appear above the groups. Initially, this looks like a QTreeWidget/View, except that the checked items need to appear outside of the existing tree.
Example (text output):
[CHECKED ITEMS APPEAR HERE]
Group 1
Row1
Row2
Row3
Group 2
Row4
Row5
Row9
Group 3
Row6
Row7
Row8
Group testing
Row10
Is there a way to group items in a QListWidget, preferable so that the 'header' can be selected and all child elements can be autoselected?
to list items in groups here a simalar question: How to list items as groups in QListWidget
if headeritems and normal items are different in one property they can be handled differently in slot.
I'm not quite certain, if it's the way you want. I tried to place checked items on the top and select all items of a group by click on the headeritem.
By selecting another signal and modyfiing the slot there are many possibilities, to change the behaviour. Here my code (i tried it in PyPt5 by replacing QtGui by QtWidgets
from PyQt4 import QtGui, QtCore
import sys
rows = [
{'text': 'Row1', 'value': 1, 'group': 1},
{'text': 'Row2', 'value': 2, 'group': 1},
{'text': 'Row3', 'value': 3, 'group': 1},
{'text': 'Row4', 'value': 4, 'group': 2},
{'text': 'Row5', 'value': 5, 'group': 2},
{'text': 'Row6', 'value': 6, 'group': 3},
{'text': 'Row7', 'value': 7, 'group': 3},
{'text': 'Row8', 'value': 8, 'group': 3},
{'text': 'Row9', 'value': 9, 'group': 2},
{'text': 'Row10', 'value': 10, 'group': 'testing'}
]
grouptitles = [1, 2, 3,'testing'] # list of grouptitles
def gruppe(d): # function for sorting the itemlist
return str(d['group'])
rows.sort(key=gruppe,reverse=False) # sort rows by groups
class MyList(QtGui.QListWidget):
def __init__(self):
QtGui.QListWidget.__init__(self)
self.setMinimumHeight(270)
for t in grouptitles:
item = QtGui.QListWidgetItem('Group {}'.format(t))
item.setData(33, 'header')
item.setData(34, t)
item.setFlags(QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable)
self.addItem(item)
for row in rows:
if row['group'] == t:
item = QtGui.QListWidgetItem(row['text'])
# These are utilizing the ItemDataRole; 33 and 34 are among the first user defined values
# http://pyqt.sourceforge.net/Docs/PyQt4/qt.html#ItemDataRole-enum
item.setData(33, row['value'])
item.setData(34, row['group'])
item.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable)
item.setCheckState(QtCore.Qt.Unchecked)
self.addItem(item)
else:
pass
self.setSelectionMode(QtGui.QAbstractItemView.MultiSelection) #
self.itemClicked.connect(self.selManager) # select an appropriate signal
def selManager(self, item):
if item.data(33) == 'header':
groupcode = item.data(34)
for i in range(0,self.count()):
if self.item(i).data(34) == groupcode and self.item(i).data(33) != 'header':
b = True if self.item(i).isSelected() == False else False
self.item(i).setSelected(b)
else:
if item.checkState() == QtCore.Qt.Unchecked:
item.setCheckState(QtCore.Qt.Checked)
self.moveItem(self.currentRow(),0)
else:
item.setCheckState(QtCore.Qt.Unchecked)
text = 'Group {}'.format(item.data(34))
new = self.indexFromItem(self.findItems(text, QtCore.Qt.MatchExactly)[0]).row() # find the row of the headeritem
self.moveItem(self.currentRow(), new) # moving back to group
def moveItem(self, old, new): # from row(old) to row(new)
ni = self.takeItem(old)
self.insertItem(new,ni)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
my_list = MyList()
my_list.show()
sys.exit(app.exec_())
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