SSAS comparing previous rows data and get the count - ssas

I have following Scenario in cube
ID
PreID
Code
Valid
1
-1
A
0
2
1
B
0
3
1
B
1
3
2
B
1
Id compared with PreID.
First Row ID 1 does not have Preid and Code is A so valid is 0. This is correct
Second Row ID 2 and Preid is 1 , code is B for ID 2 but code is A for ID 1 so valid is 0. This is correct
Third Row Id is 3 and Preid is 1 , code is B for for id 3 but Code is A for Id 1 so valid should be 0 but displaying 1 which is incorrect
Fourth Row Id is 4 and Preid is 1, code is B for for id 3 and Code is B for Id 2 so valid is 1 which is correct.
Trying to get valid 0 for third row.
All of them are dimensions and valid is measure.
Is it possible to compare the Code column of Key and pre key without adding new dimension ? currently it is looking at the Code column of only Key and not prekey and setting the valid 0 or 1.
I have tried previousmember but as shown in example there will not be consistent rows of previousmemeber

Related

Create a table with unknown columns SQL

I have a table that looks like this
ID
Steps
Letters
1
1
a
1
2
e
1
3
b
2
1
c
2
2
d
3
1
b
3
2
a
And a query that consists of the output
a
b
d
My goal is to create a table/ modify the first one to get rid of the letter column, and instead, have N additional columns (where N is the number of rows in the second query above) and the output is 1 if the last step for that ID was that specific letter, 0 if that letter was in any step, and NULL if it never was. Making a table like this
ID
a
b
d
1
0
1
NULL
2
NULL
NULL
1
3
1
0
NULL
I assume pivoting makes sense as a way to approach it, but I don't even know where to begin

SQL get count of value

I have the following table. Now I want to count the amount of each value in this table.
value
count
1
1
-1
1
2
1
3
1
-1 and 1 should be seen as 1, so the output should be
value
count
1
2
2
1
3
1
Does someone know a quick fix?
Grouping by value, and making such values the absolute value so you ignore the negative sign:
SELECT
ABS(VALUE) VALUE, COUNT() COUNT
FROM
table
GROUP BY
ABS(VALUE)

In Flink, how to convert the cumulative value into an incremental value in flink and then aggregate by some keys

How to convert the cumulative value into an incremental value in flink (some keys are considered to be a user, and then the cumulative value becomes the incremental value of two adjacent ones), and then on the basis of the incremental value (time Dimension, a key) for aggregation (sum)
For example, origin data is:
time A B value
0 1 1 1
0 2 2 2
0 1 1 4
0 2 2 3
1 1 1 5
1 2 2 6
After convert to incremental value, we got
time A B value
0 1 1 1
0 2 2 2
0 1 1 3
0 2 2 1
1 1 1 2
1 2 2 3
Then we aggregate by (time, A), got final result is
time A value
0 1 4
0 2 3
1 1 2
1 2 3
Is there a program that can do these two things at once?
One solution is to use session window or global window to convert the original table into an incremental table and store it in another place, and start another task to aggregate the results? But this will consume additional storage.
Sorry for my poor english and thanks for your advice.
There's no need to have two separate applications, or to store anything. Just let the output of the first step flow into the second step. Conceptually that's
results = input
.somehowDoTheIncrementalPart()
.thenAggregate();
or in SQL you could use a nested query, something like
SELECT ts, sum(diff) FROM (
SELECT ts, userId, diff
FROM events
MATCH_RECOGNIZE (
PARTITION BY id
ORDER BY ts
MEASURES
p2.v - p1.v AS diff, p2.id AS userId, p2.ts AS ts
AFTER MATCH SKIP TO LAST p2
PATTERN (p1 p2)
DEFINE p1 AS TRUE, p2 AS TRUE )
) GROUP BY ts, userId

Adding Auto Increment Value to Column in relation to Duplicate values in Another Column

I have a large table (3 million rows and about 12 columns). I have one column that can contain duplicate values - this is my "ID" column. I have a second column "NUM_ID" that I would like to have it start at the value of 1 for every unique "ID". Then - if I run into a duplicate value - "NUM_ID" would then bump up one value (to 2) and so on. For example:
ID NUM_ID
1 1
2 1
2 2
2 3
3 1
3 2
4 1
5 1
5 2
5 3
5 4
Again, "ID" is pre-populated, I cannot change this column and its values. My "NUM_ID" column is currently empty - I'm hoping there is a sql command I can use to populate the column as shown above? I've tried using Python but updating 3M rows is taking a long time. Also, if it matters, I am using PostGresSQL.
Help? Thanks!
If you are Using SQL Server then You Should Use ROW_NUMBER() as below :
SELECT *, ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ID) NUM_ID FROM #TM
Result :
ID NUM_ID
1 1
2 1
2 2
2 3
3 1
3 2
4 1
5 1
5 2
5 3
5 4

SQL Server cross a view with a set based on hierarchy

I have a view in my SQL Server database which basically holds hierarchical information between some records based on id values of type int. A simple representation is as follows:
ID Parent_ID
1 NULL
2 1
3 2
4 NULL
5 4
By using this view I am trying to generate another view. I want all records that derive from ID=1 (which are 1, 2 and 3) to be crossed with a set, while other records (4, 5) be crossed another set. As an example crossing all records that derive from ID=1 with set (1,2) and crossing other records with set 3, I want a view as follows:
ID Value
1 1
1 2
2 1
2 2
3 1
3 2
4 3
5 3