MDX - comma-separated column to summarize values across time dimension (rows) - csv

The below MDX query
WITH
MEMBER [Measures].[GM%] AS
IIF
(
[Measures].[Revenue] > 2
,
([Measures].[Revenue] - [Measures].[Cost]) / [Measures].[Revenue] * 100
,NULL
)
SELECT
CrossJoin
(
LastPeriods
(5
,[Date.YQM].[2018].LastChild
)
,[Measures].[GM%]
) ON COLUMNS
,NON EMPTY
{Order([SP].Children,[Measures].[Revenue],DESC)} ON ROWS
FROM [GM_SP];
Gives me the following tabular output:
Table output
Question: How can I add a column to the end that summarizes the values across the 5 Quarters?
I need this output comma-separated as it is used for rendering a sparkline within the table. (Pentaho CDE used to render).
Example Column 7
row 1: 100,,,,
row 2: 97.82535574475716, 95.83261932552514,
96.51941349723123, 96.77026598200818, 97.85452666266234

Related

How to get the first record of each type in sequence?

Table Data:
ID
Type
1
A
2
A
3
B
4
A
5
A
6
B
7
B
8
A
9
A
10
A
How to get only rows with IDs 1,3,4,6,8, or the first records on type-change by single query?
We were doing this in code using multiple queries and extensive processing especially for large data, is there a way to do this in a single query?
Use LAG() window function to get for every row the previous row's type and compare it to the current type.
Create a flag column that is true if the 2 types are different and use it to filter the table:
WITH cte AS (
SELECT *, type <> LAG(type, 1, '') OVER (ORDER BY id) flag
FROM tablename
)
SELECT * FROM cte WHERE flag;
I assume that the column type does not contain empty values (nulls or
empty strings).
See the demo.

How to properly format overlapping mySQL IN and NOT IN conditions

I have the following mySQL table:
data
1
2
3
4
5
6
7
8
9
I would like to supply my select statement with two seperate lists
Exculde List:
1,4,5,7
Include List:
1,2,3,4,5,6,7
I tried the following statement:
Select * FROM table WHERE data NOT IN ('1,4,5,7') AND data IN ('1,2,3,4,5,6,7)
Expecting the following output:
data
2
3
6
But I received no results. I realize I passed an impossible condition but I don't know how to format my query to return the expected results.
Can anyone tell me what I'm doing wrong here?
IN takes a list of values, not a string that holds a delimited list of values.
Examples:
x IN (1, 2, 3)
x IN ('a', 'b', 'c')
Use IN (1,2,3) and not IN ('1,2,3') as the former compares to individual values 1, 2 and 3 while the latter is against the literal string 1,2,3.
Select * FROM ( (Select * FROM table WHERE data NOT IN ('1,4,5,7') ) AS table WHERE data IN ('1,2,3,4,5,6,7)
you try againt

How to find value from comma-separated column

I am wondering how to write a SQL statement for SQL Server 2008 that selects entries where a column contains a comma-delimited value (usually - there could only be one entry (and no leading comma)) for instance:
Column1
---------------------------
10-15,20-30,31-97,104-187
Values in the column represents comma delimited ranges.
In this case I want to find 25.
Please consider normalizing your database. You are adding insult to injury by using a single column to keep multiple ranges data. A normalized database would have another table for the ranges, with a start value and an end value, along with a foreign key to the original table.
Something like this:
CREATE TABLE tblRange
(
Range_itemId int, --(fk to the original table)
Range_From int,
Range_To int
)
Should you do this, your query would be simply:
SELECT i.*
FROM tblItems
INNER JOIN tblRange ON(Item_Id = Range_ItemId)
WHERE Range_From <= 25
AND Range_To >= 25
However, if you can't normalize your database then you would have to use a split string function to create rows from your comma delimited column, and then parse the text of each row to find what is the range of each row.
Here is an example:
create demo table
CREATE TABLE ZZZItems
(
ItemId int identity(1,1),
ItemRanges varchar(500)
)
populate demo table
INSERT INTO ZZZItems VALUES
('10 - 20, 20 - 30, 30 - 40'),
('40 - 50, 50 - 60, 60 - 70'),
('70 - 80, 80 - 90, 90 - 100'),
('10 - 20, 20 - 30, 30 - 40')
using CROSS APPLY to a split function table and extracting the ranges from the splitted data
;WITH CTE AS
(
SELECT ItemId,
CAST(LEFT(Data, CHARINDEX('-', Data) - 1) As Int) As RangeFrom,
CAST(RIGHT(Data, LEN(Data) - CHARINDEX('-', Data)) As Int) As RangeTo
FROM ZZZItems
CROSS APPLY dbo.Split(ItemRanges, ',')
)
-- select the item ids where the requested number fits in the range.
SELECT ItemId
FROM CTE
WHERE RangeFrom < 25
AND RangeTo > 25
clean up
DROP TABLE ZZZItems
Results:
ItemId
-----------
1
4
I didn't add the split function, you should choose your own from the article I've linked to.

SSRS mdx report: calculated member with maximum rank value for passes multivalue parameter

Here is a simple mdx query to MS OLAP cube, which outputs sale step stats for 3 cities with ranking of each sale stage, it works fine:
WITH
MEMBER [Measures].[rank] AS
case [Sales_step].currentmember.member_caption
when 'Contacts' then 1
when 'Clients' then 2
when 'Funded' then 3
else 0 end
SELECT {[Measures].[rank],
[Measures].[qnt]} ON COLUMNS,
NON EMPTY
crossjoin({[City].CHILDREN},
{[Sales_step].CHILDREN}) ON ROWS
FROM ( SELECT ( STRTOSET(#[Sales_step], CONSTRAINED) ) ON COLUMNS
FROM [SALES_PIPE])
The output is:
Now I want to build totals for each city without separate sale steps, but showing maximum archived sales stage rank only. The result must be:
I tried the following code to do that:
WITH
MEMBER [Measures].[rank max] AS
case [Sales_step].currentmember.member_caption
when 'Contacts' then 1
when 'Clients' then 2
when 'Funded' then 3
else 0 end
SELECT {[Measures].[rank max],
[Measures].[qnt]} ON COLUMNS,
NON EMPTY [City].CHILDREN ON ROWS
FROM ( SELECT ( STRTOSET(#[Sales_step], CONSTRAINED) ) ON COLUMNS
FROM [SALES_PIPE])
It does not generate error, but returns null values for calculated member [Measures].[rank max]:
It works only when I pass one value to #[Sales_step] parameter. While I need a multivalued param run. When I changed this snippet in case clause:
case [Sales_step].currentmember.member_caption
to:
case strtomember(#[Sales_step]).member_caption
it throwed an error "The STRTOMEMBER function expects a member expression for the 1 argument. A tuple set expression was used". Errors fire both for one- and multy-param when I use this too:
case strtoset(#[Sales_step]).currentmember.member_caption
How do I need to modify calculated member [Measures].[rank max] to get desired result with maximum rank for passed #[Sales_step] multivalue param?
I wonder if something like this works:
WITH
SET [S] AS
NonEmpty(
EXISTING [Sales_step].CHILDREN,
([City].CURRENTMEMBER, [Measures].[qnt]) //<<I think that [City].CURRENTMEMBER is probably redundant in this tuple
)
MEMBER [Mx] AS
CASE
WHEN INTERSECT([S], {[Sales_step].[Funded]}).COUNT = 1 THEN 3
WHEN INTERSECT([S], {[Sales_step].[Clients]}).COUNT = 1 THEN 2
WHEN INTERSECT([S], {[Sales_step].[Contacts]}).COUNT = 1 THEN 1
END
...
...

WHERE clause in SSRS expression for max function

I have for example a query with return something as it
route value
1 3
2 2
3 4
4 5
5 1
then I need to put in 2 textbox the max and the min route so in sql this would be
select top 1 route from table where value=(select max(value) from table)
I add a image done in excel, how this would be.
I believe this is so easy but I dont have idea how to get it.
I got using expression, this was extactly expression
="Route "+
Convert.ToString (
Lookup(max(fields!value.Value),fields!value.Value ,fields!route.Value,"mydataset")
)
changing max for min, for the other...
thanks everyone.
I believe the query you're looking for would be:
With Min_Max_CTE as (
Select MIN(value) as Min_Value
, MAX(value) as Max_Value
From Table
)
Select Top 1 'Min' as Type
, T.route
, T.value
From Table T
Inner Join Min_Max_CTE CTE
on T.value = CTE.Min_Value
Union All
Select Top 1 'Max' as Type
, T.route
, T.value
From Table T
Inner Join Min_Max_CTE CTE
on T.value = CTE.Max_Value
Order by Type desc --This will put the Min Route first followed by the Max Route
Then, put that query into a dataset, and then create a tablix and use the Type, route, and value fields to return the minimum route and the maximum route. It should end up being set up just like your excel section with the min and max routes above.
You can do this SSRS by using a couple of separate tables. Your example data:
And two tables in the Designer:
Since the tables only have header rows, only the first row in the table will be displayed.
To make sure we get the MAX and MIN values in the two tables, each table needs to order its Dataset appropriately, i.e. by Value by descending and ascending respectively.
MAX table:
MIN table:
Which gives your expected result: