Unable to get the square brackets for the single individual records while using the json_agg inside the json_compose in Teradata-sql-assistance - teradata

According to the documentation,
if we have the following data in a table named emp_table
empID company empName empAge
1, 'Teradata', 'Cameron', 24
2, 'Teradata', 'Justin', 34
3, 'Apple', 'Someone', 34
and if we use the following query :
SELECT JSON_Compose(T.company, T.employees)
FROM
(
SELECT company, JSON_agg(empID AS id,
empName AS name,
empAge AS age) AS employees
FROM emp_table
GROUP BY company
) AS T;
we are supposed to get :
JSON_Compose
------------
{
"company" : "Teradata",
"employees" : [
{ "id" : 1, "name" : "Cameron", "age" : 24 },
{ "id" : 2, "name" : "Justin", "age" : 34 }
]
}
{
"company" : "Apple",
"employees" : [
{ "id" : 3, "name" : "Someone", "age" : 24 }
]
}
But when I try to follow the same steps, I'm not able to get the square bracket for the individual records.
The result I'm getting is :
JSON_Compose
------------
{
"company" : "Teradata",
"employees" : [
{ "id" : 1, "name" : "Cameron", "age" : 24 },
{ "id" : 2, "name" : "Justin", "age" : 34 }
]
}
{
"company" : "Apple",
"employees" :
{ "id" : 3, "name" : "Someone", "age" : 24 }
}
Is there any way to get square bracket for individual records as well ?

Related

Firebase realtime database multi location update through cloud function transactions not working

I got a firebase realtime db (js sdk) where I count users for each location (groups/location/Items/{locationId}/usercount). I also have at a root level, a list of users where for each user I have a list of locations. Within each of these locations I have a counter that counts the total number of useres for that location (users/{userId}/groups/location/Items/{locationId}/usercount).
a) So whenever a user is added to to a location at groups/location/Items/{locationId}/users, the counter at groups/location/Items/{locationId}/usercount is incremented.
b) At the same time a counter at users/groups/location/Items/{locationId}/usercount is also incremented for each user that has this location in users/{userId}/groups/location/Items/.
Below you'll find the json data structure as well as the cloud function supposed to increment both counters (a and b). Part (a) of the function works perfectly. As for part (b) it only works for the user that was just added at groups/location/Items/{locationId}/users (user yjXidCKJuYZ71TNzYe9ob5Raaub2 as per hereby json) on not for ALL users at users/{userId}.
I need this function to increment all the counters of users within users/{userId} that have the location to which a user was added.
I hope I'm clear enough and would REALLY appreciate some help on this. Maybe I should not be using transactions.
Thanks a lot!
"groups" : {
"location" : {
"Items" : {
"NA" : {
"code" : "NA",
"created" : "2019-04-03--19:10:11",
"id" : "NA",
"label" : "North America",
"levelIndex" : 1,
"levelName" : "Continent",
"type" : "location",
"usercount" : 2,
"users" : {
"Xi6XiXdxDAWqvjmSUeobnhDTe4k2" : {
"created" : "2019-04-03--19:10:11",
"id" : "Xi6XiXdxDAWqvjmSUeobnhDTe4k2",
"ip" : "hidden",
"label" : "Anonymous"
},
"yjXidCKJuYZ71TNzYe9ob5Raaub2" : {
"created" : "2019-04-03--19:10:11",
"id" : "yjXidCKJuYZ71TNzYe9ob5Raaub2",
"ip" : "hidden",
"label" : "Anonymous"
}
}
}
}
}
},
"users" : {
"Xi6XiXdxDAWqvjmSUeobnhDTe4k2" : {
"created" : "2019-04-03--19:10:11",
"groups" : {
"location" : {
"Items" : {
"NA" : {
"code" : "NA",
"created" : "2019-04-03--19:10:11",
"id" : "NA",
"label" : "North America",
"levelIndex" : 1,
"levelName" : "Continent",
"type" : "location",
"usercount" : 1
}
}
}
},
"id" : "Xi6XiXdxDAWqvjmSUeobnhDTe4k2",
"ip" : "hidden",
"label" : "Anonymous"
},
"yjXidCKJuYZ71TNzYe9ob5Raaub2" : {
"created" : "2019-04-03--19:10:11",
"groups" : {
"location" : {
"Items" : {
"NA" : {
"code" : "NA",
"created" : "2019-04-03--19:10:11",
"id" : "NA",
"label" : "North America",
"levelIndex" : 1,
"levelName" : "Continent",
"type" : "location",
"usercount" : 2
}
}
}
},
"id" : "yjXidCKJuYZ71TNzYe9ob5Raaub2",
"ip" : "hidden",
"label" : "Anonymous"
}
}
}
exports.onUserAddToLocation = functions.database
.ref('/groups/location/Items/{itemId}/users/{userId}')
.onCreate((snapshot, context) => {
const itemId = context.params.itemId
const userId = context.params.userId
const groupCounterRef = admin.database().ref('/groups/location/Items/' + itemId + '/usercount');
return groupCounterRef.transaction(usercount => {
return (usercount || 0) + 1
}).then(result => {
const count = result.snapshot.val();
const userGroupsCounterRef = admin.database().ref('/users/' + userId + '/groups/location/Items/' + itemId + '/usercount');
userGroupsCounterRef.transaction(usercount => {
return count
})
})
})
This does the trick :
.ref('/groups/location/Items/{itemId}/users/{userId}')
.onCreate((snapshot, context) => {
const itemId = context.params.itemId
const groupCounterRef = admin.database().ref('/groups/location/Items/' + itemId + '/usercount');
return groupCounterRef.transaction(usercount => {
return (usercount || 0) + 1
}).then(result => {
const count = result.snapshot.val();
const usersRef = admin.database().ref("users").orderByKey();
usersRef.once("value")
.then(function(snapshot) {
snapshot.forEach(function(childSnapshot){
const userId = childSnapshot.key
const userLocationCountersRef = admin.database().ref('users/' + userId + '/groups/location/Items/' + itemId)
const hasThisGroup = childSnapshot.hasChild('/groups/location/Items/' + itemId)
if(hasThisGroup){
userLocationCountersRef.update({usercount:count})
}
})
})
})
})
This increments a counter for each user added to a location. After transaction success it copies that counter value to each user that has this location.

Druid query does not return case Insensitive results for count aggregation

Ideally, I need to get the count of how many times "London" is used in the city name. But the query returns different values for "london" and "London" and "LoNdOn" and so on.
I have tried using Case Insensitive as an option, but it doesn't give me the required result.
Here's my query,
{
"queryType": "topN",
"dataSource": "wikiticker",
"dimension":"cityName",
"granularity": "ALL",
"metric": "count",
"threshold": 10,
"filter":
{
"type": "search",
"dimension": "cityName",
"query": {
"type": "insensitive_contains",
"value": "london",
}
},
"aggregations": [
{
"type": "longSum",
"name": "count",
"fieldName": "count"
}
],
"intervals": ["2014-10-01T00:00:00.000Z/2016-10-07T00:00:00.000Z"]
}
And here's my result :
[ {
"timestamp" : "2015-09-12T00:46:58.771Z",
"result" : [ {
"count" : 21,
"cityName" : "London"
},
{
"count" : 10,
"cityName" : "New London"
},
{
"count" : 3,
"cityName" : "london"
},
{
"count" : 1,
"cityName" : "LoNdon"
},
{
"count" : 1,
"cityName" : "LondOn"
} ]
} ]
I should get something like:
[ {
"timestamp" : "2015-09-12T00:46:58.771Z",
"result" : [ {
"count" : 26,
"cityName" : "London"
},
{
"count" : 10,
"cityName" : "New London"
} ]
} ]
Use the filtered aggregator:
A filtered aggregator wraps any given aggregator, but only aggregates the values for which the given dimension filter matches.
{
"type" : "filtered",
"filter" : {
"type" : "search",
"dimension" : cityName,
"query": {
"type":"contains",
"value":"london"
}
},
"aggregator" : {
"type": "count",
"name": "Total Count of the Name London"
}
}
References
Druid Documentation: Filtered Aggregator

Mongolite and aggregation with $lookup on ObjectId vs character

Working with mongolite v0.9.1 (R) and MongoDB v3.4, I'd like to join two collections, the first one, the parent containing an ObjectId, the second one, the children containing the string value of the parents' ObjectId.
This is the basic syntax :
conParent$aggregate('[
{ "$lookup":
{ "from":"Children",
"localField": "_id",
"foreignField": "parent_id",
"as": "children"
}
}
]')
$lookup seems to take only field name, I've tried this, producing syntaxic errors :
.../...
"foreignField": "{'$oid':'parent_id'}"
.../...
So is there a way to deal with that ?
In the other way, I tried to save the parent's ObjectId in the children in ObjectId format with no luck (I still get a string in MongoDB) :
result <- transform(
computeFunction,
parent_id = sprintf('{"$oid":"%s"}',parent$"_id"))
resultCon <- conout$insert(as.data.frame(result))
Is it possible to store an Id as ObjectId in mongolite?
Note : I'm doing bulk inserts so I can't deal with JSON string manipulations.
Any idea ?
Edit:
Here is an example of the collections i am using :
The Parent collection :
{
"_id" : ObjectId("586f7e8b837abeabb778d2fd"),
"name" : "Root1",
"date" : "2017-01-01",
"value" : 1.0,
"value1" : 10.0,
"value2" : 100.0
},
{
"_id" : ObjectId("586f7ea4837abeabb778d30a"),
"name" : "Root1",
"date" : "2017-01-02",
"value" : 2.0,
"value1" : 20.0,
"value2" : 200.0
}
The Children collection :
{
"_id" : ObjectId("586f7edf837abeabb778d319"),
"name" : "Item1",
"value" : 1.1,
"date" : "2017-01-01",
"parent_id" : "586f7e8b837abeabb778d2fd"
}
{
"_id" : ObjectId("586f7efa837abeabb778d324"),
"name" : "Item2",
"value1" : 11.111111111,
"value2" : 12.222222222,
"date" : "2017-01-01",
"parent_id" : "586f7e8b837abeabb778d2fd"
}
{
"_id" : ObjectId("586f7f15837abeabb778d328"),
"name" : "Item1",
"value" : 2.2,
"date" : "2017-01-02",
"parent_id" : "586f7ea4837abeabb778d30a"
}
{
"_id" : ObjectId("586f7f2b837abeabb778d32e"),
"name" : "Item2",
"value1" : 21.111111111,
"value2" : 22.222222222,
"date" : "2017-01-02",
"parent_id" : "586f7ea4837abeabb778d30a"
}
Could you try :
"foreignField": "_id"
Starting from mongo's website example :
library(mongolite)
library(jsonlite)
a = '[{ "_id" : 1, "item" : 1, "price" : 12, "quantity" : 2 },
{ "_id" : 2, "item" : 2, "price" : 20, "quantity" : 1 },
{ "_id" : 3 }]'
b= '[{ "_id" : 1, "sku" : "abc", "description": "product 1", "instock" : 120 },
{ "_id" : 2, "sku" : "def", "description": "product 2", "instock" : 80 },
{ "_id" : 3, "sku" : "ijk", "description": "product 3", "instock" : 60 },
{ "_id" : 4, "sku" : "jkl", "description": "product 4", "instock" : 70 },
{ "_id" : 5, "sku": null, "description": "Incomplete" },
{ "_id" : 6 }]'
mongo_orders <- mongo(db = "mydb", collection = "orders")
mongo_orders$insert(fromJSON(a))
mongo_inventory <- mongo(db = "mydb", collection = "inventory")
mongo_inventory$insert(fromJSON(b))
df <- mongo_orders$aggregate('[
{
"$lookup":
{
"from": "inventory",
"localField": "item",
"foreignField": "_id",
"as": "inventory_docs"
}
}
]')
str(df)
It works as well when both are set to _id
"localField": "_id",
"foreignField": "_id",
Well I must say that's not possible at all !
Mongilite retrieve _id as character and do not contain any ObjectId implementation.
So...

Firebase Database Groupby Using

For Example: My firebase database has to following datas.
datas=[{ "category" : "Content Server", "hits" : 1, "bytes" : 17308 },
{ "category" : "Content Server", "hits" : 1, "bytes" : 47412 },
{ "category" : "Search Engines", "hits" : 1, "bytes" : 7601 },
{ "category" : "Content Server", "hits" : 1, "bytes" : 24210 },
{ "category" : "Internet Services", "hits" : 1, "bytes" : 3690 },
{ "category" : "Search Engines", "hits" : 6, "bytes" : 613036 },
{ "category" : "Search Engines", "hits" : 1, "bytes" : 2858 } ];
How to make the Firebase Database equivalent of this SQL query:
SELECT category, sum(hits), sum(bytes)
FROM datas
GROUP BY category
ORDER BY sum(bytes) DESC

Realm. Get subset of inner element based on outer one

There is a structure:
{ "groups": [
{ "gid" : 1,
"elements" : [
{ "eid" : 1 },
{ "eid" : 2 }
]
},
{ "gid" : 2,
"elements" : [
{ "eid" : 11 },
{ "eid" : 22 }
]
}
{ "gid" : 3,
"elements" : [
{ "eid" : 21 },
{ "eid" : 32 }
]
}
]
}
I understand how to get all groups:
RealmResults<Group> all = realm.where(Group.class).findAll();
Also I could get all elements or all elements in a group.
But how could I query all element from groups that have id > 1?
RealmResults<Group> allFilteredGroups = realm.where(Group.class).greaterThan("gid", 1).findAll();
Is it possible to retrive all elements from all allFilteredGroups by one query, smth like
realm.where(Element.class).equalsTo(???, allFilteredGroups).findall() ?
I'm not quite sure what you mean by "to retrieve all elements". allFilteredGroups has all the Group objects. As they are linked to the Elements objects, you can easily iterate through them:
for(Group group : allFilteredGroups) {
for(Element element : group.getElement()) {
Log.d("TEST", "eid = " + element.eid);
}
}
There is currently no easy way to flatten the last and have all the Element objects in a single RealmResults.

Resources