MDX Measure greater than 1000 - mdx

Morning,
I have the following MDX and would like to place a filter on the measure 'Estimated Unbilled Outstanding Subtotal £'. I have searched through the forums and understand that I should be able to use FILTER or WHERE, but I cannot adapt the examples to work for me. Would really appreciate any assistance.
SELECT
{ [Measures].[Estimated Unbilled Outstanding Subtotal £] } ON COLUMNS
,{ NONEMPTY(
{ [Customer].[Billing Team].[All].CHILDREN }
* { [Customer].[Customer Name].[All].CHILDREN }
* { [Account].[Account ID].[All].CHILDREN }
* { [Account].[Account Type].&[Import]
, [Account].[Account Type].&[Other Non Billable] }
, { [Measures].[Estimated Unbilled Outstanding Subtotal £] }
) } ON ROWS
FROM [BDW];
Many thanks
Adrian

You need to put a filter around the ROWS clause:
SELECT
{ [Measures].[Estimated Unbilled Outstanding Subtotal £] } ON COLUMNS
,
FILTER(
NONEMPTY(
{ [Customer].[Billing Team].[All].CHILDREN }
* { [Customer].[Customer Name].[All].CHILDREN }
* { [Account].[Account ID].[All].CHILDREN }
* { [Account].[Account Type].&[Import]
, [Account].[Account Type].&[Other Non Billable] }
, { [Measures].[Estimated Unbilled Outstanding Subtotal £] }
)
,[Measures].[Estimated Unbilled Outstanding Subtotal £] >1000
)
ON ROWS
FROM [BDW];

Related

Sum of column values ​inside Json array with group by

I have next Django model.
class StocksHistory(models.Model):
wh_data = models.JsonField()
created_at = models.DateTimeField()
I store JSON data in wh_data.
[
{
"id":4124124,
"stocks":[
{
"wh":507,
"qty":2
},
{
"wh":2737,
"qty":1
}
],
},
{
"id":746457457,
"stocks":[
{
"wh":507,
"qty":3
}
]
}
]
Note: it's data for one row - 2022-06-06.
I need to calculate the sum inside stocks by grouping them by wh and by created_at so that the output is something like this
[
{
"wh":507,
"qty":5,
"created_at":"2022-06-06"
},
{
"wh":2737,
"qty":1,
"created_at":"2022-06-06"
},
{
"wh":507,
"qty":0,
"created_at":"2022-06-07"
},
{
"wh":2737,
"qty":2,
"created_at":"2022-06-07"
}
]
I know how to group by date, but I don't understand how to proceed with aggregations inside JsonField.
StocksHistory.objects.extra(select={'day': 'date( created_at )'})
.values('day')
.annotate(
???
)
A solution is suitable, both through Django ORM and through RAW SQL.
demo
WITH cte AS (
SELECT
jsonb_path_query(js, '$[*].stocks.wh')::numeric AS wh,
jsonb_path_query(js, '$[*].stocks.qty')::numeric AS b,
_date
FROM (
VALUES ('[
{
"id":4124124,
"stocks":[
{
"wh":507,
"qty":2
},
{
"wh":2737,
"qty":1
}
]
},
{
"id":746457457,
"stocks":[
{
"wh":507,
"qty":3
}
]
}
]'::jsonb)) v (js),
(
VALUES ('2022-06-06'), ('2022-06-07')) ss_ (_date)
),
cte2 AS (
SELECT
wh, sum(b) AS qty,
_date
FROM
cte
GROUP BY
1,
3
ORDER BY
1
)
SELECT
array_agg(row_to_json(cte2.*)::jsonb)
FROM
cte2;

How to create a multi-level json output using sql query in SQL Server 2016?

Is that possible create a multi-level json string in SQL Server 2016 ? I have a table (patient_data) like this:
And what i want to do is to create json string output in SQL Server like this:
{
"patient":[
{
"key":"A",
"data":[
{
"name":"Amy Farha",
"colored":"darkred",
"avatar_url":"https://s3.amazonaws.com/uifaces/faces/twitter/ladylexy/128.jpg",
"subtitle":"Vice President",
"patientid":"qweqweqeqeq"
},
{
"name":"Anies",
"colored":"darkblue",
"avatar_url":"https://s3.amazonaws.com/uifaces/faces/twitter/adhamdannaway/128.jpg",
"subtitle":"Vice Chairman",
"patientid":"avasdasdad"
}
]
},
{
"key":"B",
"data":[
{
"name":"Bryan Adams",
"colored":"darkgreen",
"avatar_url":"https://randomuser.me/api/portraits/med/women/91.jpg",
"subtitle":"Reggae Man",
"patientid":"avasdasdad"
}
]
},
{
"key":"D",
"data":[
{
"name":"David dummy",
"colored":"darkgreen",
"avatar_url":"https://randomuser.me/api/portraits/med/women/91.jpg",
"subtitle":"Reggae Man",
"patientid":"avasdasdad"
}
]
},
{
"key":"M",
"data":[
{
"name":"Muhammad Adams",
"colored":"darkgreen",
"avatar_url":"https://randomuser.me/api/portraits/med/women/91.jpg",
"subtitle":"Reggae Man",
"patientid":"avasdasdad"
}
]
},
{
"key":"T",
"data":[
{
"name":"Tere",
"colored":"darkgreen",
"avatar_url":"https://randomuser.me/api/portraits/med/women/91.jpg",
"subtitle":"Reggae Man",
"patientid":"avasdasdad"
},
{
"name":"Tifanny",
"colored":"darkblue",
"avatar_url":"https://s3.amazonaws.com/uifaces/faces/twitter/adhamdannaway/128.jpg",
"subtitle":"Vice Chairman",
"patientid":"avasdasdad"
}
]
},
{
"key":"X",
"data":[
{
"name":"Xavier",
"colored":"darkgreen",
"avatar_url":"https://randomuser.me/api/portraits/med/women/91.jpg",
"subtitle":"Reggae Man",
"patientid":"avasdasdad"
}
]
}
]
}
**NOTE "key" is a grouping by the first letter of patient's name
I try use json path but couldn't figure out in multi level case .I hope someone can help me
Try the following queries with FOR JSON PATH
-- test data
CREATE TABLE Patients(
id int,
name varchar(100)
)
INSERT Patients(id,name)VALUES
(11,'A Patient 11'),(12,'A Patient 12'),
(21,'B Patient 21'),
(31,'C Patient 31'),(32,'C Patient 32')
-- query 1
SELECT
(
SELECT
k.[key],
(SELECT p.id,p.[name] FROM Patients p WHERE LEFT(p.[name],1)=k.[key] FOR JSON PATH) [data]
FROM
(
SELECT DISTINCT LEFT([name],1) [key]
FROM Patients
) k
ORDER BY k.[key]
FOR JSON PATH
) patient
FOR JSON PATH
/*
[
{"patient":[
{"key":"A","data":[{"id":11,"name":"A Patient 11"},{"id":12,"name":"A Patient 12"}]},
{"key":"B","data":[{"id":21,"name":"B Patient 21"}]},
{"key":"C","data":[{"id":31,"name":"C Patient 31"},{"id":32,"name":"C Patient 32"}]}
]
}
]
*/
-- query 2
SELECT
k.[key] [patient.key],
(SELECT p.id,p.[name] FROM Patients p WHERE LEFT(p.[name],1)=k.[key] FOR JSON PATH) [patient.data]
FROM
(
SELECT DISTINCT LEFT([name],1) [key]
FROM Patients
) k
ORDER BY k.[key]
FOR JSON PATH
/*
[
{"patient":{"key":"A","data":[{"id":11,"name":"A Patient 11"},{"id":12,"name":"A Patient 12"}]}},
{"patient":{"key":"B","data":[{"id":21,"name":"B Patient 21"}]}},
{"patient":{"key":"C","data":[{"id":31,"name":"C Patient 31"},{"id":32,"name":"C Patient 32"}]}}
]
*/
You can append WITHOUT_ARRAY_WRAPPER to remove the brackets [ and ]
SELECT
(
SELECT
k.[key],
(SELECT p.id,p.[name] FROM Patients p WHERE LEFT(p.[name],1)=k.[key] FOR JSON PATH) [data]
FROM
(
SELECT DISTINCT LEFT([name],1) [key]
FROM Patients
) k
ORDER BY k.[key]
FOR JSON PATH
) patient
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
/*
{
"patient":[
{"key":"A","data":[{"id":11,"name":"A Patient 11"},{"id":12,"name":"A Patient 12"}]},
{"key":"B","data":[{"id":21,"name":"B Patient 21"}]},
{"key":"C","data":[{"id":31,"name":"C Patient 31"},{"id":32,"name":"C Patient 32"}]}
]
}
*/
I found another more shorter variant. You can use ROOT('patient') option here
SELECT
k.[key] [key],
(SELECT p.id,p.[name] FROM Patients p WHERE LEFT(p.[name],1)=k.[key] FOR JSON PATH) [data]
FROM
(
SELECT DISTINCT LEFT([name],1) [key]
FROM Patients
) k
ORDER BY k.[key]
FOR JSON PATH, ROOT('patient')
/*
{
"patient":[
{"key":"A","data":[{"id":11,"name":"A Patient 11"},{"id":12,"name":"A Patient 12"}]},
{"key":"B","data":[{"id":21,"name":"B Patient 21"}]},
{"key":"C","data":[{"id":31,"name":"C Patient 31"},{"id":32,"name":"C Patient 32"}]}
]
}
*/

MDX: Query Data analysed

In this query i used were clause in that year is 2015 and quarter-[2013]&[Quarter1], how is it possible, and getting result set 10 records. actually result set is not displaying.
WITH MEMBER [Measures].[Test] AS ( [Measures].[ProgramAssessmentPatientCnt] + [Measures].[AssessmentPatientCnt] )
MEMBER [Measures].[Test1] AS ( [Measures].[CCMPatientCnt] + [Measures].[CareteamCnt] + [Measures].[CCMPatientCnt] )
SELECT ( ( { [DimEnrollStatus].[EnrollmentStatus].[EnrollmentStatus] } ),
{ [Measures].[AssessmentPatientCnt], [Measures].[Test], [Measures].[Test1] } ) ON COLUMNS,
Subset (
NonEmpty (
{
( { [DimAssessment].[AssessmentText].[AssessmentText] },
{ [DimAssessment].[QuestionText].[QuestionText] },
{ [DimAssessment].[AnswerText].[AnswerText] } )
},
{ [Measures].[AssessmentPatientCnt], [Measures].[Test], [Measures].[Test1] }
),
0,
10
) ON ROWS
FROM [NavigateCube]
WHERE (
{
( { [DimManagedPopulation].[ManagedPopulationName].&[1044]&[LTC Lincoln Centers] },
{ [DimAnchorDate].[Calender Year].&[2015] },
{ [DimAnchorDate].[Calendar Semester Des].[All] },
{ [DimAnchorDate].[Calendar Quarter Des].&[2013]&[Quarter1] },
{ [DimAnchorDate].[English Month Name Desc].[All] } )
} )
Does this return any rows?
WHERE
(
[DimManagedPopulation].[ManagedPopulationName].&[1044]&[LTC Lincoln Centers],
[DimAnchorDate].[Calender Year].&[2015],
//[DimAnchorDate].[Calendar Semester Des].[All],
[DimAnchorDate].[Calendar Quarter Des].&[2013]&[Quarter1],
[DimAnchorDate].[English Month Name Desc].[All]
);
Maybe the following:
WHERE
(
[DimManagedPopulation].[ManagedPopulationName].&[1044]&[LTC Lincoln Centers],
{
[DimAnchorDate].[Calender Year].&[2015],
[DimAnchorDate].[Calendar Semester Des].[All],
[DimAnchorDate].[Calendar Quarter Des].&[2013]&[Quarter1],
[DimAnchorDate].[English Month Name Desc].[All]
}
);

MDX: Query Optimization

For displaying the result set, it takes 2 minutes. Is there any way to optimise the query?
WITH MEMBER [Measures].[TestMeasure] AS
(
[Measures].[EnrollPatientCnt]
+ [Measures].[AssessmentPatientCnt]
+ [Measures].[ProgramAssessmentPatientCnt]
)
MEMBER [Measures].[TotalCount] AS
Count(
NonEmpty(
(
{ [DimAssessment].[AssessmentText].[AssessmentText] },
{ [DimAssessment].[QuestionText].[QuestionText] },
{ [DimAssessment].[AnswerText].[AnswerText] }
)
,{
[Measures].[AssessmentPatientCnt]
, [Measures].[TestMeasure]
}
) )
SELECT
NON EMPTY [Measures].[TotalCount] ON COLUMNS
FROM [NavigateCube]
WHERE
(
{
(
{
[DimManagedPopulation].[ManagedPopulationName].&[1034]&[TC Tammy Brown Care Team]
}
)
}
);
Basically the same approach as Greg/Mosha:
WITH
MEMBER [Measures].[TestMeasure] AS
[Measures].[EnrollPatientCnt]
+ [Measures].[AssessmentPatientCnt]
+ [Measures].[ProgramAssessmentPatientCnt]
MEMBER [Measures].[MeasureIsEmpty] AS
IIF(
NOT ISEMPTY([Measures].[TestMeasure])
OR NOT ISEMPTY([Measures].[AssessmentPatientCnt])
,1
,NULL
)
MEMBER [Measures].[TotalCount] AS
SUM(
[DimAssessment].[AssessmentText].[AssessmentText]
*[DimAssessment].[QuestionText].[QuestionText]
*[DimAssessment].[AnswerText].[AnswerText]
,[Measures].[MeasureIsEmpty]
)
SELECT
NON EMPTY [Measures].[TotalCount] ON 0
FROM [NavigateCube]
WHERE [DimManagedPopulation].[ManagedPopulationName].&[1034]&[TC Tammy Brown Care Team];
How does this query perform?
WITH MEMBER [Measures].[TestMeasure] AS
IIf(
Not(IsEmpty([Measures].[EnrollPatientCnt]))
or Not(IsEmpty([Measures].[AssessmentPatientCnt]))
or Not(IsEmpty([Measures].[ProgramAssessmentPatientCnt]))
,1
,Null
)
MEMBER [Measures].[TotalCount] AS
Sum (
( { [DimAssessment].[AssessmentText].[AssessmentText] },
{ [DimAssessment].[QuestionText].[QuestionText] },
{ [DimAssessment].[AnswerText].[AnswerText] } ),
[Measures].[TestMeasure]
)
SELECT NON EMPTY [Measures].[TotalCount] ON COLUMNS
FROM [NavigateCube]
WHERE (
{
( { [DimManagedPopulation].[ManagedPopulationName].&[1034]&[TC Tammy Brown Care Team] } )
} )
It's not identical, but it's a similar concept to what Mosha blogged about here.

How to Multiply the count value with 5

db.collection.aggregate(
[
{ $group: { _id: null, count: { $sum: 1 } } }
]
)
Suppose the output of count is 20. i want to multiply this count value (20) with 5. result should be 100 in another row total : count*5.