I'm trying to convert the original json to the desired results below using SQL in Snowflake. How can I accomplish this?
I've tried parse_json(newFutureAllocations[0]:fundId) but this only brings back the first fundId element.
ORIGINAL
"newFutureAllocations": [
{
"fundId": 1,
"percentAllocation": 2500
},
{
"fundId": 5,
"percentAllocation": 7500
}
]
DESIRED
"newFutureAllocations": {
"1": 2500,
"5": 7500
}
You need to use flatten to turn your array elements in to rows, then use object_agg() to aggregate them back up again, as an object rather than an array. The exact syntax depends on the rest of your query, data, etc, and you haven't provided enough details about that.
The challenge here is to re-construct the object, I used string concatenations:
with data as (
select parse_json(
'[ { "fundId": 1, "percentAllocation": 2500 }
, { "fundId": 5, "percentAllocation": 7500 } ]') j
)
select parse_json('{'||
listagg('"'||x.value:fundId ||'"'||':'|| x.value:percentAllocation, ',')
||'}')
from data, table(flatten(j)) x
group by seq
I'm attempting to update a document using an index in my FaunaDB collection using FQL.
Update(
Match(
Index('users_by_id'),
'user-1'
),
{
data: {
name: 'John'
}
}
)
This query gives me the following error:
Error: [
{
"position": [
"update"
],
"code": "invalid argument",
"description": "Ref expected, Set provided."
}
]
How can I update the document using the index users_by_id?
Match returns a set reference, not a document reference, because there could be zero or more matching documents.
If you are certain that there is a single document that matches, you can use Get. When you call Get with a set reference (instead of a document reference), the first item of the set is retrieved. Since Update requires a document reference, you can then use Select to retrieve the fetched document's reference.
For example:
Update(
Select(
"ref",
Get(Match(Index('users_by_id'), 'user-1'))
),
{
data: {
name: 'John'
}
}
)
If you have more than one match, you should use Paginate to "realize" the set into an array of matching documents, and then Map over the array to perform a bulk update:
Map(
Paginate(
Match(Index('users_by_id'), 'user-1')
),
Lambda(
"ref",
Update(
Var("ref"),
{
data: {
name: "John",
}
}
)
)
)
Note: For this to work, your index has to have an empty values definition, or it must explicitly define the ref field as the one and only value. If your index returns multiple fields, the Lambda function has to be updated to accept the same number of parameters as are defined in your index's values definition.
I need to fetch all documents that matches a list of 'propIds', and would like to sort the results by how many child-properties they have.
My documents are set up like the following, each document may contain zero or more fields properties.
I'd like to have the returned set sorted so the documents with the most fields is first.
{
"propId" : 7,
"fields" : {
"fieldname1" : "fieldvalue1",
...
}
}
I am able to create a find() query like this:
db.getCollection('col').find({"propId" : {"$in" : [7, 8, 9]}})
, but is at a loss of sorting the results.
I have been looking at the aggregate operator, but I'm not currently able to convert my find() logic (picking the documents that matches a list of ids) to an aggregate query that also uses the $sortByCount aggregation.
A pointer to how the aggregator works, and how I can convert the find-logic into an aggregate query, would be appreciated. Or a pointer to how I can sort the find-query by the count of "fields".
Try this one:
let query = { propId: { $in : [7, 8, 9] } };
let result = await Collection.aggregate([
{$match: query},
{$project: { propId: 1, fields: 1, fieldArray: {$objectToArray: '$fields'} } },
{$project: { propId: 1, fields: 1, size: {$size: '$fieldArray'} } },
{$sort: size}
])
I need to filter members from [__Account.Account selection] by some condition regardless if members are empty or not, but Filter() function implicitly excludes empty members. Is this is a bug or a feature? MSDN does not mention such behavior for Filter function.
Any idea how to avoid the issue?
WITH
SET [__Account.Account selection] AS
'{
{
[Account].[Account Number].&[110]
,[Account].[Account Number].&[1130]
,[Account].[Account Number].&[1164]
,[Account].[Account Number].&[1210]
,[Account].[Account Number].&[1300]
,[Account].[Account Number].&[20]
,[Account].[Account Number].&[8500]
,[Account].[Account Number].&[8040]
}
}'
SET [__Account.Account Number_RootMembers_Smart] AS
'{
Filter(
[__Account.Account selection],
1 = 1)}'
SELECT
[__Account.Account Number_RootMembers_Smart] ON ROWS
,{} ON COLUMNS
FROM [Adventure Works]
NOTE: Function Generate() has the same behavior.
NOTE2: By "empty member" I mean member with not value on any measure.
And there are members with measures...
Please try this:
WITH
SET [__Account.Account selection] AS
{
{
[Account].[Account Number].&[110]
,[Account].[Account Number].&[1130]
,[Account].[Account Number].&[1164]
,[Account].[Account Number].&[1210]
,[Account].[Account Number].&[1300]
,[Account].[Account Number].&[20]
,[Account].[Account Number].&[8500]
,[Account].[Account Number].&[8040]
}
* [Account].[Account].[Account].Members
}
SET [__Account.Account Number_RootMembers_Smart] AS
{
Filter(
[__Account.Account selection],
1=1)}
SELECT {} ON COLUMNS,
[__Account.Account Number_RootMembers_Smart] ON ROWS
FROM [Adventure Works]
Notice that I added the [Account].[Account].[Account].Members into the query. Previously since it was not mentioned, the current coordinate was the [All Accounts] member. Since the Account dimension is a parent-child dimension, the All member doesn't exist with 6 of 8 account numbers apparently. Changing the query ensures that the relevant Account and Account Number pair get put together so that all 8 rows exist in the cube space and show up.
hi i am new to mdx,
i want to combine two select statement resultset (side by side like union) using mdx query,please any body help me to solve this query
first query:
SELECT
{ [Last Year] }
ON COLUMNS,
{ {[Location].[Location].&[7], [Location].[Location].&[12], [Location].[Location].&[11],
[Location].[Location].&[19], [Location].[Location].&[17], [Location].[Location].&[16],
[Location].[Location].&[9], [Location].[Location].&[18] },{[Location].[Location].[All]}}
ON ROWS
FROM [Cube1]
WHERE ( [Measures].[Labour %] )
here [Last Year] is a calculated set
[Last Year]=====
{STRTOMEMBER("[Date].[Month].&["+ cstr(year(now())-2) +"-11-01T00:00:00]"):
(STRTOMEMBER("[Date].[Month].&["+ cstr(year(now())-1) +"-10-01T00:00:00]"))}
2nd query:
WITH MEMBER [Measures].[Budget ] AS IIF(avg([Last Year],IIF(([Measures].[Budget]>0),[Measures].[Budget],null)),
avg([Last Year],IIF(([Measures].[Budget]>0),[Measures].[Budget],null)),0.00),
MEMBER [Measures].[YTD] AS IIF(avg([Last Year], IIF(([Measures].[Labour %]>0),[Measures].[Labour %],null))<>null,
avg([Last Year], IIF(([Measures].[Labour %]>0),[Measures].[Labour %],null)),0.00),
FORMAT_STRING = "Standard",
BACK_COLOR = CASE WHEN [YTD] = 0 THEN /*White*/16777215 /*White*/
WHEN [YTD] <= [Measures].[Budget ] THEN 65408
WHEN [YTD]<= [Measures].[Budget ] +5 THEN 65535
WHEN [YTD]> [Measures].[Budget ] +5 THEN 255
END,
VISIBLE = 1
SELECT
{ [Measures].[YTD], [Measures].[Budget ] }
ON COLUMNS,
{ { [Location].[Location].&[7], [Location].[Location].&[12], [Location].[Location].&[11], [Location].[Location].&[19], [Location].[Location].&[17], [Location].[Location].&[16], [Location].[Location].&[9], [Location].[Location].&[18] },{[Location].[Location].[All]} }
ON ROWS
FROM [Cube1]
**here ==> [Measures].[YTD], [Measures].[Budget ] are calculated member
i want result like in
coulmns===> ytd,budget,nov,dec,jan,feb.,,,,,,,,,,october and rows ====> locations and total(average of all locations)
please guide me to get solution like mdx query**
Your concept of "UNION" wont work for 2 main reasons:
1. You can't UNION members from different hierarchies. 2. Query structures are different.
Only way to see them "side by side", would be to open the queries in two different windows.
Instead, if you just wanted to see a combined view of measures and members, use either of the queries below:
//Go for this if you want to see the Locations in output
WITH MEMBER [Measures].[Budget ] AS IIF(avg([Last Year],IIF(([Measures].[Budget]>0),[Measures].[Budget],null)),
avg([Last Year],IIF(([Measures].[Budget]>0),[Measures].[Budget],null)),0.00),
MEMBER [Measures].[YTD] AS IIF(avg([Last Year], IIF(([Measures].[Labour %]>0),[Measures].[Labour %],null))<>null,
avg([Last Year], IIF(([Measures].[Labour %]>0),[Measures].[Labour %],null)),0.00),
FORMAT_STRING = "Standard",
BACK_COLOR = CASE WHEN [YTD] = 0 THEN /*White*/16777215 /*White*/
WHEN [YTD] <= [Measures].[Budget ] THEN 65408
WHEN [YTD]<= [Measures].[Budget ] +5 THEN 65535
WHEN [YTD]> [Measures].[Budget ] +5 THEN 255
END,
VISIBLE = 1
SELECT
{ [Measures].[YTD], [Measures].[Budget ], [Measures].[Labour %] }
ON COLUMNS,
{ [Last Year] }
*
{ { [Location].[Location].&[7],
[Location].[Location].&[12],
[Location].[Location].&[11],
[Location].[Location].&[19],
[Location].[Location].&[17],
[Location].[Location].&[16],
[Location].[Location].&[9],
[Location].[Location].&[18] },
{[Location].[Location].[All]} }
ON ROWS
FROM [Cube1]
OR
//A cleaner one. Go for this if you don't want to see the Locations in output
WITH MEMBER [Measures].[Budget ] AS IIF(avg([Last Year],IIF(([Measures].[Budget]>0),[Measures].[Budget],null)),
avg([Last Year],IIF(([Measures].[Budget]>0),[Measures].[Budget],null)),0.00),
MEMBER [Measures].[YTD] AS IIF(avg([Last Year], IIF(([Measures].[Labour %]>0),[Measures].[Labour %],null))<>null,
avg([Last Year], IIF(([Measures].[Labour %]>0),[Measures].[Labour %],null)),0.00),
FORMAT_STRING = "Standard",
BACK_COLOR = CASE WHEN [YTD] = 0 THEN /*White*/16777215 /*White*/
WHEN [YTD] <= [Measures].[Budget ] THEN 65408
WHEN [YTD]<= [Measures].[Budget ] +5 THEN 65535
WHEN [YTD]> [Measures].[Budget ] +5 THEN 255
END,
VISIBLE = 1
SELECT
{ [Measures].[YTD], [Measures].[Budget ], [Measures].[Labour %] }
ON COLUMNS,
{ [Last Year] } ON ROWS
FROM [Cube1]
WHERE
{ { [Location].[Location].&[7],
[Location].[Location].&[12],
[Location].[Location].&[11],
[Location].[Location].&[19],
[Location].[Location].&[17],
[Location].[Location].&[16],
[Location].[Location].&[9],
[Location].[Location].&[18] },
{[Location].[Location].[All]} }