Filtering by a subtotal of the measure - mdx

I have the following query in MDX:
SELECT {
[Season].[Season].members *
[Measures].[Wins]
} ON COLUMNS, {
Filter([Team].[Name].[Name].members, [Measures].[Wins] > 100)
} ON ROWS
FROM [Nfl2]
Here I am filtering by "All Wins > 100". How would I filter by "Wins in 2012 > 10" ?

Use this code :
SELECT {
[Season].[Season].members *
[Measures].[Wins]
} ON COLUMNS, {
Filter([Team].[Name].[Name].members, ([Season].[Season].&[2012],[Measures].[Wins]) > 100)
} ON ROWS
FROM [Nfl2]

Related

Datatable sum column by row grouping and show sum result at the end of each group

I am using datatable row grouping from https://datatables.net/examples/advanced_init/row_grouping.html and it works fine.
I have following scenario:
Now I want to calculate sum(sub-total) by each grouping abd show result in row at the end of grouping as you see in image.
At the end of listing , want to show a final row of total amount.
How to accomplish that.
Js used code is:
$(function() {
var table = $('#table').DataTable({
"columnDefs": [
{ "visible": false, "targets": 1 }
],
"order": [[ 1, 'asc' ]],
"displayLength": 25,
"drawCallback": function ( settings ) {
var api = this.api();
var rows = api.rows( {page:'current'} ).nodes();
var last=null;
api.column(1, {page:'current'} ).data().each( function ( group, i ) {
if ( last !== group ) {
$(rows).eq( i ).before(
'<tr class="group" style="background-color:#F5F5F5;"><td colspan="3">'+group+'</td></tr>'
);
last = group;
}
} );
}
});
});
For the same exemple here : https://datatables.net/examples/advanced_init/row_grouping.html
I have made some changes inside "drawCallback": function ( settings ) {
Check this fiddle https://jsfiddle.net/bLykqbo6/110/
I hope it helps
"drawCallback": function ( settings ) {
var api = this.api();
var rows = api.rows( {page:'current'} ).nodes();
var last = null;
var sum = 0;
var groupColumn = 1; //index of column which you are going to group by.
var amtColumn = 3; // index of column which you are going to sum.
api.column(groupColumn, {page:'current'} ).data().each( function ( group, i ) {
if ( last !== group ) {
api.rows().data().each( function(item){
if (item[groupColumn] == group){
sum = sum + (+item[amtColumn]);
}
});
$(rows).eq( i ).before(
'<tr class="group"><td colspan="2" style="background-color: #e7e7e7;"><b>'+group+'</b></td><td style="background-color: #e7e7e7;"><b>'+ sum +'</b></td></tr>'
);
last = group;
sum = 0;
}
});
}

Azure Stream Analytics: Get Array Elements by name

I was wondering if it is possible for me to get the elements of the array by the name of property than the position. For example, this is my incoming data:
{
"salesdata": {
"productsbyzone": {
"zones": [{
"eastzone": "shirts, trousers"
},
{
"westzone": "slacks"
},
{
"northzone": "gowns"
},
{
"southzone": "maxis"
}
]
}
}
}
I intend to move this to a SQL database and I have columns within the database for each zone. The problem is that the order of different zones changes within each json. I was successfully using the following query until I realized that the position of the zones changes within each json:
WITH
salesData AS
(
SELECT
(c.salesdata.productsbyzone.zone,0) as eastzone,
(c.salesdata.productsbyzone.zone,1) as westzone,
(c.salesdata.productsbyzone.zone,2) as northzone,
(c.salesdata.productsbyzone.zone,3) as sourthzone,
FROM [sales-data] as c
)
SELECT
eastzone.eastzone as PRODUCTS_EAST,
westzone.westzone as PRODUCTS_WEST,
northzone.northzone as PRODUCTS_NORTH,
southzone.southzone as PRODUCTS_SOUTH
INTO PRODUCTSDATABASE
FROM salesData
Need a way to reference these fields by the name rather than by the position.
I recommend a solution: Use the JavaScript UDF in the azure stream job to complete the columns sort.
Please refer to my sample:
Input data(upset the order):
{
"salesdata": {
"productsbyzone": {
"zones": [{
"westzone": "slacks"
},
{
"eastzone": "shirts, trousers"
},
{
"northzone": "gowns"
},
{
"southzone": "maxis"
}
]
}
}
}
js udf code:
function test(arg) {
var z = arg;
var obj = {
eastzone: "",
westzone: "",
northzone: "",
southzone: ""
}
for(var i=0;i<z.length;i++){
switch(Object.keys(z[i])[0]){
case "eastzone":
obj.eastzone = z[i]["eastzone"];
continue;
case "westzone":
obj.westzone = z[i]["westzone"];
continue;
case "northzone":
obj.northzone = z[i]["northzone"];
continue;
case "southzone":
obj.southzone = z[i]["southzone"];
continue;
}
}
return obj;
}
You can define the order you want in the obj parameter
SQL:
WITH
c AS
(
SELECT
udf.test(jsoninput.salesdata.productsbyzone.zones) as result
from jsoninput
),
b AS
(
SELECT
c.result.eastzone as east,c.result.westzone as west,c.result.northzone as north,c.result.southzone as south
from c
)
SELECT
b.east,b.west,b.north,b.south
INTO
jaycosmos
FROM
b
Output:
Hope it helps you.
You can use GetArrayElement to return array element then access to each property. Please refer the below query
WITH
salesData AS
(
SELECT
GetArrayElement(zones,0) as z
FROM [sales-data] as s
)
SELECT
z.eastzone
z.westzone
z.northzone
z.southzone
FROM PRODUCTSDATABASE
FROM salesData

Add Commas to a Sum LinQ

I have a Group by Sum operation, but i would like to add ',' every 3 digits
right now i get this output:
{ Pdv = REST, Total = $20786 }
But i would like this Output:
{ Pdv = REST, Total = $2,0786 }
This is my Linq Group By:
foreach (var item in Model.Select(x => new //here you count your total
{
Rid = x.Rid,
Total = x.Total
})
.GroupBy(l => l.Rid) //and then grouping
.Select(z => new
{
Turno = z.Key,
Total = "$" + Decimal.Round(z.Sum(l => l.Total), 0)
}))
{
//Loop Code
}
Your "Total" must be String.
String.Format("{#,##0.00}", 1243.50); // Outputs "1,243.50"
String.Format("{0:$#,##0.00;($#,##0.00);Zero}", 1243.50); // Outputs "$1,243.50"
String.Format("{0:$#,##0.00;($#,##0.00);Zero}", -1243.50); // Outputs"($1,243.50)"
String.Format("{0:$#,##0.00;($#,##0.00);Zero}", 0); // Outputs “Zero"

Convert SQL to MongoDB

Convert
DELETE FROM oshibka WHERE UNIX_TIMESTAMP() - UNIX_TIMESTAMP(date) > 900
to
db.oshibka.remove( { ???: { $gt: 900 } } )
db.oshibka.remove( { date : { $lt : <now-900> } } )
You just need to calculate the date that 900 before now and compare that to the field in you documents.

MDX Aggregated Row (display subtotals on the columns)

I have a very simple MDX query.
SELECT
NON EMPTY {[Measures].[ing_pc_hh_presupuestadas], horas,[Measures].[ing_pc_hh_faltantes],[Measures].[ing_pc_faltante] }
ON COLUMNS,
NON EMPTY CROSSJOIN([proyecto].[codigo proyecto].[All].CHILDREN, [proyecto].[descripcion proyecto].[All].CHILDREN, [concepto].[descripcion concepto].[All].CHILDREN)
ON ROWS
FROM
[TACO V1]
WHERE
{([concepto].[id concepto].&[1]) , ([concepto].[id concepto].&[5])}
This is the idea. A project has 2 concepts. So, in this query, I visualize some measures for each project and concept. This is fine. But I need an extra row for each project, with summarized values for each measure.
This image is the actual scenario:
I need to see the second scenario for each project (here is an example for one project)
Try this:
WITH MEMBER [proyecto].[codigo proyecto].[ Subtotal] AS ' SUM( { [proyecto].[codigo proyecto].[All].CHILDREN }) ', SOLVE_ORDER = 1000
MEMBER [proyecto].[descripcion proyecto].[ Subtotal] AS ' SUM( { [proyecto].[descripcion proyecto].[All].CHILDREN }) ', SOLVE_ORDER = 1000
MEMBER [proyecto].[descripcion concepto].[ Subtotal] AS ' SUM( { [proyecto].[descripcion concepto].[All].CHILDREN }) ', SOLVE_ORDER = 1000
SELECT NON EMPTY {[Measures].[ing_pc_hh_presupuestadas], horas,[Measures].[ing_pc_hh_faltantes],[Measures].[ing_pc_faltante] }
ON COLUMNS,
NON EMPTY { {
{ { [proyecto].[codigo proyecto].[ Subtotal] }, { [proyecto].[codigo proyecto].[All].CHILDREN } }
* { { [proyecto].[descripcion proyecto].[ Subtotal] }, { [proyecto].[descripcion proyecto].[All].CHILDREN } }
* { { [proyecto].[descripcion concepto].[ Subtotal] }, { [proyecto].[descripcion concepto].[All].CHILDREN } }
} } ON ROWS
FROM
[TACO V1]
WHERE
{([concepto].[id concepto].&[1]) , ([concepto].[id concepto].&[5])}