I have the following MDX query:
select
NON EMPTY [Measures].Members ON COLUMNS,
NON EMPTY {[MY_DIMENSION.MY_HIERARCHY].[VALUE].Members} ON ROWS
from
[MY_CUBE]
It gives the following result:
[Measures].[COUNT_TICKET]
-------------------------------------------------------------
[MY_DIMENSION.MY_HIERARCHY].[#null] 14 333 458 (<-- not needed)
[MY_DIMENSION.MY_HIERARCHY].[VAL1] 4 864
[MY_DIMENSION.MY_HIERARCHY].[VAL2] 5 588
[MY_DIMENSION.MY_HIERARCHY].[VAL3] 2 567
[MY_DIMENSION.MY_HIERARCHY].[VAL3] 4 500
Which takes a long time because there are a big number of null values out there.
Is it possible to filter the #null Members of my hierarchy?
Try the following
select
NON EMPTY {[Measures].Members} ON COLUMNS,
NON EMPTY {[MY_DIMENSION.MY_HIERARCHY].[VALUE].Members} ON ROWS
from
[MY_CUBE]
Related
I have a database which is a list of respondents to a 54-question survey in the following format...
Respondent ID Q_1 Q_2 Q_3 ... Q_54
1 5 3 [null] 2
...
3000 [null] 3 3 5
...and I have an aggregate query to get the count of respondents to each question...
Count_Q_1 Count_Q_2 Count_Q_3 ... Count_Q_54
1547 602 2999 1874
...and I am looking for a way to transpose the columns in the above query to get the following result...
Question Count_Respondents
Q_1 1547
Q_2 602
Q_3 2999
... ...
Q_54 1874
...is there any way for me to do this without 54 UNION queries (or multiple blocks of UNION queries that roll up to a master UNION query)?
No, there is not. Your results should've been normalized in the first place, Access doesn't support UNPIVOT or anything similar to it.
You can either unpivot data with many union queries, by using VBA, or by moving the data to Excel/SQL server/another program and unpivoting the results there.
I have two tables of zipcodes containing drivetime data. The first (called "DTM") contains 4 columns. The first two columns are From and To zipcodes, followed by drivetime and driving distance between them. This table contains every combination of zipcodes twice, with From and To zips reversed. it looks something like this:
FROM_ZIP TO_Zip Drivetime(min) Distance(mtr)
1011 1011 0 0
1011 1012 3 650
1011 1013 4 850
1011 1014 4 900
1012 1011 3 650
1012 1012 0 0
1012 1013 2 500
...
This table contains roughly 16.5 million records.
The second table (called "STOREZIPS") contains a list of zipcodes belonging to stores.
My goal here is to match every zipcode in the country to the zipcode of the store that is closest by, and show the drivetime and driving distance. So what i'm trying to do is extract from the first table the rows in which the TO_Zip matches one of the zipcodes in the second table, and have the lowest Drivetime(min).
There are however also instances where two Zip's have the same Drivetime(min) to another Zip. If this occurs the row with the lowest Distance(mtr) should be selected.
I've been trying to solve this for a while now, but just can't seem to get it in such a way that only the From_Zip that is closest by gets selected.
Any help or suggestion is appreciated.
I think that you will need to use CTE in that case. You can read more here:
https://technet.microsoft.com/en-us/library/ms190766(v=sql.105).aspx
If I understood you well I think that sample sql may look like this:
with cte as
(
select min(c.Drivetime) as minimum, c.zipT,
c.Distance, rank() over (partition by c.TO_Zip order by c.Distance) as place
from DTM c
inner join STOREZIPS s on c.TO_Zip = s.TO_Zip
where c.Drivetime > 0
group by c.TO_Zip, c.Distance
) select * from cte where place = 1
Let's start by saying that I'm a total newbie on MDX, I need to merge two (or more) query results into one pivot.
The queries will have the same dimensions on ROWS and COLUMNS, but different measures and filters (normally a time period).
Here is an example
Query 1:
SELECT
NON EMPTY {{[stores].[storecountry].[storecountry].Members}} ON COLUMNS,
NON EMPTY {{[SalesTypes].[Description].[Description].Members}} *
{[Measures].[TransactionValue], [Measures].[TransQty]} ON ROWS
FROM [Model]
WHERE ({[dDates].[Date].[Date].&[2016-01-05T00:00:00] : [dDates].[Date].[Date].&[2016-01-12T00:00:00]})
Result of query 1:
CA US
Regular Sale TransactionValue 761 16
Regular Sale TransQty 8 233
Return TransactionValue 156 4
Return TransQty 1 45
Query 2:
SELECT
NON EMPTY {{[stores].[storecountry].[storecountry].Members}} ON COLUMNS,
NON EMPTY {{[SalesTypes].[Description].[Description].Members}} *
{[Measures].[DiscountPerc]} ON ROWS
FROM [Model]
WHERE ({[dDates].[Date].[Date].&[2015-03-12T00:00:00] : [dDates].[Date].[Date].&[2015-06-02T00:00:00]})
Result of query 2:
CA US
Regular Sale DiscountPerc 40 % 59 %
Return DiscountPerc 32 % 43 %
Expected result after merging
CA US
Regular Sale TransactionValue 761 16
Regular Sale TransQty 8 233
Regular Sale DiscountPerc 40 % 59 %
Return TransactionValue 156 4
Return TransQty 1 45
Return DiscountPerc 32 % 43 %
Is it achievable without manually merging the AdomdClient.CellSet from the calling application?
Thank you!
I'd use calculated members:
with
Member [Measures].[TransactionValueReport] as
Aggregate(
{[dDates].[Date].[Date].&[2016-01-05T00:00:00]:[dDates].[Date].[Date].&[2016-01-12T00:00:00]},
[Measures].[TransactionValue]
)
Member [Measures].[TransQtyReport] as
Aggregate(
{[dDates].[Date].[Date].&[2016-01-05T00:00:00]:[dDates].[Date].[Date].&[2016-01-12T00:00:00]},
[Measures].[TransQty]
)
Member [Measures].[DiscountPercReport] as
Aggregate(
{[dDates].[Date].[Date].&[2015-03-12T00:00:00]:[dDates].[Date].[Date].&[2015-06-02T00:00:00]},
[Measures].[DiscountPerc]
)
Select
Non Empty [stores].[storecountry].[storecountry].Members on 0,
Non Empty [SalesTypes].[Description].[Description].Members * {[Measures].[TransactionValueReport],[Measures].[TransQtyReport],[Measures].[DiscountPercReport]} on 1
From [Model]
I have this issue with an MDX query where the NON EMPTY clause isn’t working as I expected after adding KPI goals, trends and status to the query.
The basic query looks like this
SELECT
NON EMPTY({[Measure1], [Measure2], KPIValue('MyKpi')})
ON COLUMNS,
NON EMPTY [Dim Country].[Name].[Name].ALLMEMBERS ON ROWS
FROM [BIA CO]
returning something like this, which is fine:
Measure1 Measure2 MyKpi
Canada 7977 4487 3231
USA 6 14 6
UK 442 1179 180
Problems comes when I add KPI goal, trend and status:
SELECT
NON EMPTY({[Measure1], [Measure2], KPIValue('MyKpi'), KPIGoal('MyKpi'), KPIStatus('MyKpi'), KPITrend('MyKpi')})
ON COLUMNS,
NON EMPTY [Dim Country].[Name].[Name]ALLMEMBERS ON ROWS
FROM [BIA CO]
Which returns something like:
Measure1 Measure2 MyKpi MyKpi Goal MyKpi Status MyKpi Trend
Mexico (null) (null) (null) 40300 -1 -1
Cuba (null) (null) (null) 40300 -1 -1
Canada 7977 4487 3231 40300 -1 1
Portugal (null) (null) (null) 40300 -1 -1
China (null) (null) (null) 40300 -1 -1
USA 6 14 6 40300 -1 1
UK 442 1179 180 40300 -1 1
How can I get rid of all those rows with nulls except for the goal, status and trend?
Simply add the Non-empty behavior to your KPIs. If you set Measure1 you'll filter out all empty tuples. It'll inherit the non-empty behavior from the Measure1. Also you may return the the following set on rows with similar logic (the output will be the same):
NonEmpty([Dim Country].[Name].[Name].ALLMEMBERS,[Measures].[Measure1]) on rows
I had a similar problem and this is what I did. In my case I had no control over the cube design. I'm not an expert so wait for some experienced feedback before using it.
WITH
MEMBER [KPIValue(ReservaKPI)] as KPIValue('ReservaKPI')
MEMBER [KPIGoal(ReservaKPI)] as CASE WHEN ISEMPTY([KPIValue(ReservaKPI)]) THEN NULL ELSE KPIGoal('ReservaKPI') END
MEMBER [KPIStatus(ReservaKPI)] as CASE WHEN ISEMPTY([KPIValue(ReservaKPI)]) THEN NULL ELSE KPIStatus('ReservaKPI') END
MEMBER [KPITrend(ReservaKPI)] as CASE WHEN ISEMPTY([KPIValue(ReservaKPI)]) THEN NULL ELSE KPITrend('ReservaKPI') END
SELECT
NON EMPTY({[Cant Adultos], [Cant Noches], [KPIValue(ReservaKPI)], [KPIGoal(ReservaKPI)], [KPIStatus(ReservaKPI)], [KPITrend(ReservaKPI)]})
ON COLUMNS,
NON EMPTY [Dim Mun].[NOMBRE].[NOMBRE].ALLMEMBERS ON ROWS
FROM [BIA CO]
I sometimes like to use the HAVING clause like this maybe:
SELECT
NON EMPTY
{
[Measure1]
,[Measure2]
,KPIValue('MyKpi')
,KPIGoal('MyKpi')
,KPIStatus('MyKpi')
,KPITrend('MyKpi')
} ON COLUMNS
,NON EMPTY
[Dim Country].[Name].[Name].ALLMEMBERS HAVING
(NOT IsEmpty([Measure1])) AND (NOT IsEmpty([Measure2])) ON ROWS
FROM [BIA CO];
But you could move that logic into a WITH clause as suggested by #user1998299 - I'd probably do a custom set:
WITH
SET [SmallSet] AS
NonEmpty
(
NonEmpty
(
[Dim Country].[Name].[Name].ALLMEMBERS
,[Measure1]
)
,[Measure2]
)
SELECT
NON EMPTY
{
[Measure1]
,[Measure2]
,KPIValue('MyKpi')
,KPIGoal('MyKpi')
,KPIStatus('MyKpi')
,KPITrend('MyKpi')
} ON COLUMNS
,NON EMPTY
[SmallSet] ON ROWS
FROM [BIA CO];
How to divide a summed field by another summed field in the same query.
Example: lets have the query "querySummary" which its field have been grouped already
SID SumOfCredits SumOfMarks
1 3 18
2 2 20
3 4 40
Group By Sum Sum
I want to add another field named "FAvg" to the same query that builds up of dividing "SumOfMarks" by SumOfCredits, so the the result should be as following
SID SumOfCredits SumOfMarks FAvg
1 3 18 6
2 2 20 10
3 2 40 20
Any help please ? many Thanks
Replace "Sum" in the "Total" row by "Expression" and in the "Field" row use the expression:
FAvg: Sum(Mark)/Sum(Credit)
You'll get something like this:
(The other Sum columns are not required for the FAvg expression)
The SQL looks like this:
SELECT
Table1.SID,
Sum(Table1.Credit) AS SumOfCredit,
Sum(Table1.Mark) AS SumOfMark,
Sum([Mark])/Sum([Credit]) AS FAvg
FROM
Table1
GROUP BY
Table1.SID;