How to find sum of each group in Hierarchy in MDX - ssas

In the example below I want to find sum of each group. Can someone correct my MDX query to get the desired result for GroupCount Column.
with member [GroupCount] as
aggregate([Department].[Department ID].currentmember.parent,
[Measures].[Pay Frequency])
select {[Department].[Group Name].[all].children*
[Department].[Department ID].[all].children
} on rows,
{[Measures].[Pay Frequency],[GroupCount]} on columns
from test
The result I am getting from the above query is:
However I need the output as :

Is it like this?
with member [GroupCount] as
(
[Department].[Group Name].CURRENTMEMBER,
[Department].[Department ID].[All],
[Measures].[Pay Frequency]
)
select {[Department].[Group Name].[all].children*
[Department].[Department ID].[all].children
} on rows,
{[Measures].[Pay Frequency],[GroupCount]} on columns
from test;

Related

Group By with Case Statements SQL Server

I would like to group by with the case statements in SQL Server. I created cases that's my revenue range. I'd like to calculate the number of sales orders within all these revenue ranges(cases)
Below please find my queries:
Select
sum(OrderQuantity) as Orders,
case when (sum(SalesAmount-TaxAmt-Freight)<100 and sum(SalesAmount-TaxAmt-Freight)>=0) then '$0-$100'
when (sum(SalesAmount-TaxAmt-Freight)>=100 and sum(SalesAmount-TaxAmt-Freight)<500) then '$100-$500'
when (sum(SalesAmount-TaxAmt-Freight)>=500 and sum(SalesAmount-TaxAmt-Freight)<1000) then '$500-$1000'
when (sum(SalesAmount-TaxAmt-Freight)>=1000 and sum(SalesAmount-TaxAmt-Freight)<2500) then '$1000-$2500'
when (sum(SalesAmount-TaxAmt-Freight)>=2500 and sum(SalesAmount-TaxAmt-Freight)<5000) then '$2500-$5000'
when (sum(SalesAmount-TaxAmt-Freight)>=5000 and sum(SalesAmount-TaxAmt-Freight)<10000) then '$5000-$10000'
when (sum(SalesAmount-TaxAmt-Freight)>=10000 and sum(SalesAmount-TaxAmt-Freight)<50000) then '$10000-$50000'
when (sum(SalesAmount-TaxAmt-Freight)>=50000 and sum(SalesAmount-TaxAmt-Freight)<100000) then '$50000-$100000'
when (sum(SalesAmount-TaxAmt-Freight)>=100000) then '>$100000'
end as SalesAmountCategory
From
dbo.FactResellerSales
group by
SalesAmountCategory;
I expect to get the result like:
result example
I keep getting errors when i try to group by based on the case statements. The error is "Invalid column name 'SalesAmountCategory'". How can i do that? Many thanks in advance!
It looks like you want to know number of orders in each category
So, you need first to map each order to category and then group by it, like that:
select SalesAmountCategory, count(*) from
(
Select case
when ((SalesAmount-TaxAmt-Freight)>=100000) then '>$100000'
when ((SalesAmount-TaxAmt-Freight)>=50000) then '$50000-$100000'
when ((SalesAmount-TaxAmt-Freight)>=10000) then '$10000-$50000'
when ((SalesAmount-TaxAmt-Freight)>=5000) then '$5000-$10000'
when ((SalesAmount-TaxAmt-Freight)>=2500) then '$2500-$5000'
when ((SalesAmount-TaxAmt-Freight)>=1000) then '$1000-$2500'
when ((SalesAmount-TaxAmt-Freight)>=500) then '$500-$1000'
when ((SalesAmount-TaxAmt-Freight)>=100) then '$100-$500'
when ((SalesAmount-TaxAmt-Freight)<100) then '$0-$100'
end as SalesAmountCategory
From dbo.FactResellerSales
) as t
group by SalesAmountCategory

How can i use a subquery or (WITH)statment in MDX Query?

I have a CustomerToFactor as a Measure and Customer as a Dimension. Now I want to create a MDX code like this SQL code but I can't. because (WITH) statements has another meaning in MDX.
with Total_Customer(
select cus_id
,sum(ctf_price) cus_total_price
from dbo.Customer
join dbo.CustomertoFactor on cus_id = ctf_cus_id
group by cus_id
)
select cus_id
,cus_name
,ctf_date
,ctf_price
,(cus_total_price / 100 * ctf_price) as Price_pro_customer
from dbo.Customer
join dbo.CustomertoFactor on cus_id = ctf_cus_id
join Total_Customer on Total_customer.cus_id = dbo.Customer.cus_id
SELECT NON EMPTY { [Measures].[ctf_date]
,[Measures].[ctf_price]
, (?) Price_pro_customer
} ON COLUMNS
,NON EMPTY {[Customer].[Customer - cus_name].[Customer - cus_name].ALLMEMBERS}
FROM [CustomerToFactor]
Thanks for your Answers. but it doesn't work. Actually I want it to be grouped for every name you name. for Example: for the name Alex only the sum would have to be calculated for Alex(100+300 = 400) as well as Group by.
I do not really understand the point of the calculation :)
But anyway, in MDX you can have your own measures calculated like this:
WITH MEMBER [Measures].[Price_pro_customer] AS
(SUM([Measures].[ctf_price]) / 100 * [Measures].[ctf_price])
SELECT NON EMPTY { [Measures].[ctf_date]
,[Measures].[ctf_price]
,[Measures].[Price_pro_customer]
} ON COLUMNS
,NON EMPTY {[Customer].[Customer - cus_name].[Customer - cus_name].ALLMEMBERS}
FROM [CustomerToFactor]
I am not sure you'll get the same result as the SQL query though, since you have [Customer].[Customer - cus_name].[Customer - cus_name].ALLMEMBERS on the rows which basically does a GROUP BY on the customer name.
So if in the table you had several rows for the same customer the output of MDX query should be 1 row for each customer. The SUM([Measures].[ctf_price]) is also different since it sums over all customers
I think you should create a date dimension reference to ctf_date.
Then your mdx should be as below:
WITH MEMBER [Measures].[Price_pro_customer] AS
SUM([DimDate].[ctf_date].[All], [Measures].[ctf_price]) / 100 * [Measures].[ctf_price]
SELECT NON EMPTY {
[Measures].[ctf_price] ,
[Measures].[Price_pro_customer]
} ON COLUMNS ,
NON EMPTY {[Customer].[Customer - cus_name].[Customer - cus_name].ALLMEMBERS *
[DimDate].[ctf_date].[ctf_date].ALLMEMBERS} ON ROWS
FROM [CustomerToFactor]

Filter in MDX query with Date Range filter and Year Filter

I'm trying to filter my MDX query of can someone help me on how this can be done.
My code
Select
[Measures].[Net_Cost] on 0,
[State].[City] on 1
FROM
[SALES]
WHERE
([Date].[Date.AYearly].[2017]),([Date].[Date.AMonthly].[1] : [Date].[Date.AMonthly].[12])
and it throws an error.
I want to accomplish the following:
Where
year = 2017 and month is between 1 and current month.
How can this be done.
Thanks in advance
Maybe try one filter in a subselect and the other in the outer query:
SELECT
[Measures].[Net_Cost] on 0,
[State].[City] on 1
FROM
(
SELECT
[Date].[Date.AYearly].[2017] ON 0
FROM [SALES]
)
WHERE
([Date].[Date.AMonthly].[1] : [Date].[Date.AMonthly].[12])
;
Thanks #whythe1 i did something like this
SELECT
NON EMPTY
[Date].[Date.AYearly].[Year].[2017]:
[Date].[Date.AYearly].[Year].[2017].PrevMember ON 0,
NON EMPTY
CROSSJOIN
(
CROSSJOIN(
{[Measures].[Net_Prod_Cost]},
{[Territory].[Sales_Territory_Branch_Number].Members,
[Territory].[Sales_Territory_Branch].Members}
),
{[Products].[Products.SBM].[RYAN WEST]}
) ON ROWS
FROM [SALES]
WHERE
(
({[Date].[Date.AMonthly].[Month].[1] :
[Date].[Date.AMonthly].[Month].[11]})
)}
and it worked.
Thanks your answer gave me an idea.

UNION multiple MDX queries in SSAS (powerpivot)

I have some sort of difficulties trying to join 2 MDX queries together. When running them separately they work fine. The script below
WITH
MEMBER [Measures].[ParameterCaption] AS
[Main_Incidents].[Priority].CurrentMember.Member_Caption
MEMBER [Measures].[ParameterValue] AS
[Main_Incidents].[Priority].CurrentMember.UniqueName
MEMBER [Measures].[ParameterLevel] AS
[Main_Incidents].[Priority].CurrentMember.Level.Ordinal
SELECT
{
[Measures].[ParameterCaption]
,[Measures].[# Incidents]
,[Measures].[%SLA]
} ON COLUMNS
,[Main_Incidents].[Priority].ALLMEMBERS ON ROWS
FROM [Model];
WITH
MEMBER [Measures].[ParameterCaption] AS
[Main_Incidents].[usr_directorate].CurrentMember.Member_Caption
MEMBER [Measures].[ParameterValue] AS
[Main_Incidents].[usr_directorate].CurrentMember.UniqueName
MEMBER [Measures].[ParameterLevel] AS
[Main_Incidents].[usr_directorate].CurrentMember.Level.Ordinal
SELECT
{
[Measures].[ParameterCaption]
,[Measures].[# Incidents]
,[Measures].[%SLA]
} ON COLUMNS
,[Main_Incidents].[usr_directorate].ALLMEMBERS ON ROWS
FROM [Model];
The most important bit for me is that I need the label column to show. So I want to UNION the 2 queries together so that the ParameterCaption captures values from "Priority" dimension and "Directorate" dimension....
Please someone help me to achieve this?
This is a bit complex, but definitely possible.
Union in MDX only works for members of the same hierarchy, so to achieve this we need to make the row members into Tuples that combine the two hierarchies. We can do this by cross joining each of the ALLMEMBERS sets to the [All] member for the other hierarchy. Then we just need to change the Parameter Caption, Value and Level to conditionally get the value from the appropriate hierarchy.
This could look something like the code below:
WITH
MEMBER [Measures].[ParameterCaption] AS
IIF([Main_Incidents].[Priority].CurrentMember.Level.Ordinal = 0, [Main_Incidents].[usr_directorate].CurrentMember.Member_Caption, [Main_Incidents].[Priority].CurrentMember.Member_Caption)
MEMBER [Measures].[ParameterValue] AS
IIF([Main_Incidents].[Priority].CurrentMember.Level.Ordinal = 0, [Main_Incidents].[usr_directorate].CurrentMember.UniqueName, [Main_Incidents].[Priority].CurrentMember.UniqueName)
MEMBER [Measures].[ParameterLevel] AS
IIF([Main_Incidents].[Priority].CurrentMember.Level.Ordinal = 0, [Main_Incidents].[usr_directorate].CurrentMember.Level.Ordinal , [Main_Incidents].[Priority].CurrentMember.Level.Ordinal)
SELECT
{
[Measures].[ParameterCaption]
,[Measures].[# Incidents]
,[Measures].[%SLA]
} ON COLUMNS
,{
[Main_Incidents].[Priority].ALLMEMBERS * [Main_Incidents].[usr_directorate].[All],
[Main_Incidents].[Priority].[All] * [Main_Incidents].[usr_directorate].ALLMEMBERS
} ON ROWS
FROM [Model];

How to sum column of result in sql server

I'm facing a problem, I want to sum the column of my results, this my code:
SELECT
[study_service]
,DATEDIFF(minute,[study_date_arrivee],[study_date_patient_prepared]) as [Time-ARRIVED-PREPARED]
,DATEDIFF(minute,[study_date_arrivee],[study_date_start]) as [Time-ARRIVED-START]
,DATEDIFF(minute,[study_date_start],[study_date_dossier_pret]) as [Time-START-DOSSPRET]
,DATEDIFF(minute,[study_date_dossier_pret],[study_date_remise_resultat]) as [Time-DOSSPRET-DOSSREMIS]
FROM
[VisionTST].[dbo].[PremierTrimestre]
I want the final result like this:
Total of `[Time-ARRIVED-PREPARED]`,`[Time-ARRIVED-Start]`,...
SELECT
[study_service]
,sum(DATEDIFF(minute,[study_date_arrivee],[study_date_patient_prepared])) as [Time-ARRIVED-PREPARED]
,sum(DATEDIFF(minute,[study_date_arrivee],[study_date_start])) as [Time-ARRIVED-START]
,sum(DATEDIFF(minute,[study_date_start],[study_date_dossier_pret])) as [Time-START-DOSSPRET]
,sum(DATEDIFF(minute,[study_date_dossier_pret],[study_date_remise_resultat])) as [Time-DOSSPRET- DOSSREMIS]
FROM [VisionTST].[dbo].[PremierTrimestre]