MDX How to calculate measure against dimension without displaying members in results - sql-server-2008

Could someone please help me with the below MDX problem:
I got the dataset like the one below:
With
Set Range1 as {[Date].[Calendar].[Month].&[2008]&[1]
:[Date].[Calendar].[Month].&[2008]&[12]}
Set Range2 as
{[Delivery Date].[Calendar].[Month].&[2008]&[1]:
[Delivery Date].[Calendar].[Month].&[2008]&[12]}
MEMBER measures.A as [Measures].[Internet Order Count] + [Measures].[Reseller Order Count]
select {[Measures].[Internet Order Count],A} on columns,
non empty(
[Sales Territory].[Sales Territory Country].[Sales Territory Country]*
Range1
*Range2
)
having [Date].[Calendar].currentmember.member_caption
= [Delivery Date].[Calendar].currentmember.member_caption
AND A > 100
on rows
from [Adventure Works]
The problem is that results are displayed with [Sales Territory] Dimension - I would like to get the results only displaying months but with measure A also calculated against [Sales Territory].
The results for measure A should looks like:
For example: 1128 for January
The Correct results should looks like:
Thanks
Sorry - was unable to past the code:

WITH MEMBER [Measures].[A] AS
[Measures].[Internet Order Count] + [Measures].[Reseller Order Count]
SELECT
{ [Measures].[Internet Order Count], [Measures].[A] } ON COLUMNS,
[Date].[Calendar].[Month] ON ROWS
FROM (
SELECT { [Measures].[Internet Order Count] } ON COLUMNS,
Filter (
Filter ( [Date].[Calendar].[Month] * [Delivery Date].[Calendar].[Month], [Date].[Calendar].CurrentMember.Name = [Delivery Date].[Calendar].CurrentMember.Name ) * [Sales Territory].[Sales Territory Country].[Sales Territory Country],
[Measures].[Internet Order Count] + [Measures].[Reseller Order Count] > 100
) ON ROWS
FROM [Adventure Works]
)
WHERE [Date].[Calendar Year].[CY 2008]

I've ignored the ranges for now but this is as close as I can get:
WITH
MEMBER measures.c1 AS
[Date].[Calendar].CurrentMember.Member_Caption
MEMBER measures.c2 AS
[Delivery Date].[Calendar].CurrentMember.Member_Caption
MEMBER measures.x AS
IIF
(
measures.c1 = measures.c2
,
[Measures].[Internet Order Count] + [Measures].[Reseller Order Count]
,null
)
SET q AS
Filter
(
[Sales Territory].[Sales Territory Country].[Sales Territory Country]*
[Date].[Calendar].[Month].MEMBERS*
[Delivery Date].[Calendar].[Month].MEMBERS
,
measures.x > 100
)
SELECT
{measures.x} ON 0
,NON EMPTY
Extract
(
q
,[Date].[Calendar]
,[Delivery Date].[Calendar]
) ON 1
FROM [Adventure Works];
EDIT
The following gives two options for the Rows - option A and option B:
WITH
MEMBER measures.c1 AS
[Date].[Calendar].CurrentMember.Member_Caption
MEMBER measures.c2 AS
[Delivery Date].[Calendar].CurrentMember.Member_Caption
MEMBER measures.x AS
IIF
(
measures.c1 = measures.c2
,
[Measures].[Internet Order Count] + [Measures].[Reseller Order Count]
,null
)
SET q AS
Filter
(
[Sales Territory].[Sales Territory Country].[Sales Territory Country]*
[Date].[Calendar].[Month].&[2008]&[1]*
[Delivery Date].[Calendar].[Month].MEMBERS
,
measures.x > 100
)
SELECT
{measures.x} ON 0
//<<<<<Option A.try just this and sum the cells ....1128
,q
//<<<<<Option B.try just this and sum the cells ....1221
//,Extract
// (
// q
// ,[Date].[Calendar]
// ,[Delivery Date].[Calendar]
// )
ON 1
FROM [Adventure Works];
EDIT 2
This gives the correct answer - be mean of me not to reference the real author: https://social.msdn.microsoft.com/Forums/en-US/be750f75-0e39-41b8-9578-9dceb58a5865/strange-context-aware-behaviour-of-adventure-works-script?forum=sqlanalysisservices
WITH
MEMBER measures.c1 AS
[Date].[Calendar].CurrentMember.Member_Caption
MEMBER measures.c2 AS
[Delivery Date].[Calendar].CurrentMember.Member_Caption
MEMBER measures.[Calendar Month] AS
measures.c1
MEMBER measures.[Delivery Month] AS
measures.c2
MEMBER measures.x AS
IIF
(
measures.c1 = measures.c2
,
[Measures].[Internet Order Count] + [Measures].[Reseller Order Count]
,NULL
)
SELECT
{
measures.[Calendar Month]
,measures.[Delivery Month]
,measures.x
} ON COLUMNS
FROM [Adventure Works]
WHERE
{
NonEmpty
(
[Sales Territory].[Sales Territory].[Country].MEMBERS*
[Date].[Calendar].[Month].&[2008]&[1]*
[Delivery Date].[Calendar].[Month].MEMBERS
,measures.x
)
}
-
{
Filter
(
NonEmpty
(
[Sales Territory].[Sales Territory].[Country].MEMBERS*
[Date].[Calendar].[Month].&[2008]&[1]*
[Delivery Date].[Calendar].[Month].MEMBERS
,measures.x
)
,
measures.x < 100
)
};

You can filter with sales territory, by doing the following instead of your current from [Adventure Works]
from (
select {[Sales Territory].[Sales Territory Country].[Sales Territory Country]} on columns
from [Adventure Works]
)
Having said that, you can then remove the sales territory from here.. and just have Range1 * Range2
non empty(
[Sales Territory].[Sales Territory Country].[Sales Territory Country]
Range1
*Range2
)
You can then continue to filter, using the MDX Filter function
from (
select {[Sales Territory].[Sales Territory Country].[Sales Territory Country]} on columns
from
(
select {
FILTER (
[Date].[Calendar].MEMBERS
,[Date].[Calendar].currentmember.member_caption = [Delivery Date].[Calendar].currentmember.member_caption and A > 100
)
} on columns
[Adventure Works]
)
)

If you don't want to see [Sales Territory].[Sales Territory Country].[Sales Territory Country] in the final SELECT, you can use EXISTS clause in MDX
With
Set Range1 as {[Date].[Calendar].[Month].&[2008]&[1]
:[Date].[Calendar].[Month].&[2008]&[12]}
Set Range2 as
{[Delivery Date].[Calendar].[Month].&[2008]&[1]:
[Delivery Date].[Calendar].[Month].&[2008]&[12]}
MEMBER measures.A as [Measures].[Internet Order Count] + [Measures].[Reseller Order Count]
select {[Measures].[Internet Order Count],A} on columns,
non empty filter((
exists({Range1
*Range2}, [Sales Territory].[Sales Territory Country].[Sales Territory Country]
)), measures.A > 100)
having [Date].[Calendar].currentmember.member_caption
= [Delivery Date].[Calendar].currentmember.member_caption

Related

Request to retrieve a list of FullvisitorId - BigQuery

I want to get a list of FullvisitorId.
But I do not know how to select a list of FullvisitorId in my request ..
the goal is to retrieve a list of FullvisitorId in particular and not all sessions
standardSQL
SELECT
date,
visitStartTime,
TIMESTAMP_SECONDS(visitStartTime) as starttime_UTC,
DATETIME(TIMESTAMP_SECONDS(visitStartTime), "Europe/Amsterdam") as starttime_LOCAL,
totals.timeOnSite,
trafficSource.source,
trafficSource.referralPath,
hits.hour,
hits.minute,
hits.hitNumber,
fullVisitorId,
hits.isEntrance,
hits.isExit,
hits.referer,
hits.eventInfo.eventAction,
hits.latencyTracking.userTimingValue,
hits.type,
CONCAT(CAST(fullvisitorid AS string),CAST(visitid AS string)) AS case_id,
CONCAT(CAST( EXTRACT(YEAR FROM PARSE_DATE('%Y%m%d',date) ) AS string) ,"/",
CAST( EXTRACT(MONTH FROM PARSE_DATE('%Y%m%d',date))AS string) , "/" ,
CAST( EXTRACT(DAY FROM PARSE_DATE('%Y%m%d',date) ) AS string), " " ,
CAST(hits.hour AS string), ":" ,
CAST(hits.minute AS string), ":" ,
CAST(hits.hitNumber AS string)) AS dated,
(SELECT MAX( IF(index = 4,value,NULL) ) FROM UNNEST(hits.customDimensions)) type_page,
(SELECT MAX( IF(index = 6,value,NULL) ) FROM UNNEST(hits.customDimensions)) Univers
FROM `ga-orange-pro-etp.*****.ga_sessions_20190616*` , UNNEST(hits) as hits
WHERE (SELECT MAX( IF(index = 10,value,NULL) ) FROM UNNEST(hits.customDimensions)) = 'prod'
You can simply get the list of distinct fullVisitorId by running:
SELECT
DISTINCT fullVisitorId
FROM `ga-orange-pro-etp.*****.ga_sessions_20190616*` , UNNEST(hits) as hits
WHERE (SELECT MAX( IF(index = 10,value,NULL) ) FROM UNNEST(hits.customDimensions)) = 'prod'
If you remove DISTINCT it will return a value per single row so you will see duplicate fullVisitorId.

RANKING Function in DAX or MDX

How can i get result in attached image by DAX or MDX.Both can work for me.
( I will use DAX in Tabular Model and Will use MDX on SSRS part).
Sample Format
I tried below code but it didn't work.
WITH
SET OrderSet AS
Order
(
(
[Dim Product].[Category Name].[Category Name].MEMBERS
,[Dim Product].[Subcategory Name].[Subcategory Name].MEMBERS
,[Dim Product].[Sub Subcategory Name].[Sub Subcategory Name].MEMBERS
,[Dim Product].[Product Name].[Product Name].MEMBERS
)
,[Measures].[Order Quantity]
,BDESC
)
MEMBER [Measures].[RankOrderCount] AS
Rank
(
(
[Dim Product].[Category Name].CurrentMember
,[Dim Product].[Subcategory Name].CurrentMember
,[Dim Product].[Sub Subcategory Name].CurrentMember
,[Dim Product].[Product Name].CurrentMember
)
,OrderSet
)
SELECT
{
[Measures].[Order Quantity]
,[Measures].[RankOrderCount]
} ON 0
,NON EMPTY
{OrderSet} ON 1
FROM [Adventure Works DW2016CTP3];
I don't understand the problem. If I run this:
WITH
SET OrderSet AS
Order
(
(
[Product].[Category].[Category].MEMBERS
,[Product].[Subcategory].[Subcategory].MEMBERS
)
,[Measures].[Order Quantity]
,BDESC
)
MEMBER [Measures].[RankOrderCount] AS
Rank
(
(
[Product].[Category].CurrentMember
,[Product].[Subcategory].CurrentMember
)
,OrderSet
)
SELECT
{
[Measures].[Order Quantity]
,[Measures].[RankOrderCount]
} ON 0
,NON EMPTY
{OrderSet} ON 1
FROM [Adventure Works];
I get this:
This looks like your desired output format - but I did not change the script - so what is the question?
For example:
DEFINE
VAR TableTMP1 =
SELECTCOLUMNS (
DimProductTable,
"Product Name", DimProductTable[Product Name],
"Sub Subcategory Name", RELATED ( SubSubcategoryTable[Sub Subcategory Name] ),
"Subcategory Name", RELATED ( SubcategoryTable[Subcategory Name] ),
"Category Name", RELATED ( CategoryTable[Category Name] )
)
VAR TableTMP2 =
ADDCOLUMNS (
TableTMP1,
"Order Count", CALCULATE ( SUM ( OrderTable[Quantity] ) )
)
EVALUATE
ADDCOLUMNS ( TableTMP2, "Rank", RANKX ( TableTMP2, [Order Count], ASC ) )
ORDER BY [Rank] ASC

Multiple conditions on MDX query

I have the following MDX query:
WITH
member [Measures].[USD Sales YTD] as (
SUM( {[Invoice Date].[Calendar].[Month].&[201501]:[Invoice Date].[Calendar].[Month].&[201506]},[Measures].[USD Sales] ))
member [Measures].[USD Sales LY YTD] as (
SUM ( {[Invoice Date].[Calendar].[Month].&[201401]:[Invoice Date].[Calendar].[Month].&[201406]},[Measures].[USD Sales]))
member [Measures].[Unit Sales YTD] as (
SUM ( {[Invoice Date].[Calendar].[Month].&[201501]:[Invoice Date].[Calendar].[Month].&[201506]},[Measures].[Unit Sales]))
member [Measures].[Unit Sales LY YTD] as (
SUM ( {[Invoice Date].[Calendar].[Month].&[201401]:[Invoice Date].[Calendar].[Month].&[201406]},[Measures].[Unit Sales]))
SELECT
NON EMPTY { [Measures].[USD Sales YTD]
, [Measures].[USD Sales LY YTD]
, [Measures].[Unit Sales YTD]
, [Measures].[Unit Sales LY YTD]
} ON COLUMNS
, NON EMPTY {
(
[Invoice Date].[Calendar].[Month]
* [Customer].[Customer Name].[Customer Name]
* [Part].[Part #].[Part #]
* [Sales Rep].[Sales Reps].[Sales Rep]
) } DIMENSION PROPERTIES [Part].[Part #].[Part Desc], MEMBER_CAPTION, MEMBER_UNIQUE_NAME ON ROWS
FROM ( SELECT ( [Sales Rep].[Sales Reps].[Sales Rep].&[12]) ON COLUMNS
FROM ( SELECT ( [Invoice Date].[Calendar].[Month].&[201506]) ON COLUMNS
FROM [AlphaBI])
)
WHERE ( {[Part].[Sales Unit].&[B], [Part].[Sales Unit].&[M]})
It works fine. It's giving me the month, customer, product, salesman and Sales YTD, Sales LYTD, Units YTD and Units LYDT.
Recently, a user asked me to change the WHERE condition to say if it's Sales than use sales unit B & M (like now), BUT if it's Units, then just B. Do you know how I can do that? In SQL it would be pretty easy, but my MDX skills are a bit rusty. Any help would be appreciated.
Thanks,
Alex
Just add a small condition in your WHERE clause, just like in SQL using IIF
WHERE (
{
[Part].[Sales Unit].&[B],
IIF(#param = "sales", [Part].[Sales Unit].&[M], null)
}
)
This will select the members [Part].[Sales Unit].&[B] and [Part].[Sales Unit].&[M] if the parameter selected is "sales", otherwise selects only [Part].[Sales Unit].&[B]
If you want to go a step even further and add even one more level of check, like if "none" is selected, it should add Sales Unit "A", then use nested IIF statements like so -
IIF(#param = "sales", [Part].[Sales Unit].&[M], IIF(#param = "units", null, [Part].[Sales Unit].&[A])
Assuming no additional checks are required, the code should look like -
WITH
MEMBER [Measures].[USD Sales YTD] AS
SUM(
{
[Invoice Date].[Calendar].[Month].&[201501]
:
[Invoice Date].[Calendar].[Month].&[201506]
},
[Measures].[USD Sales]
)
MEMBER [Measures].[USD Sales LY YTD] AS
SUM (
{
[Invoice Date].[Calendar].[Month].&[201401]
:
[Invoice Date].[Calendar].[Month].&[201406]
},
[Measures].[USD Sales]
)
MEMBER [Measures].[Unit Sales YTD] AS
SUM (
{
[Invoice Date].[Calendar].[Month].&[201501]
:
[Invoice Date].[Calendar].[Month].&[201506]
},
[Measures].[Unit Sales]
)
MEMBER [Measures].[Unit Sales LY YTD] AS
SUM (
{
[Invoice Date].[Calendar].[Month].&[201401]
:
[Invoice Date].[Calendar].[Month].&[201406]
},
[Measures].[Unit Sales]
)
SELECT
NON EMPTY
{
[Measures].[USD Sales YTD],
[Measures].[USD Sales LY YTD],
[Measures].[Unit Sales YTD],
[Measures].[Unit Sales LY YTD]
} ON COLUMNS,
NON EMPTY
{
(
[Invoice Date].[Calendar].[Month] *
[Customer].[Customer Name].[Customer Name] *
[Part].[Part #].[Part #] *
[Sales Rep].[Sales Reps].[Sales Rep]
)
} DIMENSION PROPERTIES [Part].[Part #].[Part Desc], member_caption,
member_unique_name ON ROWS
FROM ( SELECT
(
[Sales Rep].[Sales Reps].[Sales Rep].&[12]
) ON COLUMNS
FROM ( SELECT
(
[Invoice Date].[Calendar].[Month].&[201506]
) ON COLUMNS
FROM [AlphaBI]) )
WHERE (
{
[Part].[Sales Unit].&[B],
IIF(#param = "sales", [Part].[Sales Unit].&[M], null)
}
)
EDIT 2
Does this help? I have moved the association with Sales Unit attribute to the definition of member instead of WHERE clause.
WITH
MEMBER [Measures].[USD Sales YTD] AS
SUM(
{
[Invoice Date].[Calendar].[Month].&[201501]
:
[Invoice Date].[Calendar].[Month].&[201506]
}
*
{
[Part].[Sales Unit].&[B],
[Part].[Sales Unit].&[M]
}
,
[Measures].[USD Sales]
)
MEMBER [Measures].[USD Sales LY YTD] AS
SUM (
{
[Invoice Date].[Calendar].[Month].&[201401]
:
[Invoice Date].[Calendar].[Month].&[201406]
}
*
{
[Part].[Sales Unit].&[B],
[Part].[Sales Unit].&[M]
}
,
[Measures].[USD Sales]
)
MEMBER [Measures].[Unit Sales YTD] AS
SUM (
{
[Invoice Date].[Calendar].[Month].&[201501]
:
[Invoice Date].[Calendar].[Month].&[201506]
}
*
{
[Part].[Sales Unit].&[B]
}
,
[Measures].[Unit Sales]
)
MEMBER [Measures].[Unit Sales LY YTD] AS
SUM (
{
[Invoice Date].[Calendar].[Month].&[201401]
:
[Invoice Date].[Calendar].[Month].&[201406]
}
*
{
[Part].[Sales Unit].&[B]
}
,
[Measures].[Unit Sales]
)
SELECT
NON EMPTY
{
[Measures].[USD Sales YTD],
[Measures].[USD Sales LY YTD],
[Measures].[Unit Sales YTD],
[Measures].[Unit Sales LY YTD]
} ON COLUMNS,
NON EMPTY
{
(
[Invoice Date].[Calendar].[Month] *
[Customer].[Customer Name].[Customer Name] *
[Part].[Part #].[Part #] *
[Sales Rep].[Sales Reps].[Sales Rep]
)
} DIMENSION PROPERTIES [Part].[Part #].[Part Desc], member_caption,
member_unique_name ON ROWS
FROM ( select {[Part].[Sales Unit].&[B], [Part].[Sales Unit].&[M]} FROM (
SELECT
(
[Sales Rep].[Sales Reps].[Sales Rep].&[12]
) ON COLUMNS
FROM ( SELECT
(
[Invoice Date].[Calendar].[Month].&[201506]
) ON COLUMNS
FROM [AlphaBI]) ) )
(I don't feed our mdx into RS so guessing a bit)
Can you add in a parameter into your report (I've called it #switch) which dynamically creates a set at the top of your script- this is then referenced in the WHERE clause:
WITH
set [whereSet] AS
strToSet(
iif(#switch = "Both",
"{[Part].[Sales Unit].&[B], [Part].[Sales Unit].&[M]}"
,"{[Part].[Sales Unit].&[B]}"
)
)
member [Measures].[USD Sales YTD] as (
SUM( {[Invoice Date].[Calendar].[Month].&[201501]:[Invoice Date].[Calendar].[Month].&[201506]},[Measures].[USD Sales] ))
member [Measures].[USD Sales LY YTD] as (
SUM ( {[Invoice Date].[Calendar].[Month].&[201401]:[Invoice Date].[Calendar].[Month].&[201406]},[Measures].[USD Sales]))
member [Measures].[Unit Sales YTD] as (
SUM ( {[Invoice Date].[Calendar].[Month].&[201501]:[Invoice Date].[Calendar].[Month].&[201506]},[Measures].[Unit Sales]))
member [Measures].[Unit Sales LY YTD] as (
SUM ( {[Invoice Date].[Calendar].[Month].&[201401]:[Invoice Date].[Calendar].[Month].&[201406]},[Measures].[Unit Sales]))
SELECT
...
...
WHERE [whereSet]

Error adding new column

I'm trying to add an "account subcategory" column to my report using the query builder.
I added the column and got the error message:
"An MDX expression was expected. An empty expression was specified."
How do I format the MDX in order to get the result I am looking for?
I posted the code below. Hope this helps.
SELECT NON EMPTY { [Measures].[Order Partic Adj], [Measures].[PY Net Cash], [Measures].[Order Merch Adj], [Measures].[OCCURS - APY BOOKING FACT], [Measures].[PY Orders], [Measures].[Net Cash Merch Adj], [Measures].[Net Cash Acq Adj], [Measures].[Total Proj Orders], [Measures].[Order Acq Adj], [Measures].[Net Cash Partic Adj], [Measures].[Total Proj Net Cash] } ON COLUMNS, NON EMPTY { ([Current Assignment].[Territory Code].[Territory Code].
ALLMEMBERS * [Current Assignment].[Sales Rep Name Caps].[Sales Rep Name Caps].
ALLMEMBERS * [Current Assignment].[Sales Rep Employee Code].[Sales Rep Employee Code].
ALLMEMBERS * [Account].[Account Subcategory Name].[Account Subcategory Name].
ALLMEMBERS * [Account].[ACCOUNT NAME].[ACCOUNT NAME].
ALLMEMBERS * [Account].[LIFETOUCH ID].[LIFETOUCH ID].
ALLMEMBERS * [Account].[NACAM Ordered].[NACAM Ordered].
ALLMEMBERS * [Account].[ENROLLMENT COUNT].[ENROLLMENT COUNT].
ALLMEMBERS * [Account].[STATE].[STATE].
ALLMEMBERS * [Account].[District Name].[District Name].
ALLMEMBERS * [Account].[City].[City].
ALLMEMBERS * [Account].[COUNTY].[COUNTY].
ALLMEMBERS * [APY].[BOOKING STATUS].[BOOKING STATUS].
ALLMEMBERS * [APY].[X BOOKING STATUS ID].[X BOOKING STATUS ID].
ALLMEMBERS * [APY].[AT RISK IND].[AT RISK IND].
ALLMEMBERS * [APY].[INCENTIVE ACQUISITION IND].[INCENTIVE ACQUISITION IND].
ALLMEMBERS * [APY].[CY BOOKING CAMERA TYPE NAME].[CY BOOKING CAMERA TYPE NAME].
ALLMEMBERS * [APY].[CURRENT YEAR SELLING METHODS].[CURRENT YEAR SELLING METHODS].
ALLMEMBERS * [APY].[PY ACTUAL CAMERA TYPE NAME].[PY ACTUAL CAMERA TYPE NAME].
ALLMEMBERS * [APY].[ACQUIRED BUSINESS NAME].[ACQUIRED BUSINESS NAME].
ALLMEMBERS * [APY].[PRIOR YEAR SELLING METHODS].[PRIOR YEAR SELLING METHODS].
ALLMEMBERS * [Marketing].[SUB PROGRAM NAME].[SUB PROGRAM NAME].
ALLMEMBERS * [Current Assignment].[AMS Business Unit Name].[AMS Business Unit Name].
ALLMEMBERS * [Account].[POSTAL CODE].[POSTAL CODE].
ALLMEMBERS ) } DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME ON ROWS FROM ( SELECT ( STRTOSET(#APYBOOKINGSTATUS, CONSTRAINED) ) ON COLUMNS FROM ( SELECT ( STRTOSET(#CurrentAssignmentSalesRepNameCaps, CONSTRAINED) ) ON COLUMNS FROM ( SELECT ( STRTOSET(#CurrentAssignmentTerritoryCode, CONSTRAINED) ) ON COLUMNS FROM ( SELECT ( STRTOSET(#CurrentAssignmentRegionName, CONSTRAINED) ) ON COLUMNS FROM ( SELECT ( STRTOSET(#CurrentAssignmentAreaName, CONSTRAINED) ) ON COLUMNS FROM ( SELECT ( STRTOSET(#CurrentAssignmentAMSBusinessUnitName, CONSTRAINED) ) ON COLUMNS FROM ( SELECT ( STRTOSET(#CurrentAssignmentCountryName, CONSTRAINED) ) ON COLUMNS FROM ( SELECT ( STRTOSET(#SchoolYearSCHOOLYEAR, CONSTRAINED) ) ON COLUMNS FROM ( SELECT ( STRTOSET(#MarketingSUBPROGRAMNAME, CONSTRAINED) ) ON COLUMNS FROM [BO_APY_Booking_Fact_Full]))))))))) WHERE ( IIF( STRTOSET(#SchoolYearSCHOOLYEAR, CONSTRAINED).Count = 1, STRTOSET(#SchoolYearSCHOOLYEAR, CONSTRAINED), [School Year].[SCHOOL YEAR].currentmember ), IIF( STRTOSET(#CurrentAssignmentCountryName, CONSTRAINED).Count = 1, STRTOSET(#CurrentAssignmentCountryName, CONSTRAINED), [Current Assignment].[Country Name].currentmember ), IIF( STRTOSET(#CurrentAssignmentAreaName, CONSTRAINED).Count = 1, STRTOSET(#CurrentAssignmentAreaName, CONSTRAINED), [Current Assignment].[Area Name].currentmember ), IIF( STRTOSET(#CurrentAssignmentRegionName, CONSTRAINED).Count = 1, STRTOSET(#CurrentAssignmentRegionName, CONSTRAINED), [Current Assignment].[Region Name].currentmember ) ) CELL PROPERTIES VALUE, BACK_COLOR, FORE_COLOR, FORMATTED_VALUE, FORMAT_STRING, FONT_NAME, FONT_SIZE, FONT_FLAGS

SQL Sum cumulative and non-cumulative in same query

Hi I was wondering if there is a way to get a cumulative and non-cumulative total in the same query. I have a table with following fields:
Department, SalesPerson, fin_month, activity, cost
What I would like is have two sums, one that would give a monthly total for salesperson, and another giving a year to date total. I am having a problem setting two different where criteria to get it to work.
Many Thanks
Would something like this help?
SELECT
*
FROM
(
SELECT
Department, SalesPerson
, SUM(fin_month) SalesPerson_Sum
FROM
[TABLE_NAME]
GROUP BY Department, SalesPerson
) a
INNER JOIN
(
SELECT
Department
, SUM(fin_month) AS Department_Sum
FROM
[TABLE_NAME]
GROuP BY
Department
) b
ON
a.Department = b.Department
This solution uses CTEs, recursion, and ranking to obtain cumulative totals for every fin_month per SalesPerson in every Department based on the corresponding monthly totals.
;WITH
monthlytotals AS (
SELECT
Department,
SalesPerson,
fin_month,
MonthlyTotal = SUM(cost),
rn = ROW_NUMBER() OVER (PARTITION BY Department, SalesPerson
ORDER BY fin_month)
FROM atable
GROUP BY Department, SalesPerson, fin_month
),
alltotals AS (
SELECT
Department,
SalesPerson,
fin_month,
MonthlyTotal,
CumulativeTotal = MonthlyTotal,
rn
FROM monthlytotals
WHERE rn = 1
UNION ALL
SELECT
m.Department,
m.SalesPerson,
m.fin_month,
m.MonthlyTotal,
CumulativeTotal = a.CumulativeTotals + m.MonthlyTotal,
m.rn
FROM monthlytotals m
INNER JOIN alltotals a
ON m.Department = a.Department
AND m.SalesPerson = a.SalesPerson
AND m.rn = a.rn + 1
)
SELECT
Department,
SalesPerson,
fin_month,
MonthlyTotal,
CumulativeTotal
FROM alltotals