How to correctly use InStr with ActivePivot? - mdx

I am trying to use the INSTR() function with ActivePivot. I am testing by using an Instr call that should always return > 0.
Here is my initial MDX query that works fine:
SELECT
{
{[CODE].[ALL].[AllMember]}
} ON ROWS
FROM [Cube]
WHERE ([Measures].[contributors.COUNT])
Here is my test InStr query:
SELECT
NON EMPTY
Generate
(
[CODE].[ALL].[AllMember]
,(
Instr
(
"Test"
,"es"
) > 0
)
)ON ROWS
FROM [Cube]
WHERE ([Measures].[contributors.COUNT])
Please can you help me create a working example for the Instr MDX query in ActivePivot?
Many thanks
Edit: What I wanted to do
SELECT
NON EMPTY Hierarchize({[CODE].[CODE].Members}) ON ROWS,
NON EMPTY Hierarchize({Filter([RELEVANCE].Members, InStr([RELEVANCE].CurrentMember.Name, "n/a") > 0)}) ON COLUMNS
FROM [Cube]
WHERE ([Measures].[contributors.COUNT])

I'm not sure about what you try to achieve with your MDX but here is an example which may be helpful:
Before, I consider all places with a positive contributors.COUNT:
SELECT
NON EMPTY Hierarchize({Filter([PlaceDimension].[Continent].Members, [Measures].[contributors.COUNT] > 0)}) ON ROWS
FROM [TwitterCube]
WHERE ([Measures].[contributors.COUNT])
After, I keep only places with an "a" in their name:
SELECT
NON EMPTY Hierarchize({Filter([PlaceDimension].[Continent].Members, InStr([PlaceDimension].CurrentMember.Name, "a") > 0)}) ON ROWS
FROM [TwitterCube]
WHERE ([Measures].[contributors.COUNT])

Related

Count all elements in an array

I have a table that I save some data include list of numbers.
like this:
numbers
(null)
،42593
،42593،42594،36725،42592،36725،42592
،42593،42594،36725،42592
،31046،36725،42592
I would like to count the number elements in every row in SQL Server
count
0
1
6
4
3
You could use a replacement trick here:
SELECT numbers,
COALESCE(LEN(numbers) - LEN(REPLACE(numbers, ',', '')), 0) AS num_elements
FROM yourTable;
The above trick works by counting the number of commas (assuming your data really has commas as separators). For example, your last sample data point was:
,31046,36725,42592 => length is 18
310463672542592 => length is 15
Hence the difference in lengths correctly yields the right number of elements.
Another idea is to useSTRING_SPLIT:
SELECT y.numbers,
(SELECT COUNT(Value) - 1
FROM string_split(COALESCE(y.numbers,''),',')) AS num_elements
FROM yourtable AS y;
I know this looks a bit unhandy on first glance due to this strange -1 in the second line and the COALESCE in the third line. So why do I talk about this option?
Well, the strange thing in your case which causes these difficulties in my query is that your rows always start with a comma.
This is quite weird and it would be much easier without this first comma in every row.
Let's assume you remove this comma in future. Then this will become really easy and good readable:
SELECT y.numbers,
(SELECT COUNT(Value)
FROM string_split(y.numbers,',')) AS num_elements
FROM yourtable AS y;
Try out: db<>fiddle
your data
CREATE TABLE yourtable(
numbers VARCHAR(max)
);
INSERT INTO yourtable
(numbers) VALUES
(null),
('،42593'),
('،42593،42594،36725،42592،36725،42592'),
('،42593،42594،36725،42592'),
('،31046،36725،42592');
you need ISNULL and len
select
ISNULL(len(numbers) - len(replace(numbers,'،','')) ,0) count
from yourtable
the other way is by using IIF and string_split as follows
SELECT IIF(count < 0, 0, count) count
FROM   (SELECT (SELECT Count(*) - 1
                FROM   STRING_SPLIT (Replace(Replace(numbers, 'R', ''), '،',
                                     'R'), 'R'
                       )) AS
               'count'
        FROM   yourtable) A
dbfiddle

Why SUBSTRING_INDEX in Hive is not working for negative count?

When I execute the following two queries, the SUBSTRING_INDEX function with positive count gives me correct result but the SUBSTRING_INDEX function with negative count gives me wrong output result.
SELECT SUBSTRING_INDEX('wwwbig.data.nsqlcom',"data", 1)
Output: wwwbig.
SELECT SUBSTRING_INDEX("wwwbig.data.nsqlcom",'data', -1)
Output: ata.nsqlcom
As per the function definition, the second query should return ".nsqlcom" value. Note: This issue is only seen in the case of Hive and not any other tool.
its working perfectly.
definition is -
substring_index(STRING a, STRING delim, INT count)
Returns the substring from string A before count occurrences of the delimiter delim (as of Hive 1.3.0).
If count is positive, everything to the left of the final delimiter (counting from the left) is returned.
If count is negative, everything to the right of the final delimiter (counting from the right) is returned.
In your second case, when count is -1 and delimiter is 'data', function should return everything to the right of delimiter based on final occurrence position of delimiter.
As per your data "wwwbig.data.nsqlcom", final occurrence of data is at 7. So, everything from 8th position will be returned which is ata.nsqlcom.

Why BigQuery produces an empty array when ARRAY_CONCAT is applied on a non empty array in conjunction with ARRAY_AGG?

I encountered a very curious behavior in BigQuery.
The following query produces the unexpected result empty array [], since I'm concatenating arrays I'm expecting an array with all the elements that I'm concatenating.
SELECT ARRAY_CONCAT(
(
SELECT ARRAY_AGG(col1) -- This is creating an empty array since condition is always false
FROM ...
WHERE 1 = 2
),
['test']
)
While a very similar query produces the expected result ['test']:
SELECT ARRAY_CONCAT(
[],
['test']
)
And ARRAY_AGG() produces indeed an empty array []:
SELECT ARRAY_AGG(col1) -- This is creating an empty array since condition is always false
FROM ...
WHERE 1 = 2
Why the first query produces an empty array while it is a combination of the second and third query?
This is a little tricky to explain. Your first query does not produce an empty array. It produce an array column whose value is NULL. That is why the concat returns NULL.
As a general rule, almost aggregation function in a subquery with no rows returns NULL. The only exception is COUNT() which returns 0.
It is tempting to think that NULL and an empty array are the same thing. They are not.

Mdx, greater than works like String and not Numeric (Saiku)

I am using Saiku and trying to filter by mdx, using the symbol '> (greater than)', in the default Sales cube. The problem is that's it filtering like String and not Numeric. The values that I want for the query below is [51,52], but the server olap response is [6,7,8,9,51,52]. Any idea how can I filter that?
Here's the query:
WITH
SET [~ROWS] AS
{
FILTER([Time].[Weekly].[Week].Members, [Time].[Weekly]. [Week].CurrentMember.Properties("Caption") > '50')
}
SELECT
NON EMPTY {[Measures].[Unit Sales]} ON COLUMNS,
NON EMPTY [~ROWS] ON ROWS
FROM [Sales]
The response is to use 'Cint', like bellow:
WITH
SET [~ROWS] AS
{
FILTER([Time].[Weekly].[Week].Members, Cint([Time].[Weekly].[Week].CurrentMember.Properties("Caption")) > 50)
}
SELECT
NON EMPTY {[Measures].[Unit Sales]} ON COLUMNS,
NON EMPTY [~ROWS] ON ROWS
FROM [Sales]
I think there may be alternative approaches. With experimenting as some may be more efficient.
WITH MEMBER Measures.ValueColumn as [Date].[Calendar].[July 1, 2001].MemberValue
MEMBER Measures.KeyColumn as [Date].[Calendar].[July 1, 2001].Member_Key
MEMBER Measures.NameColumn as [Date].[Calendar].[July 1, 2001].Member_Name
SELECT {Measures.ValueColumn, Measures.KeyColumn, Measures.NameColumn} ON 0
from [Adventure Works]

MS Access Expression to Find Records Where the Value to the Left of a Comma is Greater than Zero

I have some records in a column that contain IDs and some of these records contain multiple IDs separated by commas. Additionally there are some records where I have ",3" and ",2" when they should simply be "3" and "2". I do not have write privileges in this DB so updating those records is not an option.
I am trying to write a query that returns records that have a comma where the value to the left of any comma in the record is greater than 0 e.g. "2,3", "2,3,12" etc but NOT ",3" or ",2".
What would this expression look like in MS Access?
Thanks in advance.
If you want to remove the starting comma from the records when you return them, you can do so using a simple query:
SELECT IIF(MyField LIKE ",*", Right(MyField, Len(MyField)-1), MyField)
FROM MyTable
To answer your original question, you could simply use Val:
SELECT * FROM YourTable WHERE Val([YourField]) > 0
I would simply use:
select t.*
from t
where val not like ",*";
This doesn't handle the 0 part, but you don't give any examples in your answer. Perhaps this answers that part:
select t.*
from t
where val not like ",*" and val not like "*0,*";