Adding together cube measures with different where clauses - mdx

I Have two MDX queries,
select
[Measures].[Goals A] on 0
from [FDC Star]
where [Squad A].[Squad Key]
And
select
[Measures].[Goals B] on 0
from [FDC Star]
where [Squad B].[Squad Key]
I would like to add the two values together but being very new to MDX I have no idea.

You can use calculated members:
WITH
MEMBER [Measures].[A] AS ([Measures].[Goals A], [Squad A].[Squad Key])
MEMBER [Measures].[B] AS ([Measures].[Goals B], [Squad B].[Squad Key])
SELECT {[Measures].[A], [Measures].[B]} ON 0
FROM [FDC Star]

Related

SQL - Display columns from two tables with where clause, preform count and GROUP BY COLUMNS in one table

I have two tables. One table is the main registered Boat table that holds most of the information. it has many columns one of them is [MFR CODE].
The other table is a reference table that contains all the records associated with only Business Boats and it has only 3 columns : [MFR CODE], [MFR NAME] & [MODEL]
I'm trying to find how many times (count) Business Boats appear in the Registered boat tables.
SELECT
Count(*) As 'TotalNumberBoats'
, [BoatsReg].[dbo].[MASTER].[MFR MDL CODE]
,[BoatsReg].[dbo].[BusinessBoats].[MFR]
,[BoatsReg].[dbo].[BusinessBoats].[MODEL]
FROM [BoatsReg].[dbo].[MASTER],[BoatsReg].[dbo].[BusinessBoats]
Where [BoatsReg].[dbo].[MASTER].[MFR MDL CODE] = [BoatsReg].[dbo].[BusinessBoats].[CODE]
group by [BoatsReg].[dbo].[BusinessBoats].[CODE]
order by TotalNumberBoats asc
How do i get rid of all the square brackets, it's annoying.
why do i get an error ?
Well when you aggregate a result, you have to specify all other fixed columns in the GROUP BY clause
(Answer edited to add total row with a UNION ALL and add a sort field to put the total row as last one)
In your case:
SELECT 1 as sorted,
,Count(*) As TotalNumberBoats
,MASTER.[MFR MDL CODE]
,BusinessBoats.MFR
,BusinessBoats.MODEL
FROM BoatsReg, BusinessBoats
Where MASTER.[MFR MDL CODE] = BusinessBoats.CODE
Group by
,MASTER.[MFR MDL CODE]
,BusinessBoats.MFR
,BusinessBoats.MODEL
UNION ALL
SELECT 2 as sorted,
,Count(*) As TotalNumberBoats
,'','',''
FROM BoatsReg
Order by sorted, TotalNumberBoats

join 2 mdx queries into one table

I need to merge the result 2 queries into one table. Queries are similar except one of WHERE conditions.
As far as I was able to find out while googling it is impossible to do as MDX have internal connections in database design.
I have tried to use this way: Merge 2 MDX queries
But it turns out that in 1 hour I get this error:
XML for Analysis parser: The XML for Analysis request timed out before it was completed.
I have tried to make new members like that:
member new_A AS
aggregate
(
K
,
A
)
And then
select { new_A, ...
select { A , B , C , D } on 0,
non empty { Y * Z } on 1
from X
where (except(K), L, M, N, P);
select { A , B , C , D } on 0,
non empty { Y * Z } on 1
from X
where (K, L, M, N, P);
What I need to get in the end is a table that contains values of elements A,B,C,D as columns for condition K only and for all except condition K just next to it. it can be either A, new_A, B, new_B, etc or A, B, C, D , new_A, new_B, etc.
P.S. database is extremely big and the faster it works the better :)
So i have tried to map your problem to AdventureWorks sample database.
In my problem i am trying report [internet Sales Amount] for some countries for a set of subcategories. Then I edit my query to report all subcategories except "Road Bikes" in main columns and for road bikes i use measures.RoadBikes.
Query 1
with member
measures.RoadBikes
as
( ([Product].[Subcategory].&[2],[Measures].[Internet Sales Amount]))
select
non empty
{
({[Customer].[Country].&[Australia],[Customer].[Country].&[Canada],[Customer].[Country].&[France],[Customer].[Country].&[United Kingdom],[Customer].[Country].&[United States]},
[Measures].[Internet Sales Amount]
),
({[Customer].[Country].&[Australia],[Customer].[Country].&[Canada],[Customer].[Country].&[France],[Customer].[Country].&[United Kingdom],[Customer].[Country].&[United States]},
measures.RoadBikes
)
}
on columns,
non empty
[Date].[Month of Year].[Month of Year]
on
rows
from
[Adventure Works]
where
({[Product].[Subcategory].&[31],[Product].[Subcategory].&[1],[Product].[Subcategory].&[2],[Product].[Subcategory].&[37],[Product].[Subcategory].&[3]})
Result
Edit the query
with member measures.UsRoadBikes as ([Product].[Subcategory].&[2],[Measures].[Internet Sales Amount])
select non empty
{({[Customer].[Country].&[Australia],[Customer].[Country].&[Canada],[Customer].[Country].&[France],[Customer].[Country].&[United Kingdom],[Customer].[Country].&[United States]},
[Measures].[Internet Sales Amount])}
on columns,
non empty
({[Product].[Subcategory].&[31],[Product].[Subcategory].&[1],[Product].[Subcategory].&[2],[Product].[Subcategory].&[37],[Product].[Subcategory].&[3]},
[Date].[Month of Year].[Month of Year]
) on rows
from [Adventure Works]
Result
Thanks everyone!
It was decided to run queries in parallel to speed up.
Return data to be populated into dataset and then Linq to be used to work with it.

Add additional expression column to cross-tab - Is this possible?

I have a cross-tab that aggregates values by month and year as averages. The only component left is calculating the spread between the two agg. function columns. The user is able to pick the two nodes in which they want to see the spreads for. The user can also pick the year.
Here is the code:
PARAMETERS [Node 1] Long, [Node 2] Long, [Year] Long;
TRANSFORM Format(Avg([Monthly values].[total_lmp_on]),"Fixed") AS AVG_ON_LMP
SELECT [Monthly values].Month, [Monthly values].Year
FROM [Monthly values]
WHERE ((([Monthly values].pnode_id)=[Node 1] Or ([Monthly values].pnode_id)=[Node 2])AND [Monthly values].Year = [Year])
GROUP BY [Monthly values].Month, [Monthly values].Year
PIVOT [Monthly values].pnode_id;
The user is prompted Node 1, Node 2, and year. Lets say:
Node 1: 12345
Node 2: 6789
Year: 2017
The following will show:
Month--------------Year-----------12345--------------6789
Jan-----------------2017------------10-----------------20
Feb-----------------2017------------15-----------------15
March----------------2017------------5-----------------0
April-----------------2017------------20-----------------10
The problem
How can I add a column that will give me the spread between the two chosen nodes? Which would look like:
Month--------------Year-----------12345--------------6789-------------Spread
Jan-----------------2017------------10-----------------20----------------(-10)
Feb-----------------2017------------15-----------------15-----------------(0)
March----------------2017------------5-----------------0------------------(5)
April-----------------2017------------20-----------------10---------------(10)
I am fairly certain this isn't possible, but would like to exhaust all resources. Or if there are any other options out there.
Consider conditional aggregation using AVG(IIF(...)) to find the difference of corresponding node averages. Do note: Spread column will appear to the right of pivoted columns.
PARAMETERS [Node 1] Long, [Node 2] Long, [Year] Long;
TRANSFORM Format(AVG(m.[total_lmp_on]), "Fixed") AS AVG_ON_LMP
SELECT m.Month, m.Year,
AVG(IIF(m.pnode_id = [Node1], m.[total_lmp_on], NULL)) -
AVG(IIF(m.pnode_id = [Node2], m.[total_lmp_on], NULL)) AS Spread
FROM [Monthly values] m
WHERE (m.pnode_id) IN ([Node 1], [Node 2]) AND (m.Year = [Year])
GROUP BY m.Month, m.Year
PIVOT m.pnode_id;

SUM and multiplying on calculated fields

I have a piece of script which is pulling through the desired figures fine. Now I need to multiply the figure given by another calculated field (I shall call field x, which is a case statement with 9 'When Statements') then multiply that by 0.74.
I can not get this to work at all... any ideas... script below
Case
when Left([dbo].[Table1].FIELD 1,3) = 'NEW' and [Field 2] = 'Live'
Then
(SELECT distinct[dbo].[Table2].[Field a]
FROM [MY_DATABASE].[dbo].[Table2]
right Join [dbo].[Table 3]
on ([dbo].[Table2].[Field b]=[dbo].[Table 3].[Field 1])
Where [dbo].[Table2].[Field c] =
(Select
[dbo].[Table 3].[Field1]
From [dbo].[Table 3]
Where [dbo].[Table 3].[Field4] = #parameter
and [Field5] = '7')
and [dbo].[Table2].[Field D]= #Parameter
and [dbo].[Table2].[Field E]= '07')
ELSE 0
END
As [Named Field]
One option here would be common table expressions.
I'd suggest wrapping your select in a cte, the selecting from it and performing any further calculations at the bottom level.
;with cte as (
your original select query
)
select *
, [named field] * [field x] as [calculated answer]
from cte
This saves you having to make the calculations over and over again. There are performance concerns around cte's but they can help in these situations. I'd test on your data and see how much of a hit you take.

WHERE clause in calculated member

I am facing some problem to calculate values from comparing dimension's value. I have 3 dimensions (Datatype, Customer, Product) and one measure (GrossSales).
What would be the MDX query if I want GrossSales for ProductID = 1,2,3 and Dataype = 4,5,6?
Here Datatype has relationship with GrossSales, Customer has relationship with GrossSales and Product has relationship with Customer.
I am trying this but doesn't work
CREATE MEMBER CURRENTCUBE.[Measures].Forecast_Gross_Sales AS
(
SELECT NON Empty [Measures].[Gross Sale]
FROM [Measures]
WHERE (
[Data Type].[ID].[ID] = 4
AND [Chain].[Customer ID] = [Measures].[Customer ID]
)
), VISIBLE = 1
, DISPLAY_FOLDER = 'Forecast'
, ASSOCIATED_MEASURE_GROUP = 'Data Types';
It looks like you are just getting started with MDX. There are some fundamental concepts that will help you get what you need. This comparison of SQL and MDX might be helpful. MDX uses the where clause as a slicer (to select certain dimension members) rather than a filter. You can't put member = somevalue in the where clause. And you can't really use the where clause to define a relationship to some other table.
Instead, your where clause would be something more like
[Data Type].[ID].[ID].&[4]
Since I can't see your data model, I can't be sure, but I would guess that [Chain].[Customer ID] = [Measures].[Customer ID] is something that you want to define the the dimension usage of your cube rather than in the query.
Edit: Now that the question has been edited, it looks like you are creating a calculated member. in this case there is no select or where clause. It will look more like this:
CREATE MEMBER CURRENTCUBE.[Measures].Forecast_Gross_Sales AS
Aggregate([Data Type].[ID].[ID].&[4], [Measures].[Gross Sale])
, VISIBLE = 1
, DISPLAY_FOLDER = 'Forecast'
, ASSOCIATED_MEASURE_GROUP = 'Data Types';
The relationship from the measure group through the Customer dimension to the Chain dimension is something that should be defined in the dimension usage. This is called a Reference dimension relationship.