Hi,
I am trying to work out on creating a n1ql query which has to fetch the latest document in the list when compared to a given date ordered by desc and group by two fields.
Ex: Documents will be in below fashion
{
_Id = 1
"Key1 " : “7777”,
“Key2” : “abcdef”
“DateTime”: “2015-03-27T00:00:00Z”
“Date 2”: “2020-03-27T00:00:00Z”
“Other Field 1”: “Other Field 11”
“Other Field 2”: “Other Field 21”
…
}
{
_Id = 2
"Key1 " : “7777”,
“Key2” : “abcdef”
“DateTime”: “2016-03-27T00:00:00Z”
“Date 2”: “2020-03-28T00:00:00Z”
“Other Field 1”: “Other Field 12”
“Other Field 2”: “Other Field 22”
…
}
{
_Id = 3
"Key1 " : “7777”,
“Key2” : “abcdef”
“DateTime”: “2017-03-27T00:00:00Z”
“Date 2”: “2020-03-29T00:00:00Z”
“Other Field 1”: “Other Field 13”
“Other Field 2”: “Other Field 23”
…
}
{
_Id = 4
"Key1 " : “7777”,
“Key2” : “ghijkl”
“DateTime”: “2015-03-27T00:00:00Z”
“Date 2”: “2020-03-27T00:00:00Z”
“Other Field 1”: “Other Field 14”
“Other Field 2”: “Other Field 24”
…
}
{
_Id = 5
"Key1 " : “7777”,
“Key2” : “ghijkl”
“DateTime”: “2016-03-27T00:00:00Z”
“Date 2”: “2020-03-27T00:00:00Z”
“Other Field 1”: “Other Field 15”
“Other Field 2”: “Other Field 25”
…
}
Similarly documents with Key1 as “8888” as above.
{
_Id = 6
"Key1 " : “8888”,
“Key2” : “abcdef”
“DateTime”: “2015-03-27T00:00:00Z”
“Date 2”: “2020-03-27T00:00:00Z”
“Other Field 1”: “Other Field 11”
“Other Field 2”: “Other Field 21”
…
}
{
_Id = 7
"Key1 " : “8888”,
“Key2” : “abcdef”
“DateTime”: “2016-03-27T00:00:00Z”
“Date 2”: “2020-03-28T00:00:00Z”
“Other Field 1”: “Other Field 12”
“Other Field 2”: “Other Field 22”
…
}
{
_Id = 8
"Key1 " : “8888”,
“Key2” : “abcdef”
“DateTime”: “2017-03-27T00:00:00Z”
“Date 2”: “2020-03-29T00:00:00Z”
“Other Field 1”: “Other Field 13”
“Other Field 2”: “Other Field 23”
…
}
{
_Id = 9
"Key1 " : “8888”,
“Key2” : “ghijkl”
“DateTime”: “2015-03-27T00:00:00Z”
“Date 2”: “2020-03-27T00:00:00Z”
“Other Field 1”: “Other Field 14”
“Other Field 2”: “Other Field 24”
…
}
{
_Id = 10
"Key1 " : “8888”,
“Key2” : “ghijkl”
“DateTime”: “2016-03-27T00:00:00Z”
“Date 2”: “2020-03-27T00:00:00Z”
“Other Field 1”: “Other Field 15”
“Other Field 2”: “Other Field 25”
…
}
When queried with list of Key1 , list of Key2, and on particular date, it should fetch the top one from the list when ordered by desc for each Key1 and Key2 combination. The resultant should just provide me documents with _id just 2 and 7.
Here i need to select just Key1, Key2, Date2, OtherField1 and not every thing else from the documents. I have come up something like this,
select p.jp.Key1, p.jp.Key2, p.jp.Date2, p.jp.OtherField1 from (
SELECT max ([a.DateTime, a]) [1] as jp
from bucket as a
join bucket as b on KEYS META(a).id
WHERE a.Key1 in [“7777”,“8888”]
AND a.Key2 in [“abcdef”,“ghijkl”]
and a.DateTime <= 2016-03-28
group by a.Key1,a.Key2) p;
Is there any other best way to do achieve this? Is the query correct in the first place?
Tried something like below, but it doesn’t seem to work.
SELECT a.key1, a.key2, cmbyid[0].Date2 as Date2, cmbyid[0].OtherField1 as OtherField1,
from bucket as a
join bucket as b on KEYS META(a).id
WHERE a.Key1 in [“7777”,“8888”]
AND a.Key2 in [“abcdef”,“ghijkl”]
and a.DateTime <= 2016-03-28
group by a.key1, a.key2
LETTING cmbyid = ARRAY_AGG(a);
Thanks.
Please let me know if this can be achieved using spring JPA ?