MDX: How to get dimension count - sql-server-2008

I execute this query and gives me this error:
Infinite Recursion Detected: The loop of dependencies is: Direct Reporting->Direct Reporting.
Can anybody help me?
WITH
MEMBER [Measures].[Direct Reporting] AS COUNT([Employee].[Employees].Members, EXCLUDEEMPTY)
SELECT
{[Measures].[Direct Reporting]} ON COLUMNS,
{[Geography].[Country].AllMembers} ON ROWS
FROM [Adventure Works]

You've picked a special type of hierarchy: a parent/child hierarchy.
Here is an example of a count against the advWks cube:
WITH
MEMBER [Measures].[aCount] AS
Sum
(
(
[Customer].[Customer].[Customer]
,[Customer].[Customer Geography].CurrentMember
)
,IIF
(
[Measures].[Internet Order Quantity] > 50
,1
,0
)
)
SELECT
{
[Measures].[Direct Reporting]
,[Measures].[Internet Order Count]
} ON COLUMNS
,{[Customer].[Customer Geography].[Country].MEMBERS} ON ROWS
FROM [Adventure Works];

Specifying Count with EXCLUDEEMPTY means the engine needs to evaluate the cube's cells referenced by each member of the dimension and the current [Measures]. Not 100% sure about SSAS behavior but it seems the current [Measures] is the calc. measure being defined itself: hence the infinite loop.
To fix it, you can explicitly define the [Measures]:
WITH MEMBER [Measures].[Direct Reporting] AS
COUNT( { [Measures].[use-here-your-measure] } * [Employee].[Employees].Members, EXCLUDEEMPTY)
Or perhaps use the NonEmpty function:
AS Count( NonEmpty( [Employee].[Employees].Members,
[Measures].[use-here-your-measure] ))
Hope that helps.

Related

Cannot use window function 'count' in having statement

I'm new to MYSQL, and I'm trying to validate the number of data which have the same name from 2 column that occurs more than one time, which I already try to use 'having' statement in this case and it throws me an error like this
Error Code: 3593. You cannot use the window function 'count' in this context.'
below I include an image of what I'm trying to do
you can see there a column named "number_of_same_year" represent the "COUNT OVER PARTITION" output, which has numbers that logically could be validated. I only want to show where the numbers are above 1 (which means occur more than one time)
ps: I'm using MySQL in Windows 10
You cannot use having and a window function. You would want to instead do as follows
select * from (
select unit_name
,month(transaction_date)
,year(transaction_date) as year
,budget
,count(*) over(partition by unit_name,year(transaction_date)) as number_of_same_year
from sql_advertising.history_transaction
)x
where x.number_of_same_year >1
order by x.unit_name
SELECT {fieldset}
FROM {tableset}
WHERE {conditions-1}
GROUP BY {expression-1}
HAVING {conditions-2}
AND {expression-2} = COUNT({expression-3}) OVER ({window})
Window function is applied to output dataset, but HAVING alters it. So window function cannot be used in HAVING. The above code is invalid.
You may solve it by:
WITH `cte` AS ( SELECT {fieldset},
{expression-2} = COUNT({expression-3}) OVER ({window}) AS `criteria`
FROM {tableset}
WHERE {conditions-1}
GROUP BY {expression-1}
HAVING {conditions-2} )
SELECT {fieldset}
FROM `cte`
WHERE `criteria`

Where is the grand total column Excel created in my MDX?

I am having a miserable time trying to recreate part of a pivot table in SSRS. You know when you connect to a cube in Excel, then add measures and other data points, how Excel nicely puts things together for you, including giving you grand totals? Well I am trying to recreate this in SSRS in Query Designer and having no luck. This is day three I'm still stuck on this.
What I can create is all of the dataset along with the proper filters. It's those grand total columns that Excel magically created that I can't get. I need those the most for my SSRS report.
Here is the MDX that I currently have. It works great except that in Query Designer in Visual Studio the grand total column for "Total Calls" is not appearing.
Does anyone know what to do or how to get that grand total column?
Please help.
SELECT
NON EMPTY
{
[Measures].[Total Calls], [Measures].[Total Moves]
}
ON COLUMNS,
NON EMPTY
{
([To Person].[Department].[Unit].ALLMEMBERS * [From Person].[Location].[City].ALLMEMBERS * [From Person].[Support].[Person].ALLMEMBERS * [From Person].[ID].[ID].ALLMEMBERS)
}
DIMENSION PROPERTIES MEMBER_CAPTION,
MEMBER_UNIQUE_NAME
ON ROWS
FROM
(
SELECT
(
STRTOSET(#MDepartment, CONSTRAINED)
)
ON COLUMNS
FROM
(
SELECT
(
STRTOSET(#Weeks, CONSTRAINED)
)
ON COLUMNS
FROM [PersonTransf]
)
)
WHERE
(
IIF
(
STRTOSET(#Weeks, CONSTRAINED).Count = 1,
STRTOSET(#Weeks, CONSTRAINED),
[Date].[Sun Weeks].currentmember
),
IIF
(
STRTOSET(#MDepartment, CONSTRAINED).Count = 1,
STRTOSET(#MDepartment, CONSTRAINED),
[From Person].[Department].currentmember
)
)
CELL PROPERTIES VALUE,
BACK_COLOR,
FORE_COLOR,
FORMATTED_VALUE,
FORMAT_STRING,
FONT_NAME,
FONT_SIZE,
FONT_FLAGS
Did you try adding a column outside the group then just put SUM(Fields!YourMDXColumn.Value) on the cell?

MySQL String Position and Substring sort

Example data to sort:
xy3abc
y3bbc
z3bd
Sort order must be abc, bbc, bd regardless of what is before the numeral.
I tried:
SELECT
*,
LEAST(
if (Locate('0',fcccall) >0,Locate('0',fcccall),99),
if (Locate('1',fcccall) >0,Locate('1',fcccall),99),
if (Locate('2',fcccall) >0,Locate('2',fcccall),99),
if (Locate('3',fcccall) >0,Locate('3',fcccall),99),
if (Locate('4',fcccall) >0,Locate('4',fcccall),99),
if (Locate('5',fcccall) >0,Locate('5',fcccall),99),
if (Locate('6',fcccall) >0,Locate('6',fcccall),99),
if (Locate('7',fcccall) >0,Locate('7',fcccall),99),
if (Locate('8',fcccall) >0,Locate('8',fcccall),99),
if (Locate('9',fcccall) >0,Locate('9',fcccall),99)
) as locationPos,
SUBSTRING(fcccall,locationPos,3) as fccsuffix
FROM memberlist
ORDER BY locationPos, fccsuffix
but locationPos gives me an error on the substring function call
It's not possible to reference that expression by its alias locationPos, within another expression in the same SELECT list.
Replicating the entire expression would be the SQL way to do it. (Yes, it is ugly repeating that entire expression.)
Another (less performant) approach is to use your query (minus the fccsuffix expression) as an inline view. The outer query can reference the assigned locationPos alias as a column name.
As a simple example:
SELECT v.locationPos
FROM ( SELECT 'my really big expression' AS locationPos
FROM ...
) v
This approach of using an inline view ("derived table") can have some serious performance implications with large sets.
But for raw performance, repeating the expression is the way to go:
SELECT *
, LEAST(
if (Locate('0',fcccall) >0,Locate('0',fcccall),99),
if (Locate('1',fcccall) >0,Locate('1',fcccall),99),
if (Locate('2',fcccall) >0,Locate('2',fcccall),99),
if (Locate('3',fcccall) >0,Locate('3',fcccall),99),
if (Locate('4',fcccall) >0,Locate('4',fcccall),99),
if (Locate('5',fcccall) >0,Locate('5',fcccall),99),
if (Locate('6',fcccall) >0,Locate('6',fcccall),99),
if (Locate('7',fcccall) >0,Locate('7',fcccall),99),
if (Locate('8',fcccall) >0,Locate('8',fcccall),99),
if (Locate('9',fcccall) >0,Locate('9',fcccall),99)
) AS locationPos
, SUBSTRING(fcccall
, LEAST(
if (Locate('0',fcccall) >0,Locate('0',fcccall),99),
if (Locate('1',fcccall) >0,Locate('1',fcccall),99),
if (Locate('2',fcccall) >0,Locate('2',fcccall),99),
if (Locate('3',fcccall) >0,Locate('3',fcccall),99),
if (Locate('4',fcccall) >0,Locate('4',fcccall),99),
if (Locate('5',fcccall) >0,Locate('5',fcccall),99),
if (Locate('6',fcccall) >0,Locate('6',fcccall),99),
if (Locate('7',fcccall) >0,Locate('7',fcccall),99),
if (Locate('8',fcccall) >0,Locate('8',fcccall),99),
if (Locate('9',fcccall) >0,Locate('9',fcccall),99)
),3
) AS fccsuffix
FROM memberlist
ORDER BY locationPos, fccsuffix
Unfortunately, with MySQL, it's not possible to reference the result of the locationPos column within an expression in the same SELECT list.
For only one numeral I like:
SELECT *
FROM memberlist
ORDER BY SUBSTRING(fcccall,
LOCATE('0',fcccall)+
LOCATE('1',fcccall)+
LOCATE('2',fcccall)+
LOCATE('3',fcccall)+
LOCATE('4',fcccall)+
LOCATE('5',fcccall)+
LOCATE('6',fcccall)+
LOCATE('7',fcccall)+
LOCATE('8',fcccall)+
LOCATE('9',fcccall),3)
But the sensible approach is not to store two separate bits of information in one field.

MDX - Always return at least one row even if no data is available

I am using the following MDX to pull a parameter:
WITH
MEMBER [Measures].[Label] AS [Dim].[Hier].CURRENTMEMBER.NAME
MEMBER [Measures].[Value] AS [Dim].[Hier].CURRENTMEMBER.UNIQUENAME
SELECT
{
[Measures].[Label]
, [Measures].[Value]
}
ON 0,
NONEMPTY([Dim].[Hier].children, [Measures].[Measure])
ON 1
FROM [Cube]
WHERE
(
-- Criteria
)
Sometimes, when selecting certain filtering criteria the query results in empty set. Instead, I would like it to display "N/A" in both value and label. Is it doable in MDX or should I use SSRS Calculated member to count rows in resulting dataset and substitute?
Something like :
WITH
SET [MySet] AS NONEMPTY([Dim].[Hier].children, [Measures].[Measure])
MEMBER [Measures].[Label] AS [Dim].[Hier].CURRENTMEMBER.NAME
MEMBER [Measures].[Value] AS [Dim].[Hier].CURRENTMEMBER.UNIQUENAME
MEMBER [Dim].[Hier].[All(likely)].[N/A] AS 'N/A'
SELECT
{[Measures].[Label] , [Measures].[Value] }
ON 0,
IIF( count( [MySet] ) = 0, {[Dim].[Hier].[All(likely)].[N/A]}, [MySet] )
ON 1
FROM
[Cube]
WHERE
(-- Criteria)

How To Filter a list in String with MDX

Goal:
Filter the value in the table column based on string "Canada" as a criteria.
Problem:
I cannot get the MDX syntax code in where state to be correct in order to filter the data based on Canada.
Information:
This situation is a simplified sample and my request is to add filter criteria in the where state.
Data source is SSAS:s AdventureWorksDW2012
Code:
SELECT
{
[Measures].[Reseller Order Count],
[Measures].[Discount Amount]
} ON COLUMNS,
{
([Reseller].[Reseller Type].[Business Type].ALLMEMBERS )
} ON ROWS
FROM [Adventure Works]
WHERE
(
FILTER
(
[Geography].[Country].ALLMEMBERS, [Geography].[Country].NAME ='Canada'
)
)
There are two issues:
In Adventure Works, the Geography dimension is not linked to the Internet Sales measure group. Have a look at the cube definition, tab "Dimension Usage". You should use the hierarchies from the Customer dimension found in the Location folder when using measures from the Internet Sales measure group. I use [Customer].[Country] below.
In a filter, you should use Current and a set alias to refer to the current element during the iteration of the set.
The following shows what you want, I just changed the Filter:
SELECT
{
[Measures].[Reseller Order Count],
[Measures].[Discount Amount]
} ON COLUMNS,
{
([Reseller].[Reseller Type].[Business Type].ALLMEMBERS )
} ON ROWS
FROM [Adventure Works]
WHERE
(
FILTER
(
[Customer].[Country].ALLMEMBERS as c, c.current.NAME ='Canada'
)
)