Kusto Query: Join multiple tables - join

‎
TablesA, TableB, TableC
After joining the tables: TableA, TableB, TableC using Kusto Query how to show the value of column: IsPriLoc in the column: PriLoc and IsSecLoc in SecLoc. Below is the expected result
ExpectedResult

I made a couple of assumptions (regarding data types for example) but this query delivers the required results:
Link to execute on Kusto help cluster
let A = datatable (GUID_PK: string, Name: string, Desc: string) [
"1", "Test1", "Desc1",
"2", "Test2", "Desc2",
"3", "Test3", "Desc3",
"4", "Test4", "Desc4",
"5", "Test5", "Desc5",
"6", "Test6", "Desc6"
];
let B = datatable (GUID_FK: string, PriLoc: string, SecLoc: string) [
"1", "PriLoc1", "SecLoc1",
"3", "PriLoc3", "SecLoc3",
"5", "PriLoc5", "SecLoc5",
];
let C = datatable (GUID_FK: string, IsPriLoc: string, IsSecLoc: string) [
"2", "TRUE", "FALSE",
"4", "FALSE", "TRUE",
"6", "TRUE", "FALSE",
];
let BC = B
| union (
C
| project GUID_FK, PriLoc=IsPriLoc, SecLoc=IsSecLoc);
A
| join BC on $left.GUID_PK == $right.GUID_FK

Related

Array of dictionaries: Add values if find identical in to single dictionary Swift

I have an array of structure like this
[["grocery_section": "other", "partial_quantity": "0", "name": "Ground turmeric", "unit": "teaspoons", "whole_quantity": "1"],
["grocery_section": "other", "partial_quantity": "", "name": "I", "unit": "cups", "whole_quantity": "1"],
["grocery_section": "other", "partial_quantity": "", "name": "I", "unit": "cups", "whole_quantity": "2"]]
Now, I want to find identical entries from ingredients that have the same key-value pair in that section and If found, want to add quantity into one and make one ingredient. You can check above array where the section name is "OTHER" and check ingredients there. I want to merge that 2 identical ingredients into a single one with quantity 1+1 = 2. So, the final result should be like
[["grocery_section": "other", "partial_quantity": "0", "name": "Ground turmeric", "unit": "teaspoons", "whole_quantity": "1"],
["grocery_section": "other", "partial_quantity": "", "name": "I", "unit": "cups", "whole_quantity": "3"]]
What I tried is loop here but I don't think its efficient way. So, Is there any better way to pull this thing off?
My Code
guard let first = ingredients.first else {
return [] // Empty array
}
var uniqueIngredients: [[String:String]] = [first] // Keep first element
for elem in ingredients.dropFirst() {
let equality = ingredients.compactMap { $0["name"] == elem["name"] }.count
if equality > 1 {
// SAME NAME FOR 2 INGREDIENT FOUND
// COMBINE BOTH OBJECT
} else {
// NEW NAME
// ADD NEW OBJECT
}
}
Group the array to a dictionary by name
Create a variable result
Enumerate the dictionary. If there is more than one item in value sum up the quantities and append only one item
The code assumes that the keys name and whole_quantity exist in all records and the value for key whole_quantity can be converted to Int
let array = [["grocery_section": "other", "partial_quantity": "0", "name": "Ground turmeric", "unit": "teaspoons", "whole_quantity": "1"],
["grocery_section": "other", "partial_quantity": "", "name": "I", "unit": "cups", "whole_quantity": "1"],
["grocery_section": "other", "partial_quantity": "", "name": "I", "unit": "cups", "whole_quantity": "2"]]
let groupedDictionary = Dictionary(grouping: array, by: {$0["name"]!})
var result = [[String:String]]()
for (_, value) in groupedDictionary {
if value.isEmpty { continue }
else if value.count == 1 {
result.append(value[0])
} else {
let totalQuantity = value.map{Int($0["whole_quantity"]!)!}.reduce(0, +)
var mutableValue = value[0]
mutableValue["whole_quantity"] = String(totalQuantity)
result.append(mutableValue)
}
}

Objective-C - JSON format for Core Data relationship like in MySQL - Best practices

I have JSON like:
{
cities: {
1: "London",
3: "New York",
9: "Tokio",
10: "Moscow"
},
keywords: {
120: "walk",
121: "run",
122: "eat",
123: "lake"
},
parks: [
{
id: "1",
likes: "5",
parkName: "Park 1 in NEW YORK",
city: "3", // id of the City NEW YORK
keywords: [
"120",
"121"
]
},
{
id: "1",
likes: "678",
parkName: "Park 2 in NEW YORK",
city: "3", // id of the City NEW YORK
keywords: [
"120",
"123"
]
},
{
id: "1",
likes: "416",
parkName: "Park in London",
city: "1", // id of the City London
keywords: [
"123",
"122"
]
},
... so many ...
]
}
}
So, in Xcode, I wanna use like this:
sample Xcode Core Data relationship
I create NSManagedObject Subclass, and I have this methods:
- (void)addParksObject:(PKParks *)value;
- (void)removeParksObject:(PKParks *)value;
- (void)addParks:(NSSet<PKParks *> *)values;
But, I was expecting to connect Parks table to another tables like in MySQL, using id of the Cities and Keywords.
So,
I have a lot of Parks, some Cities and Keywords.
Is this way to storage data correct method?
Should I store IDs of the Cities and Keywords?
Should I connect tables manually, like in MySQL?
Should I change JSON format to no IDs?
For example JSON:
{
parks: [
{
id: "1",
likes: "5",
parkName: "Park 1 in NEW YORK",
city: "NEW YORK",
keywords: [
"walk",
"run"
]
},
{
id: "1",
likes: "678",
parkName: "Park 2 in NEW YORK",
city: "NEW YORK",
keywords: [
"walk",
"lake"
]
},
{
id: "1",
likes: "416",
parkName: "Park in London",
city: "London",
keywords: [
"lake",
"eat"
]
},
... so many ...
]
}
}
But, I was expecting to connect Parks table to another tables like in MySQL, using id of the Cities and Keywords.
This is the classic mistake of thinking that Core Data is like SQL. It uses SQLite, but SQL-style thinking will get you into trouble.
Core Data is intended to save instances of your model objects. Those objects might have properties which are also model objects. That's a Core Data relationship. You don't use foreign keys or object IDs to represent the relationship, you use model object properties. Core Data translates that to/from SQL.
You might want to read a little about how Core Data relationships work.

XCODE Filter Table View Cells with multipe checkboxes

I'm currently developing an app in Xcode. Within this app I want to give the user the possibility to filter the content of Table View Cells (objects) by multiple checkboxes [see picture 1].
Every object thats shown in the Table View has 8 properties which have the value 1, 2 or 3. The checkboxes shown in the filter area are in relationship with these 8 properties.
Like:
Checkbox1 = prop1
Checkbox2 = prop2
Checkbox3 = prop3
etc..
Objects arrays:
var objects:[Object] = [
Object(name: "Lorem Ipsum", prop1: "3", prop2: "1", prop3: "1", prop4: "1", prop5: "1", prop6: "1", prop7: "3", prop8: "2"),
Object(name: "Lorem Ipsum", prop1: "1", prop2: "1", prop3: "1", prop4: "3", prop5: "1", prop6: "1", prop7: "3", prop8: "2"),
Object(name: "Lorem Ipsum", prop1: "1", prop2: "2", prop3: "2", prop4: "2", prop5: "2", prop6: "1", prop7: "3", prop8: "2"),
Object(name: "Lorem Ipsum", prop1: "2", prop2: "3", prop3: "3", prop4: "1", prop5: "3", prop6: "1", prop7: "3", prop8: "2"),
Object(name: "Lorem Ipsum", prop1: "1", prop2: "3", prop3: "1", prop4: "2", prop5: "2", prop6: "3", prop7: "2", prop8: "1"),
Object(name: "Lorem Ipsum", prop1: "2", prop2: "3", prop3: "1", prop4: "1", prop5: "1", prop6: "1", prop7: "3", prop8: "2")
]
When all checkboxes are unchecked all the objects will show the same default color (blue).
But when one of the checkboxes is checked, the filter needs to change the color of the objects that have the related property to the selected checkbox, as value 2 or 3. When the value is 2 = change to green color, and when the value is 3 = change to purple color.
So for instance; if checkbox2 is selected, all objects with prop2 as 2 or 3 should have a different color [see picture 2]
I did research but couldn't find a way to do it and make it work. If somebody could help, that would be amazing!
EDIT-NOTE: If the user selects two checkboxes, for instance 1 and 2, the objects with value 3 on property 1 or 2 needs to have the color purple, even if they have a value 2 on one of these properties. So value 3 'weighs' more than 2
I would suggest maybe using a dictionary for all the checkboxes. Assign each checkbox a unique tag value (0-7).
let filters: [Int: Bool] = [0: false, 1: false ... 7:0]
When one is selected via a fund didSelect(checkbox: UIButton){ self.tableView.reloadData() }, you would update the corresponding filter by setting self.filters[checkbox.tag] = true.
Then your tableView data items should be filtered by iterating through all the filters and checking if they are true/false before assigning a color for the cell using cellForRow(at: IndexPath).

Mongoid, aggregate models by name and number

I have model named Shop which has name, image. There is possibility that there will be multiple shops with the same name.
For example if I have these shops:
[{_id: "1", name: "Shop A", image: "ImageShopA.jpg"},
{_id: "2", name: "Shop A", image: "ImageShopA1.jpg"},
{_id: "3", name: "Shop B", image: "ImageShopB.jpg"},
{_id: "4", name: "Shop B", image: "ImageShopB1.jpg"},
{_id: "5", name: "Shop C", image: "ImageShopC.jpg"}]
I want to get this kind of result:
[{name: "Shop A", ids: ["1", "2"], image: "ImageShopA.jpg"},
{name: "Shop B", ids: ["3", "4"], image: "ImageShopB.jpg"},
{name: "Shop C", ids: ["5"], image: "ImageShopC.jpg"}]
It creates JSON Objects which has ids of grouped models which are grouped by name and get image of first found model.
Is it possible to do that with aggregation?
Yes, its possible. If you run the following aggregation pipeline in mongo shell with the given data sample
db.collection.aggregate([
{
"$sort": {
"_id": 1
}
},
{
"$group": {
"_id": "$name",
"ids": {
"$push": "$_id"
},
"image": {
"$first": "$image"
}
}
},
{
"$project": {
"name": "$_id", "ids": 1, "image": 1
}
}
])
you will get the desired results:
/* 0 */
{
"result" : [
{
"_id" : "Shop C",
"ids" : [
"5"
],
"image" : "ImageShopC.jpg",
"name" : "Shop C"
},
{
"_id" : "Shop B",
"ids" : [
"3",
"4"
],
"image" : "ImageShopB.jpg",
"name" : "Shop B"
},
{
"_id" : "Shop A",
"ids" : [
"1",
"2"
],
"image" : "ImageShopA.jpg",
"name" : "Shop A"
}
],
"ok" : 1
}
You would need to convert this to the appropriate ruby implemenation

How to compare 3 json array in ios?

Here are the 3 JSON arrays from different responses.
I just want to print "Name" in case Book which has "size_id" be 1 then print "Name" and "Discripition"
and "Prize" who has category_id & Size_id match in Objective-C (on iOS).
category
[ {
"Category_Id": "1",
" Name": "Books",
}
{
"Category_Id": "2",
" Name": "Dummies",
}
product
[
{
"Sub_Cat_Id": "3",
"Category_Id": "1",
"Size_id": "3",
"Sub_Name": "java",
"Description": "We are introducing new product.",
},
{
"Sub_Cat_Id": "4",
"Category_Id": "2",
"Size_id": "3",
"Sub_Name": "Android",
"Description": "We are introducing new product.",
},
{
"Sub_Cat_Id": "2",
"Category_Id": "1",
"Size_id": "1",
"Sub_Name": "ios",
"Description": "We are introducing new product.",
},
Prize
[
{
"Size_Id": "1",
"pirze": "600 "
},
{
"Size_Id": "2",
" pirze ": "300 "
},
{
"Size_Id": "2",
" pirze ": "800"
}
I suggest transforming this JSON into Foundation's NSArray via NSJSONSerialization's dataWithJSONObject:.
After that just use NSArray/NSDictionary filtering to retrieve your values
I suggest you to create a class for your objects (category, product and prize), so you can create an objects for your readings and add them to a NSArray. Once you've got the objects in the same array you can apply a predicate to that array, in order to choose only the objects you want:
NSArray *objects = ...; // This is the array with your objects
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(prize.size_id == 1)"];
NSArray *array = [objects filteredArrayUsingPredicate:tiltPredicate]; // This is the array with the objects you want (size_id = 1)

Resources