How to get top 5 in mdx - sql

How to get top 5 from mdx query?
I have a this query:
WITH SET [Geography].[City] AS
TopCount(
[Geography].[City]
,5
,[Measures].[Reseller Freight Cost]
)
SELECT
NON EMPTY {
CROSSJOIN(
{
[Date].[Calendar]
},
{
[Product].[Category]
}
),
CROSSJOIN(
{
[Date].[Calendar].children
},
{
[Product].[Category]
}
)
} DIMENSION PROPERTIES children_cardinality, parent_unique_name ON COLUMNS,
NON EMPTY {
[Geography].[City],
[Geography].[City].children
} DIMENSION PROPERTIES children_cardinality, parent_unique_name ON ROWS
FROM [Adventure Works]
WHERE (
[Measures].[Reseller Freight Cost]
)
But is not working.
Now i have error
error: {"faultstring":"Query (1, 21) Parser: The syntax for '.' is incorrect.","faultcode":"XMLAnalysisError.0xc10e0002"}
The best if I not must change a code after SELECT word .

When you create a set just declare it with no associated hierarchy so this is wrong [Geography].[City].
Also there is a much more readable syntax for cross-join - just use an asterisk *
Try this:
WITH SET [CitySet] AS
TopCount(
[Geography].[City]
,5
,[Measures].[Reseller Freight Cost]
)
SELECT
NON EMPTY {
[Date].[Calendar] * [Product].[Category]
,[Date].[Calendar].children * [Product].[Category]
} DIMENSION PROPERTIES children_cardinality, parent_unique_name ON COLUMNS,
NON EMPTY {
[CitySet], //<<changed here [Geography].[City],
[Geography].[City].children
}
DIMENSION PROPERTIES children_cardinality, parent_unique_name ON ROWS
FROM [Adventure Works]
WHERE (
[Measures].[Reseller Freight Cost]
)

Related

MDX Combine two queries

I need two join two queries, I tried with members but it didn't work. I am new to MDX, please let me know if there a way of doing this. Error is due to list in the calculated member.
SELECT NON EMPTY { [RR TYPE].[Item].[Item].ALLMEMBERS * [Measures].[Unit Value] } ON COLUMNS,
NON EMPTY { ([FFS].[FFSCD].[FFSCD].ALLMEMBERS) } ON ROWS
FROM [GLCube]
WHERE ( [Location].[Site Name].&[Sandbox]
, [LT DT].[DATE HIERARCHY].[YEAR].&[2010]
,-{ [Ledger].[ID].&[A1],[Ledger].[ID].&[A2] ,[Ledger].[ID].&[A3]} )
SELECT NON EMPTY { [RT TYPE].[Item].[Item].ALLMEMBERS * [Measures].[Price Value] } ON COLUMNS,
NON EMPTY { ([FFS].[FFSCD].[FFSCD].ALLMEMBERS) } ON ROWS
FROM (SELECT [CCTYPE].[Desc].&[FCD] ON COLUMNS
FROM [GLCube])
WHERE ( [Location].[Site Name].&[Sandbox]
, [LT DT].[DATE HIERARCHY].[YEAR].&[2010])
Combined query
WITH MEMBER [Measures].[Unit Value]] AS
(-{[Ledger].[ID].&[A1],[Ledger].[ID].&[A2] ,[Ledger].[ID].&[A3]},[Measures].[Unit Value])
MEMBER [Measures].[Price Value] AS
( [CCTYPE].[Desc].&[FCD],[Measures].[Price Value] )
SELECT NON EMPTY {[Measures].[Unit Value],[Measures].[Price Value]} ON COLUMNS,
NON EMPTY { ([FFS].[FFSCD].[FFSCD].ALLMEMBERS} ON ROWS
FROM [GLCube]
WHERE ( [Location].[Site Name].&[Sandbox]
, [LT DT].[DATE HIERARCHY].[YEAR].&[2010])
Expected Output
WITH
MEMBER [Measures].[Filtered Unit Value] as
Aggregate(
-{ [Ledger].[ID].&[A1],[Ledger].[ID].&[A2] ,[Ledger].[ID].&[A3]},
[Measures].[Unit Value]
)
SELECT NON EMPTY
{
[RR TYPE].[Item].[Item].ALLMEMBERS
* {[RT TYPE].[Item].[All]}
* { [CCTYPE].[Desc].[All]}
* [Measures].[Filtered Unit Value]
}
+
{
{[RR TYPE].[Item].[All]}
* [RT TYPE].[Item].[Item].ALLMEMBERS
* { [CCTYPE].[Desc].&[FCD]}
* [Measures].[Price Value]
}
ON COLUMNS,
NON EMPTY { ([FFS].[FFSCD].[FFSCD].ALLMEMBERS) } ON ROWS
FROM [GLCube]
WHERE ( [Location].[Site Name].&[Sandbox]
, [LT DT].[DATE HIERARCHY].[YEAR].&[2010]
)

Filter MDX, SQL Server and Analysis Services

I have query like this one:
SELECT NON EMPTY {
[DIM CUSTOMER].[Country Region Code].[Country Region Code].ALLMEMBERS
}
ON COLUMNS,
NON EMPTY CROSSJOIN( {
[DIM PRODUCT].[Category Name].CHILDREN
},
{
[DIM PRODUCT].[Sub Category Name].CHILDREN
})
ON ROWS FROM [Foo]
WHERE (
[Measures].[Order Qty]
);
now I would like to add filtering, basically include only categories and subcategories where at least 10 different products were sold.
I end up with sth like below, but it returns nothing...
SELECT NON EMPTY {
[DIM CUSTOMER].[Country Region Code].[Country Region Code].ALLMEMBERS
}
ON COLUMNS,
FILTER (
CROSSJOIN( {
[DIM PRODUCT].[Category Name].CHILDREN
},
{
[DIM PRODUCT].[Sub Category Name].CHILDREN
},
{ [DIM PRODUCT].[Name].CHILDREN
}),
[DIM PRODUCT].[Name].CHILDREN.COUNT > 0
)
ON ROWS FROM [Foo]
WHERE (
[Measures].[Order Qty]
);
If someone is so proficient that it wouldn't take him 40 minutes to spot mistake like me, please help & thanks in advance :)
Instead of .CHILDREN you may need .members
Maybe swap this:
[DIM PRODUCT].[Name].CHILDREN.COUNT > 0
For this:
[DIM PRODUCT].[Name].CURRENTMEMBER.CHILDREN.COUNT > 0

Add a column to the MDX Query with a tuple

I have an MDX Query running on the SSAS cube that returns lots of object codes and the period balances for them. I was able to add multiple period balances by using a crossjoin on the rows, however I would like to add one more row with the period end balance for the last fiscal period, and can't seem to figure out a way to do it.
The initial query is
select
non empty
{
[Object Code].[Object Code Number].[Object Code Number]
*
[Object Code].[Object Code Description].[Object Code Description]
*
[Object Code Pathing 1E 1R].[1E_R1 Value].[1E_R1 Value]
*
[Object Code Pathing 1E 1R].[1E_R2 Value].[1E_R2 Value]
*
[Object Code Pathing 1E 1R].[1E_R3 Value].[1E_R3 Value]
*
[Object Code Pathing 1E 1R].[1E_R4 Value].[1E_R4 Value]
}
on rows,
{
[Measures].[Current Period Balance]
}
*
{
[Date].[Fiscal].[Fiscal Period].&[2016]&[1]:[Date].[Fiscal].[Fiscal Period].&[2016]&[7]
}
on columns
from [Finance]
and when I am trying to add one more column
select
non empty
{
[Object Code].[Object Code Number].[Object Code Number]
*
[Object Code].[Object Code Description].[Object Code Description]
*
[Object Code Pathing 1E 1R].[1E_R1 Value].[1E_R1 Value]
*
[Object Code Pathing 1E 1R].[1E_R2 Value].[1E_R2 Value]
*
[Object Code Pathing 1E 1R].[1E_R3 Value].[1E_R3 Value]
*
[Object Code Pathing 1E 1R].[1E_R4 Value].[1E_R4 Value]
}
on rows,
{
[Measures].[Balance At Period End]
*
[Date].[Fiscal].[Fiscal Period]&[2016]&[7]
},
{
[Measures].[Current Period Balance]
}
*
{
[Date].[Fiscal].[Fiscal Period].&[2016]&[1]:[Date].[Fiscal].[Fiscal Period].&[2016]&[7]
}
on columns
from [Finance]
I get the
Parser: The statement dialect could not be resolved due to ambiguity. error
and if I add it like
{
[Measures].[Current Period Balance],
[Measures].[Balance At Period End]
}
*
{
[Date].[Fiscal].[Fiscal Period].&[2016]&[1]:[Date].[Fiscal].[Fiscal Period].&[2016]&[7]
}
on columns
I get Period end Balances for all periods, and this is not needed in the report, I only need the Balance at Period End for the very last period
Here is your first piece of troublesome code:
crossjoin (
[Measures].[Current Period Balance]
,{
[Date].[Fiscal].[Fiscal Period].&[2016]&[1]
,[Date].[Fiscal].[Fiscal Period].&[2016]&[2]
,[Date].[Fiscal].[Fiscal Period].&[2016]&[3]
}
), //<<1
crossjoin(
[Measures].[Balance At Period End]
,{[Date].[Fiscal].[Fiscal Period].&[2016]&[3]}
) on columns
from [Finance]
At point 1 you have closed the first crossjoin and then put a comma - this is a syntax error.
You could try moving that brace from 1 to the end of the statement:
crossjoin (
[Measures].[Current Period Balance]
,{
[Date].[Fiscal].[Fiscal Period].&[2016]&[1]
,[Date].[Fiscal].[Fiscal Period].&[2016]&[2]
,[Date].[Fiscal].[Fiscal Period].&[2016]&[3]
}
, //<<1
crossjoin(
[Measures].[Balance At Period End]
,{[Date].[Fiscal].[Fiscal Period].&[2016]&[3]}
)
) on columns //<<now closing initial crossjoin here
from [Finance]
Ok I just tested the above via the following and it is not a valid approach:
SELECT
CrossJoin
(
[Measures].[Internet Sales Amount]
,{
[Date].[Calendar].[Date].&[20060628]
,[Date].[Calendar].[Date].&[20060629]
}
,CrossJoin
(
[Measures].[Internet Order Quantity]
,{[Date].[Calendar].[Date].&[20060629]}
)
) ON COLUMNS
,[Product].[Product Categories].[All] ON ROWS
FROM [Adventure Works];
We get the following error:
Query (2, 3) The Measures hierarchy is used more than once in the
Crossjoin function.
You could switch to the following structure, creating a set of tuples. This does run:
SELECT
{
[Measures].[Internet Sales Amount]
*
{
[Date].[Calendar].[Date].&[20060628]
,[Date].[Calendar].[Date].&[20060629]
}
,(
[Measures].[Internet Order Quantity]
,{[Date].[Calendar].[Date].&[20060629]}
)
} ON COLUMNS
,[Product].[Product Categories].[All] ON ROWS
FROM [Adventure Works];
Result:

OrderBy MDX Queries?

This is my MDX Query.
select
{
[Measures].[Quantity],
[Measures].[Net Sales]
}
on columns,
NON EMPTY
{(
[Products].[Item Description].children,
[Calendar].[Date].[Date]
)}
on rows
from
(
select
{
[Calendar].[Date].&[2015-03-23T00:00:00],
[Calendar].[Date].&[2015-03-22T00:00:00],
[Calendar].[Date].&[2015-03-21T00:00:00]
}
ON columns
FROM [SalesReport])
This gives the sales records in quantity and net sales as expected. We need now to sort it to get the item which has the highest quantity in first. In other words, we're looking to apply 'descending' sort here.
I tried all order method which gives me error. Can you please help me to get my wish done?
Have you tried the below ?
select
{
[Measures].[Quantity],
[Measures].[Net Sales]
}
on columns,
NON EMPTY
ORDER((
[Products].[Item Description].children,
[Calendar].[Date].[Date]
), [Measures].[Quantity], DESC)
on rows
from
(
select
{
[Calendar].[Date].&[2015-03-23T00:00:00],
[Calendar].[Date].&[2015-03-22T00:00:00],
[Calendar].[Date].&[2015-03-21T00:00:00]
}
ON columns
FROM [SalesReport])
Second attempt [WORKING SOLUTION]
select
{
[Measures].[Quantity],
[Measures].[Net Sales]
}
on columns,
NON EMPTY
ORDER((
[Products].[Item Description].children
), [Measures].[Quantity], DESC)
*[Calendar].[Date].[Date]
on rows
from
(
select
{
[Calendar].[Date].&[2015-03-23T00:00:00],
[Calendar].[Date].&[2015-03-22T00:00:00],
[Calendar].[Date].&[2015-03-21T00:00:00]
}
ON columns
FROM [SalesReport])
If you wanted to further order the inner group you could do this:
SELECT
{
[Measures].[Quantity],
[Measures].[Net Sales]
}
ON 0,
NON EMPTY
GENERATE(
ORDER(
[Products].[Item Description].children
,[Measures].[Quantity], BDESC)
,
ORDER(
[Products].[Item Description].CURRENTMEMBER //<< possibly [Products].CURRENTMEMBER
*
{[Calendar].[Date].[Date].Members}
,[Measures].[Quantity], BDESC))
ON 1
FROM
(
SELECT
{
[Calendar].[Date].&[2015-03-23T00:00:00],
[Calendar].[Date].&[2015-03-22T00:00:00],
[Calendar].[Date].&[2015-03-21T00:00:00]
}
ON 0
FROM [SalesReport]
);

EXISTING equivalent to HAVING

This works:
WITH
MEMBER [Measures].[CurrentDay] AS
AGGREGATE(
[Date].[Calendar].Currentmember,
[Measures].[Reseller Sales Amount]
)
MEMBER [Measures].[CurrentMonth] AS
AGGREGATE(
[Date].[Calendar].Currentmember.parent,
[Measures].[Reseller Sales Amount]
)
SELECT
NON EMPTY
{ [Measures].[CurrentDay],
[Measures].[CurrentMonth] }
ON COLUMNS,
NON EMPTY
{ [Date].[Calendar].[Date] }
HAVING [Measures].[CurrentDay]<>null //<<<<<<<<<<<<<<having line
ON ROWS
From [Adventure Works]
Returning the following from the version of Adventure Works that I have:
If I comment out the line HAVING [Measures].[CurrentDay]<>null then this happens:
Is there another way of eliminating the rows that are null for CurrentDay without using HAVING ?
I've tried using EXISTING without any success:
WITH
MEMBER [Measures].[CurrentDay] AS
AGGREGATE(
[Date].[Calendar].Currentmember,
[Measures].[Reseller Sales Amount]
)
MEMBER [Measures].[CurrentMonth] AS
AGGREGATE(
[Date].[Calendar].Currentmember.parent,
[Measures].[Reseller Sales Amount]
)
SELECT
NON EMPTY
{ [Measures].[CurrentDay],
[Measures].[CurrentMonth] }
ON COLUMNS,
NON EMPTY
{ EXISTING [Date].[Calendar].[Date] }
ON ROWS
From [Adventure Works]
EDIT
To run nsousa's solution in SSMS I need to nest the IIF like this:
WITH
MEMBER [Measures].[CurrentDay] AS
AGGREGATE(
[Date].[Calendar].Currentmember,
[Measures].[Reseller Sales Amount]
)
MEMBER [Measures].[CurrentMonth] AS
IIF(
ISEMPTY([Measures].[CurrentDay]),
NULL,
AGGREGATE(
[Date].[Calendar].Currentmember.parent,
[Measures].[Reseller Sales Amount]
)
)
SELECT
NON EMPTY
{ [Measures].[CurrentDay],
[Measures].[CurrentMonth] }
ON COLUMNS,
NON EMPTY
{ [Date].[Calendar].[Date] }
ON ROWS
From [Adventure Works]
You can redefine your measure:
WITH
MEMBER [Measure].[Not Null Reseller Sales Amount] AS
IIF( IsEmpty( [Measures].[Reseller Sales Amount] ), 0, [Measures].[Reseller Sales Amount] )
MEMBER [Measures].[CurrentDay] AS
AGGREGATE(
[Date].[Calendar].Currentmember,
[Measures].[Not Null Reseller Sales Amount]
)
MEMBER [Measures].[CurrentMonth] AS
AGGREGATE(
[Date].[Calendar].Currentmember.parent,
[Measures].[Not Null Reseller Sales Amount]
)
SELECT
NON EMPTY
{ [Measures].[CurrentDay],
[Measures].[CurrentMonth] }
ON COLUMNS,
NON EMPTY
{ [Date].[Calendar].[Date] }
ON ROWS
From [Adventure Works]