I upgraded to SQL server 2008 R2 from 2005, and now this query is no longer working(although I don't rule out something I did being the cause). I have simplified the names/query to demonstrate the issue:
SELECT
NON EMPTY
{
[BizDim].[County].[County]
* [BizDim].[name].[name]
}
ON COLUMNS,
{
[Biz Line Type Dimension].[Line Number].[Line Number]
* [Biz Line Type Dimension].[Display Name].[Display Name]
}
ON ROWS
FROM [TPS Data View]
Where (
STRTOSET("{[BizDim].[County ID].&[16]}", CONSTRAINED)
,STRTOSET("{([BizDim].[Corp].[Corp].ALLMEMBERS,[BizDim].[Local].[Local].ALLMEMBERS,[BizDim].[HQ].[HQ].&[x]),([BizDim].[Corp].[Corp].&[x],[BizDim].[Local].[Local].ALLMEMBERS,[BizDim].[HQ].[HQ].ALLMEMBERS)}")
)
Essentially this is a logical OR saying if column Corp == 'x' OR HQ == 'x' then include it in the result. This is known as an assymmetric(sic) set.
The above gives the error:
The Tuple function expects a tuple expression for the 3 argument. A tuple set expression was used.
I can remove the STRTOSET function and it works perfectly:
Where (
STRTOSET("{[BizDim].[County ID].&[16]}", CONSTRAINED)
,{([BizDim].[Corp].[Corp].ALLMEMBERS,[BizDim].[Local].[Local].ALLMEMBERS,[BizDim].[HQ].[HQ].&[x]),([BizDim].[Corp].[Corp].&[x],[BizDim].[Local].[Local].ALLMEMBERS,[BizDim].[HQ].[HQ].ALLMEMBERS)}
)
However, this is no good because the actual query is parameterized, so it must work with a STRTO* function:
Where (
STRTOSET(#Counties, CONSTRAINED)
,STRTOSET(#BizTypes)
)
I have tried STRTOTUPLE and get the same error.
I could build the query dynamically but I'd rather avoid taking that risk, especially given that it worked fine before with a parameter.
So the question is, how to get this assymmetric set to work as a parameter again in SQL Server 2008 R2 SSAS?
Update:
Note that this eliminates the error by replacing the keys will ALLMEMBERS, but doesn't actually filter anything so it is useful only to show in general my syntax doesn't seem to be bad:
Where (
STRTOSET("{[BizDim].[County ID].&[16]}", CONSTRAINED)
,{([BizDim].[Corp].[Corp].ALLMEMBERS,[BizDim].[Local].[Local].ALLMEMBERS,[BizDim].[HQ].[HQ].ALLMEMBERS),([BizDim].[Corp].[Corp].ALLMEMBERS,[BizDim].[Local].[Local].ALLMEMBERS,[BizDim].[HQ].[HQ].ALLMEMBERS)}
)
I did manage to get this working in a less dynamic way, but quite annoying. Basically my filter would need to be divided into many different parameters because I would need a STRTOSET call for each one:
Where (
STRTOSET("{[BizDim].[County ID].&[16]}", CONSTRAINED)
,{
STRTOSET("([BizDim].[Corp].[Corp].ALLMEMBERS,[BizDim].[Local].[Local].ALLMEMBERS,[BizDim].[HQ].[HQ].&[x])")
,STRTOSET"([BizDim].[Corp].[Corp].&[x],[BizDim].[Local].[Local].ALLMEMBERS,[BizDim].[HQ].[HQ].ALLMEMBERS)")
}
)
[edited after comments]
Where
STRTOSET("{[BizDim].[County ID].&[16]}", CONSTRAINED)
* STRTOSET("{([BizDim].[Corp].[Corp].ALLMEMBERS,[BizDim].[Local].[Local].ALLMEMBERS,[BizDim].[HQ].[HQ].&[x]),([BizDim].[Corp].[Corp].&[x],[BizDim].[Local].[Local].ALLMEMBERS,[BizDim].[HQ].[HQ].ALLMEMBERS)}")
They might be an ambiguity with the ( x, y, z ) notation which is either a tuple or a parenthesis operator; e.g., ( {}, {} ) is a crossjoin.
Perhaps you need an explicit wrapping into a set :
{ [BizDim].[HQ].[HQ].&[x] }
Or replace:
([BizDim].[Corp].[Corp].ALLMEMBERS,[BizDim].[Local].[Local].ALLMEMBERS,[BizDim].[HQ].[HQ].&[x])
with an explicit crossjoin:
{ [BizDim].[Corp].[Corp].ALLMEMBERS * [BizDim].[Local].[Local].ALLMEMBERS * { [BizDim].[HQ].[HQ].&[x] } }
Hope that helps.
Related
When using primitive types such as Integer, I can without any problems do a query like this:
with connection.cursor() as cursor:
cursor.execute(sql='''SELECT count(*) FROM account
WHERE %(pk)s ISNULL OR id %(pk)s''', params={'pk': 1})
Which would either return row with id = 1 or it would return all rows if pk parameter was equal to None.
However, when trying to use similar approach to pass a list/tuple of IDs, I always produce a SQL syntax error when passing empty/None tuple, e.g. trying:
with connection.cursor() as cursor:
cursor.execute(sql='''SELECT count(*) FROM account
WHERE %(ids)s ISNULL OR id IN %(ids)s''', params={'ids': (1,2,3)})
works, but passing () produces SQL syntax error:
psycopg2.ProgrammingError: syntax error at or near ")"
LINE 1: SELECT count(*) FROM account WHERE () ISNULL OR id IN ()
Or if I pass None I get:
django.db.utils.ProgrammingError: syntax error at or near "NULL"
LINE 1: ...LECT count(*) FROM account WHERE NULL ISNULL OR id IN NULL
I tried putting the argument in SQL in () - (%(ids)s) - but that always breaks one or the other condition. I also tried playing around with pg_typeof or casting the argument, but with no results.
Notes:
the actual SQL is much more complex, this one here is a simplification for illustrative purposes
as a last resort - I could alter the SQL in Python based on the argument, but I really wanted to avoid that.)
At first I had an idea of using just 1 argument, but replacing it with a dummy value [-1] and then using it like
cursor.execute(sql='''SELECT ... WHERE -1 = any(%(ids)s) OR id = ANY(%(ids)s)''', params={'ids': ids if ids else [-1]})
but this did a Full table scan for non empty lists, which was unfortunate, so a no go.
Then I thought I could do a little preprocessing in python and send 2 arguments instead of just the single list- the actual list and an empty list boolean indicator. That is
cursor.execute(sql='''SELECT ... WHERE %(empty_ids)s = TRUE OR id = ANY(%(ids)s)''', params={'empty_ids': not ids, 'ids': ids})
Not the most elegant solution, but it performs quite well (Index scan for non empty list, Full table scan for empty list - but that returns the whole table anyway, so it's ok)
And finally I came up with the simplest solution and quite elegant:
cursor.execute(sql='''SELECT ... WHERE '{}' = %(ids)s OR id = ANY(%(ids)s)''', params={'ids': ids})
This one also performs Index scan for non empty lists, so it's quite fast.
From the psycopg2 docs:
Note You can use a Python list as the argument of the IN operator using the PostgreSQL ANY operator.
ids = [10, 20, 30]
cur.execute("SELECT * FROM data WHERE id = ANY(%s);", (ids,))
Furthermore ANY can also work with empty lists, whereas IN () is a SQL syntax error.
What is the difference between representing tuple in an mdx query as
[CF Type].[CF TYPE].&[6]
OR
[ValScen].[Scenario Type].&[Term Point Rates]
Can I use the &[number] and the &[actual string] format interchangeably? If the cube is generated by another system, can the &[6] be different for each generation in usual practice? Or is it safer to use StrToMember or StrToSet. But this is not efficient as per this article. To give some context, in my case I use it like in below pseudocode
SELECT NON EMPTY { [Measures].x} ON COLUMNS, NON EMPTY { (My Column1 * My Column2)} DIMENSION PROPERTIES MEMBER_CAPTION ON ROWS FROM ( SELECT ( { [CF Type].[CF TYPE].&[6] } ) ON COLUMNS FROM
( SELECT ( { [ValScen].[Scenario Type].&[Term Point Rates] } ) ON COLUMNS FROM [My Cube]]))
WHERE ( [ValScen].[Scenario Type].&[Term Point Rates], [CF Type].[CF TYPE].&[6] )
Those are members and not tuples. A Tuple would be surrounded by braces ().
I think they are interchangeable syntax if you’re using the caption such as .&[hello] and .[hello] but if you use the key then the ampersand is mandatory.
I'm working on a Tabular cube in Visual Studio.
I have a DAX formula in the cube that works fine:
SUMX(FILTER(factFHA, factFHA[EventCd]="D"), [LoanCount])
When I run it in SSMS as:
evaluate(
SUMX(FILTER(factFHA, factFHA[EventCd]="D"), [LoanCount])
)
it fails with following error:
Query (1, 1) The expression specified in the EVALUATE statement is not a valid table expression.
I have 2 other formulas that both work fine:
evaluate(factFHA)
evaluate(filter('factFHA', [EventCd] = "D"))
I can't figure out what is wrong with the SUMX with FILTER
Please advise. Thank you.
EVALUATE function only works if you pass a table or an expression that returns a table, you are passing a SUMX function which return a scalar value (Decimal).
The syntax to write queries using DAX, is as follows:
[DEFINE { MEASURE <tableName>[<name>] = <expression> } -> Define a session (optional) measure
EVALUATE <table> --> Generate a table using your measures or creating calculated columns
[ORDER BY {<expression> [{ASC | DESC}]}[, …] --> Order the returned table by a passed column or expression
[START AT {<value>|<parameter>} [, …]]] --> This is an ORDER BY Sub-clause to define from which the query results will start.
Define your measure then use then use it inside the EVALUATE clause using a expression that evaluates to a table.
DEFINE
MEASURE factFHA[MyMeasure] =
SUMX ( FILTER ( factFHA, factFHA[EventCd] = "D" ), [LoanCount] )
EVALUATE
( SUMMARIZE ( FILTER ( factFHA, factFHA[EventCd] = "D" ), factFHA[AnyColumnToGroup]
, "Sum of MyMeasure", SUM ( factFHA[MyMeasure] ) ) )
Let me know if this helps.
I have this where clause in sql language:
where (cond1=1 or cond2=1) and cond3=1
How can I get this result in MDX with the slicing(condition into the where)?
{[cond1].&[1],[cond2].&[1]} /*and*/ {[cond3].&[1]}
Thanks
Try to use a subcube:
Select
-- YOUR SELECTED MEASURES AND DIMENSIONS
From
(
Select
{[cond1].&[1],[cond2].&[1]} on 0
,{[cond3].&[1]} on 1
-- ,{more slices} on x
From [CubeName]
)
Hope this help!
You can use subcube expression as stated above, but this is not the only option. If you use subcube, you would increase query performance greatly (assuming the fact you don't perform crossjoins in it).
You can also use general WHERE keyword after last expression that returns cube:
select
{ .. } on 0,
{ .. } on 1
from (select { [Dim1].[X].allmembers } on 0)
where ([Dim2].[Y].&[Y1])
Or:
select
{ .. } on 0,
{ .. } on 1
from (select { [Dim1].[X].allmembers } on 0)
where {[DimTime].[Time].[Year].&[2001] : [DimTime].[Time].[Year].&[2015]}
This is applied at the end of execution, which means performance may decrease. However, if you need to apply external filter to all axis, this is the option you need.
Another way to filter member values is using tuple expressions:
with member LastDateSale as ( (Tail(EXISTING [DimTime].[Time].[Dates].members,1), [Measures].[ActualSales]) )
This will take your DimTime axis, apply external filter, get the last element from it and calculate [ActualSales] for it, if possible.
I have following query for calculated member:
SUM(
EXCEPT([Policy].[Policy Status].[Policy Status],[Policy].[Policy Status].&[Void])
, [Measures].[CountPolicyEndorsesNull]
)
What I want is to include another EXCLUDE filter. I have tried following:
SUM(
{EXCEPT([Policy].[Policy Status].[Policy Status],[Policy].[Policy Status].&[Void])}
*
{EXCEPT([Invoice].[Invoice Status].[Invoice Status],[Invoice].[Invoice Status].&[Void])}
, [Measures].[CountPolicyEndorsesNull]
)
but it returns more result than the first query. Any ideas?
Your syntax is correct. The reasons it could be more are the following:
You have negative results in ([Invoice].[Invoice Status].&[Void], [Measures].[CountPolicyEndorsesNull])
You have a default member set on [Invoice].[Invoice Status].[Invoice Status].
Try returning the following query:
select
[Measures].[CountPolicyEndorsesNull]
on columns,
{[Invoice].[Invoice Status].DefaultMember,
[Invoice].[Invoice Status].&[Void]
}
on rows
from [CubeName]
That will get you your culprit.