My cube has a fact table that contains a row for every modification made to any entity. What I need is a measure that will return a count of the entities based on the selected dimensions.
So if the user selects a month from the date dimension then the measure should return the number of entities that were modified that month (rather than the number of modifications).
In SQL this would be something like:
SELECT EntityID, COUNT(*)
FROM FactTable
WHERE Date BETWEEN X AND Y
GROUP BY EntityID
How can you do this in MDX? Surely this would be an extremely common scenario with cubes.
Your t-sql query is equivalent of mdx query:
select
[Measures].[<Fact rows count measure>] on columns,
<Entity dimension members> on rows
from [<Cube Name>]
where (<month member>)
In the above query [Fact rows count measure] would be a measure with aggregation formula Count - count of rows
However, if you need to return the distinct count of entity members when you slice by another dimension, you basically have several options:
create a distinct count measure on the entityID key
create a calculated measure with expression: count(exists(existing [Entity].[Entity].[Entity].MEMBERS,,'Measure Group Name'))
HTH,
Hrvoje Piasevoli
Related
I am having troubles to get correct values for sumSquare which is calculated column in following query:
SELECT
building_type,
COUNT(distinct building.id) as buildingCount,
SUM(squareTable.sumSquare) as sumSquare,
FROM building
LEFT JOIN (
SELECT building_id, SUM(square) as sumSquare
FROM building_square
WHERE (square >= '500')
GROUP BY building_id
) squareTable on (squareTable.building_id = building.id)
JOIN building_square ON (building_square.building_id = building.id)
WHERE building_square.square >= '500'
GROUP BY building_type
building_type is column from building table (f.e. some of the types are: house, apartment...), i need to group by building type.
Also i have relation 1:N between building and building_square tables. In main query in where clause i need to filter by square which is column from building_square table.
I quess that problem is that i am joining building_square table in main query as i need to filter on some fields from that table, while i also use subquery that calculates sum of squares for particular building. As a result in sum i am counting some rows more than one time.
How to calculate sum of squares in subquery only for distinct building_id?
Basically, I'm trying to retrieve only 1 record from a table based on catalog_no and packing_list_no. However, the table I'm retrieving the information from has additional details that I don't need but makes the 1 record I need into 3 distinct records.
I tried summing and grouping the info, but I'm still getting 3 records instead of 1.
Any ideas of how to solve this issue?
Your GROUP BY groups your result on the columns quantity picked, quantity shipped and weight shipped. A different value in any of those columns will result into a different row.
You can drop the GROUP BY clause all together, if the desirable result is the packing list and catalog no that you have specified. You can use the GROUP BY clause to columns that you do not use sum to group the result set.
SELECT catalog_no, sum(qty_picked), sum(qty_shipped), sum(weight_shipped), packing_list_no, bay_no, carrier_code, tracking_no FROM oeorder_shipping
WHERE packing_list_no='CP12618525' AND catalog_no='437656500'
GROUP BY bay_no, carrier_code, tracking_no;
I have cube within OLAP database (SSAS 2012) where [Persons], [Dates] (date of order), [Categories] (category of item within order) are dimensions and [Orders], [OrdersCategories] are fact tables. [OrdersCategories] is many-to-many table to connect orders with categiries.
Using query:
SELECT
{[Measures].[Orders Distinct Count]} ON COLUMNS,
{[Persons].[Id].Children} ON ROWS
FROM [DB]
WHERE (
{[Dates].[Id].&[2015-06-11T00:00:00] : [Dates].[Id].&[2015-06-17T00:00:00]},
{[Categories].[Id].&[10], [Categories].[Id].&[11]}
)
i can count number of orders (for specified time period and by specified categories) for each person. Is this case condition by category works like 'OR' condition and no matter order has relation with first category or second category or both.
Is it possible to write query with condition by categories which would work like 'AND' condition, namely order was counted only if it is related with both categories?
AND with members from the same hierarchy is generally implemented like the following:
SELECT
{[Measures].[Orders Distinct Count]} ON COLUMNS
,{[Persons].[Id].Children} ON ROWS
FROM [DB]
WHERE
Exists
(
Exists
(
{
[Dates].[Id].&[2015-06-11T00:00:00] : [Dates].[Id].&[2015-06-17T00:00:00]
}
,{[Categories].[Id].&[10]}
,"MeasuresGroupName"
)
,{[Categories].[Id].&[11]}
,"MeasuresGroupName"
);
You need to add a measure group name from your cube. Ameasure group name corresponds to the folder names in the Measures hierarchy - an example in Adventure Works is "Internet Sales"
Possible Alternative
I think you could try implementing the above using nested NonEmpty functions:
SELECT
{[Measures].[Orders Distinct Count]} ON COLUMNS
,{[Persons].[Id].Children} ON ROWS
FROM [DB]
WHERE
NonEmpty
(
NonEmpty
(
{
[Dates].[Id].&[2015-06-11T00:00:00] : [Dates].[Id].&[2015-06-17T00:00:00]
}
,{([Categories].[Id].&[10],[Measures].[Orders Distinct Count])}
)
,{([Categories].[Id].&[11],[Measures].[Orders Distinct Count])}
);
I'm trying to write an MDX query for a calculated member "Daily Active users" in my SSAS OLAP cube based on an attribute in the fact table called "IsDailyActive'
I have a measure called "Active Users" that uses the COUNT DISTINCT aggregation from the "Fact Activity" table on the UserId column. I want to reuse that measure and calculate distinct count of users by sepcifying the filter 'Is Daily Active' as True in my query.
I tried this, but it gives me null
WITH
SET MySet AS
{[Measures].[Active Users]}
MEMBER [Measures].[Daily Active Users] AS
DISTINCTCOUNT(MySet)
SELECT
{[Measures].[Daily Active Users]} ON COLUMNS
FROM MyDataCube
WITH
SET MySet AS
{[Measures].[Active Users]}
MEMBER [Measures].[Daily Active Users] AS
DISTINCTCOUNT(MySet)
SELECT
{[Measures].[Daily Active Users]} ON COLUMNS
FROM MyDataCube
WHERE
([Is Daily Active].[Boolean].&[True])
What I'm doing wrong?
Note: IsDailyActive column in the "FactActivity" table is of type "bit" so it has 0 or 1 values in the rows.
If the measure Active Users already exists in the cube script then why does the following not work?
SELECT
{[Measures].[Active Users]} ON COLUMNS
FROM MyDataCube
WHERE
([Is Daily Active].[Boolean].&[True])
I'm looking for the best way to implement measure that counts Orders by the number of products ordered.
There is a fact table of ProductsOrdered, like this
CREATE TABLE ProductsOrdered (
IdOrder UNIQUEIDENTIFIER,
IdProduct UNIQUEIDENTIFIER,
OrderDate DATE,
Quantity INT,
Price DECIMAL(18,2)
)
for every order there are rows in the table for every kind of product ordered.
I would like to have a measure that counts how many ordered a single product, how many 2, and so on.
Just like this SQL query does.
SELECT OrderedProductNumber, COUNT(IdOrder) AS NumberOfOrders
FROM (
SELECT IdOrder, COUNT(IdProduct) AS OrderedProductNumber
FROM ProductsOrdered
GROUP BY IdOrder
) t
GROUP BY OrderedProductNumber
Which MDX expression is best counterpart?
Thanks.
The problem you have is that you can only produce a count by a dimension. In this case, your dimension is the products ordered (1, 2, 3, etc.) Your option is to create a physical dimension that has the range of possibilities (tedious), or to create "dummy" members for a dimension to do the same thing as a calculation (tedious AND likely a performance problem). I would lean towards creating a dimension for products ordered, and then create a fact table using your SQL query and add the dimension and measure group to the cube. It's going to be easier to implement in the long run and by physically instantiating the counts, you will get much better query performance.
You can define dimension based on product
then measure group based on fact table, that you named ProductOrdered,
then you can define distinct count measure based on your IOrder column (e.g [IOrderdistinctcount])
after that you can use
select
{IOrderdstinccount} on 0,
[dim Product].[product].[Product].members on 1
from [Cube]