Supposedly there's a set of records where each record looks like the following:
{
"total": 300,
"items": [
{
"id": "item1",
"amount": 100
},
{
"id": "item2",
"amount": 200
},
]
}
Is it possible to do a sum of all totals and items with id = "item1"?
So basically, smth like this (i know that query is ultimately incorrect, but just to show the idea):
select sum(total) as total, sum(items(id="item1").amount) as item1Amount
from MyRecord
Got the answer from GitHub's project. Apparently my initial hypothetical query was quite close to what it should be.
Here's the answer:
select sum(total) as total, sum(items[id="item1"].amount) as item1Amount
from MyRecord
Related
I've 200 records in my mongodb I want to perform basic search operation along with auto suggestion functionality. When I type something in search text box then list of records should be displayed as a suggestion in the drop down. If I select the record from the suggestion list then I want to find the page Number of that particular record on which page that record is coming based on that I can fetch all the 50 records of that particular page and I can highlight the selected record in my data panel where I'm showing all the records.
I'm facing issue in writing query for finding page number of the selected record I'm passing the ID of selected record from the UI and based on that ID I want to find the page number of that record.
I've tried using count query but but I didn't find a way to count the number of records till the particular row index in Mongo, so based on that I can calculate the page number.
[
{
"name": "ABC1",
"id": "33d47e5da3a08555dceb17e34"
},
{
"name": "ABC2",
"id": "5f47e5da3a08555dceb17e8f"
},
{
"name": "ABC3",
"id": "5f47e5da3a08555dceb17e99"
},
{
"name": "ABC4",
"id": "5f47e5da3a08555dceb17e77"
},
{
"name": "ABC5",
"id": "5f47e5da3a08555dceb17e66"
},
{
"name": "ABC6",
"id": "5f47e5da3a08555dceb17e55"
},
{
"name": "ABC7",
"id": "5f47e5da3a08555dceb17e78"
},
{
"name": "ABC8",
"id": "5f47e5da3a08555dceb17e68"
},
{
"name": "ABC9",
"id": "5f47e5da3a08555dceb17e55"
}
]
I want to know the below record is available on which pageNumber. For that I need a Total record count till the ABC5 record.
{
"name": "ABC5",
"id": "5f47e5da3a08555dceb17e66"
}
So that I can find the pageNumber like this;
int pageNumber = count/pageLimit;
final int pageLimit = 3;
int pageNumber = count/pageLimit;
Page<T> page = repository.findAll(new PageRequest(pageNumber, pageLimit));
And this should be my final Result:
[
{
"name": "ABC4",
"id": "5f47e5da3a08555dceb17e77"
},
{
"name": "ABC5",
"id": "5f47e5da3a08555dceb17e66"
},
{
"name": "ABC6",
"id": "5f47e5da3a08555dceb17e55"
},
]
Can someone please help on this?
Thanks in advance.
Im attempting to create this query (which works as I hope)
SELECT userAgent, COUNT(*) FROM page_hour GROUP BY userAgent order by 2 desc limit 10
as a json. I've tried this:
{
"queryType": "groupBy",
"dataSource": "page_hour",
"granularity": "hour",
"dimensions": ["userAgent"],
"aggregations": [
{ "type": "count", "name": "total", "fieldName": "userAgent" }
],
"intervals": [ "2020-02-25T00:00:00.000/2020-03-25T00:00:00.000" ],
"limitSpec": { "type": "default", "limit": 50, "columns": ["userAgent"] },
"orderBy": {
"dimension" : "total",
"direction" : "descending"
}
}
but instead of doing the aggregation over the full range it appears to pick an arbitrary time span (EG 2020-03-19T14:00:00Z)
If you want results from the entire interval to be combined in a single result entry per user agent, set granularity to all in the query.
A few notes on Druid queries:
You can generate a native query by entering a SQL statement in the management console and selecting the explain/plan menu option from the three-dot menu by the run button.
It's worth confirming expectations that the count query-time aggregator will return the number of database rows (not the number of ingested events). This could be the reason the resulting number is smaller than anticipated.
A granularity of all will prevent bucketing results by hour.
A fieldName spec within the count aggregator? I don't know what behavior might be defined for this, so I would remove this property. The docs:
see: https://druid.apache.org/docs/latest/querying/aggregations.html#count-aggregator
I have a document that looks like this:
"userName": "sample name",
"values": [
{
"values": [
{
"brand": "SOLIGNUM CLEAR",
"name": "Solignum Colourless AZ",
"price": "569",
"qip": "30.00",
"sku": "1L",
"unit": "Piece"
}
]
},
{
"values": [
{
"brand": "FirePRO",
"name": "FirePRO",
"price": "419.75",
"qip": "30.00",
"sku": "1L",
"unit": "Cartons"
},
{
"brand": "SOLIGNUM AEROSOL",
"name": "Solignum Colourless AZ Aerosol",
"price": "397",
"qip": "30.00",
"sku": "500ML",
"unit": "Piece"
}
]
}
]
My query looks like this:
SELECT orders.unit, orders.sku, orders.name, orders.srp, TONUMBER(orders.price) AS price, orders.qip as quantity
FROM jdi stoCallLog
UNNEST stoCallLog.`values`[0].`values` AS orders
Query result looks like this
I have tried changing the unnest block into this:
UNNEST stoCallLog.`values`[1].`values` AS orders
selects only the 2nd array value
Also like this:
UNNEST stoCallLog.`values`.`values` AS orders
not possible i guess, it returns none
I need a way to select all of the values at once. Is there any way to do it?
Solved by modifying the UNNEST block to:
UNNEST `values` as rawOrders
UNNEST rawOrders.`values` as orders
I have a with a jsonb column called jsonb that contains data in the following format.
{
"stuff": [
{
"name": "foo",
"percent": "90.0000"
},
{
"name": "bar",
"percent": "10.0000"
}
],
"countries": [
{
"name": "USA",
"value": "30"
},
{
"name": "Canada",
"value": "25"
},
{
"name": "Mexico",
"value": "20"
},
{
"name": "Ecuador",
"value": "10"
}
]
}
I am having a lot of trouble working with this data. Specifically what I want to do is find all the different values "name" can have in "stuff" as well as in "countries", kind of like a SELECT distinct.
But my problem is that I can't seem to extract anything useful from this jsonb. My approach so far was to do
SELECT jsonb->>'stuff' FROM table, but this only gave me a column of type text which contained [{"name": "foo","percent": "90.0000"},{"name": "bar","percent": "10.0000"}].
But since this is text I can't really do anything with it. I also tried SELECT jsonb_array_elements_text(jsonb) FROM table but that returned the following Error:
ERROR: cannot extract elements from an object
SQL state: 22023
Any help with working with this format of data is greatly appreciated!
You need to unnest each array separately and then create a union on the result of those two steps:
select c.x ->> 'name'
from the_table
cross join lateral jsonb_array_elements(json_column -> 'countries') as c(x)
union
select s.x ->> 'name'
from the_table
cross join lateral jsonb_array_elements(json_column -> 'stuff') as s(x);
Online example: https://rextester.com/ZEOIXF91294
I am trying to figure out specific mongoDb query, so far unsuccessfully.
Documents in my collections looks someting like this (contain more attributes, which are irrelevant for this query):
[{
"_id": ObjectId("596e01b6f4f7cf137cb3d096"),
"code": "A",
"name": "name1",
"sys": {
"cts": ISODate("2017-07-18T12:40:22.772Z"),
}
},
{
"_id": ObjectId("596e01b6f4f7cf137cb3d097"),
"code": "A",
"name": "name2",
"sys": {
"cts": ISODate("2017-07-19T12:40:22.772Z"),
}
},
{
"_id": ObjectId("596e01b6f4f7cf137cb3d098"),
"code": "B",
"name": "name3",
"sys": {
"cts": ISODate("2017-07-16T12:40:22.772Z"),
}
},
{
"_id": ObjectId("596e01b6f4f7cf137cb3d099"),
"code": "B",
"name": "name3",
"sys": {
"cts": ISODate("2017-07-10T12:40:22.772Z"),
}
}]
What I need is to get current versions of documents, filtered by code or name, or both. Current version means that from two(or more) documents with same code, I want pick the one which has latest sys.cts date value.
So, result of this query executed with filter name="name3" would be the 3rd document from previous list. Result of query without any filter would be 2nd and 3rd document.
I have an idea how to construct this query with changed data model but I was hoping someone could lead me right way without doing so.
Thank you