SSAS Dimension Attribute based on a SSAS calculated measure - ssas

I'm currently struggle with an issue and I hope you help with that.
I need to dynamically associate an attribute of a dimension in my SSAS cube (multidimensional) based on a calculation performed on the cube itself.
Simplifing, based on a date selected by the user, I have a calculation (already done) that returns the number of days that type of material is in stock. With that value I want to return an attribute based on a range of values.
for example:
Nr_Days_Calculated = 80
DIMENSION:
ID INI END DSC
1 0 90 TextA
2 91 180 TextB
3 181 99999 TextC
Result : 1 - TextA
Can anyone please help me? Thanks for your attention.

It's doable if your dimension also has dynamic calculations. Let me show you another example, but idea is the same.
I have a dimension [Repeat Customers], if a customer (identified by email) signed in the second, third, fourth time - move this customer to appropriate member of this dimension.
First, create dimension with one default member.
Second, add several empty members:
(you can skip it, because dim members are materialized in your case)
CREATE MEMBER CURRENTCUBE.[Repeat Customers].[Repeat Customers].[All].[One] as NULL;
CREATE MEMBER CURRENTCUBE.[Repeat Customers].[Repeat Customers].[All].[Two] as NULL;
CREATE MEMBER CURRENTCUBE.[Repeat Customers].[Repeat Customers].[All].[Three] as NULL;
CREATE MEMBER CURRENTCUBE.[Repeat Customers].[Repeat Customers].[All].[Four] as NULL;
CREATE MEMBER CURRENTCUBE.[Repeat Customers].[Repeat Customers].[All].[Five] as NULL;
CREATE MEMBER CURRENTCUBE.[Repeat Customers].[Repeat Customers].[All].[6+] as NULL;
CREATE MEMBER CURRENTCUBE.[Repeat Customers].[Repeat Customers].[All].[N/A] as NULL;
Third, add their calculations:
SCOPE([Repeat Customers].[Repeat Customers].[All].[One],[Measures].[Count]);
THIS=Count(Filter([Email].[Email].Members,([Measures].[Count],[Repeat Customers].[Repeat Customers].&[0])=1));
END SCOPE;
SCOPE([Repeat Customers].[Repeat Customers].[All].[Two],[Measures].[Count]);
THIS=Count(Filter([Email].[Email].Members,([Measures].[Count],[Repeat Customers].[Repeat Customers].&[0])=2));
END SCOPE;
...
SCOPE([Repeat Customers].[Repeat Customers].[All].[6+],[Measures].[Count]);
THIS=Count(Filter([Email].[Email].Members,([Measures].[Count],[Repeat Customers].[Repeat Customers].&[0])>=6));
END SCOPE;
SCOPE([Repeat Customers].[Repeat Customers].[All].[N/A],[Measures].[Count]);
THIS=([Measures].[Count],[Repeat Customers].[Repeat Customers].&[0])
-SUM({
[Repeat Customers].[Repeat Customers].[All].[One]
,[Repeat Customers].[Repeat Customers].[All].[Two]
,[Repeat Customers].[Repeat Customers].[All].[Three]
,[Repeat Customers].[Repeat Customers].[All].[Four]
,[Repeat Customers].[Repeat Customers].[All].[Five]
,[Repeat Customers].[Repeat Customers].[All].[6+]
},[Measures].[Count]);
END SCOPE;
Output:
I think your filter has to use >= and <= Nr_Days_Calculated. But not sure which measure are you trying to show. Is 1 - TextA related to [Measures].[Nr_Days_Calculated]? If yes, we are good, just use your measure and SUM or else as aggregate instead of Count() in my case.
This is not the best performance solution (because of dynamic calculation), but still it works. Hope it helps.

Related

Why do we need to Treat() MDSYS.ST_GEOMETRY as ST_LINESTRING to use ST_PointN(1)?

MDSYS.ST_GEOMETRY; Oracle 18c:
The following query works. It extracts the first point from an MDSYS.ST_GEOMETRY:
--Source: https://www.spdba.com.au/using-oracles-st_geometry-type-hierarchy-with-sdo_geometry-st_pointn-and-st_numpoints/
with cte as (
select treat(mdsys.st_geometry.from_wkt('LINESTRING(10 10, 20 20)',26917) as mdsys.st_linestring) as shape
from dual
)
select
(shape).st_pointn(1) as first_point
from
cte
Result:
MDSYS.ST_POINT(MDSYS.SDO_GEOMETRY(2001, 26917, MDSYS.SDO_POINT_TYPE(10, 10, NULL), NULL, NULL))
I don't understand why we need to Treat() the ST_GEOMETRY supertype as an ST_LINESTRING subtype in order to use ST_PointN() to get the point.
For example, if I remove the Treat(... as ST_LINESTRING), then I get an error:
with cte as (
select mdsys.st_geometry.from_wkt('LINESTRING(10 10, 20 20)',26917) as shape
from dual
)
select
(shape).st_pointn(1) as first_point
from
cte
Error:
ORA-00904: "MDSYS"."ST_GEOMETRY"."ST_POINTN": invalid identifier
Why do I get that error when I remove Treat()?
Why do I get that error when I remove Treat()?
ST_LINESTRING is a sub-type of ST_CURVE which, in turn, is a sub-type of ST_GEOMETRY.
ST_POINTN is a member function declared on the sub-type ST_CURVE and ST_LINESTRING inherits this function.
ST_POINTN is not declared as a member function on the parent type ST_GEOMETRY.
The ST_GEOMETRY.FROM_WKT() function returns an ST_GEOMETRY instance that, in this case is a actually a ST_LINESTRING sub-type but the return type of the function is ST_GEOMETRY as it could return any child sub-type.
When you remove TREAT() then you are trying to call the ST_POINTN member function on the parent type ST_GEOMETRY and, as the error message states "MDSYS"."ST_GEOMETRY"."ST_POINTN" is an invalid identifier because the type does not have that member function.
When you include TREAT() then you cast the super-type to the sub-type and then call the member function on that sub-type and the member function does exist so it works.
A similar example is:
CREATE TYPE parent_type IS OBJECT (
x NUMBER,
y NUMBER
) NOT FINAL;
CREATE TYPE child_type UNDER parent_type (
MEMBER FUNCTION get_x RETURN NUMBER
);
CREATE TYPE BODY child_type IS
MEMBER FUNCTION get_x RETURN NUMBER
IS
BEGIN
RETURN self.x;
END;
END;
/
Then:
CREATE FUNCTION create_parent RETURN PARENT_TYPE
IS
BEGIN
RETURN child_type(1, 2);
END;
/
If you use:
SELECT create_parent().get_x() FROM DUAL;
Then the function declares it returns a PARENT_TYPE and so the member function is called on that type, even though the actual returned value is a CHILD_TYPE, so raises the error:
ORA-00904: "SCHEMA_NAME"."PARENT_TYPE"."GET_X": invalid identifier
If you use TREAT to cast the returned parent to its actual child type:
SELECT TREAT(create_parent() AS child_type).get_x() FROM DUAL;
Then the output is:
TREAT(CREATE_PARENT()ASCHILD_TYPE).GET_X()
1
db<>fiddle here

How to compare two oracle sql object type with common base super class

Example:
Figure_t base class (super class)
sphere_t under figure_t
pyramid_t under figure_t
both has volume.
How to do objects comparison using map or order function?
What I am doing is using the map member function in the super class for comparing using the volume. I have tried with/without override of the map function in the subclass but still no luck. I can compare if I create the same object twice but not if I create different ones.
In example below I paste just the sphere since it is almost the same for both sphere and pyramid.
This is my super class:
CREATE OR REPLACE TYPE figure_t AS OBJECT (
v_volume NUMBER,
v_area NUMBER,
MAP MEMBER FUNCTION compare RETURN NUMBER, PRAGMA restrict_references ( compare, wnds, trust )
);
/
CREATE OR REPLACE TYPE BODY figure_t AS
MAP MEMBER FUNCTION compare RETURN NUMBER IS
BEGIN
RETURN v_volume;
END;
END;
/
ALTER TYPE figure_t NOT FINAL
CASCADE;
/
Then, this is my subtype:
CREATE OR REPLACE TYPE sphere_t UNDER figure_t (
v_radio NUMBER,
CONSTRUCTOR FUNCTION sphere_t (
radio NUMBER
) RETURN SELF AS RESULT,
MEMBER FUNCTION get_volume RETURN NUMBER,
MEMBER FUNCTION get_area RETURN NUMBER,
OVERRIDING MAP MEMBER FUNCTION compare RETURN NUMBER
);
/
CREATE OR REPLACE TYPE BODY sphere_t AS
CONSTRUCTOR FUNCTION sphere_t (
radio NUMBER
) RETURN SELF AS RESULT IS
BEGIN
self.v_radio := radio;
self.v_volume := ( 4 / 3 ) * 3.141592654 * power(radio, 3);
self.v_area := 4 * 3.141592654 * power(radio, 2);
return;
END;
MEMBER FUNCTION get_volume RETURN NUMBER IS
BEGIN
RETURN v_volume;
END;
MEMBER FUNCTION get_area RETURN NUMBER IS
BEGIN
RETURN v_area;
END;
OVERRIDING
MAP MEMBER FUNCTION compare RETURN NUMBER IS
BEGIN
RETURN self.v_volume;
END;
END;
/
For doing the comparison it looks like:
DECLARE
sphere_v sphere_t;
pyramid_v pyramid_t;
BEGIN
pyramid_v := pyramid_t(120, 90, 30);
sphere_v := sphere_t(10);
IF ( sphere_v != pyramid_v ) THEN
dbms_output.put_line('NOT EQUAL');
END IF;
END;
There should be a way for this comparison since figures have a super class in common.
There should be a way for this comparison since figures have a super
class in common
Am not very sure as what you wanted to achieve here. Also what is definition of pyramid_t which you are using in your comparison.
IF ( sphere_v != pyramid_v ) THEN
The above condition looks dicey to me as this will always be the case.
When you do sphere_v := sphere_t(10); means you try to get all the return of the sphere_t to sphere_v.
So it would be good if you could compare the volume and area of the Sphere and pyramid separately. See below demo how you could take these value:
DECLARE
sphere_v sphere_t;
-- pyramid_v pyramid_t;
BEGIN
sphere_v := sphere_t(10);
dbms_output.put_line('Input Radio -->'||sphere_v.v_radio);
dbms_output.put_line('Volume of Sphere-->'||sphere_v.v_volume);
dbms_output.put_line('Area Of Sphere -->'||sphere_v.v_area);
--Similarly you can take the values of `volume` and `area`
--of pyramid and get it compared with that of Sphere.
-- pyramid_v := pyramid_t(120, 90, 30);
-- dbms_output.put_line('Input Radio Pyramid -->'||pyramid_v.v_radio);
-- dbms_output.put_line('Volume of Pyramid -->'||pyramid_v.v_volume);
-- dbms_output.put_line('Area Of Pyramid -->'||pyramid_v.v_area);
-- If sphere_v.v_volume = pyramid_v.v_volume then
-- dbms_output.put_line('Equal');
-- Else
-- dbms_output.put_line('Not Equal');
END;
Assumption: pyramid_t also have the same Object Body definition having volume and area calculation.
There should be a way for this comparison since figures have a super class in common.
There is a way, it's just not obvious.
When the types are exactly the same e.g. two instances of the same subtype we can invoke the map function implicitly. So we can compare two spheres like this:
IF ( sphere_1 != sphere_2 ) THEN ...
However, to compare two different subtypes we need to invoke the parent map function, and to make this happen we must reference it explicitly:
IF ( sphere_v.compare() != pyramid_v.compare() ) THEN ...
Yes, this is clunky. But Oracle is an RDBMS not an ORDBMS (whatever they claimed back in the version 8.0 days).

PL/SQL: Type mismatch when fetching cursor from pipeline table function

I would like to simply SQL interface to Oracle R Enterprise.
First, I create a global object type and a table type from the type.
create or replace type sts_one_sample_t_test is object
(
"Variable Name" varchar2(4000),
"Average" number,
"T-Test" number,
"P-Value" number,
"Con.Level Lower Bound (95%)" number,
"Con.Level Upper Bound (95%)" number
);
create or replace type sts_one_sample_t_test_table is table of sts_one_sample_t_test;
Second, I make the pipelined function to return the table object.
create or replace function f_sts_one_sample_t_test
(p_data in sys_refcursor,
target_number in number)
return sts_one_sample_t_test_table pipelined
is
v_sts_one_sample_t_test sts_one_sample_t_test;
cursor v_cursor is
select *
from table (
cast(
rqTableEval(
p_data,
cursor
(
select target_number as "target_number",
1 as "ore.connect"
from dual
), -- Param Cursor
'select str_col as "Variable Name",
num_col as "Average",
num_col as "T-Test",
num_col as "P-Value",
num_col as "Con.Level Lower Bound (95%)",
num_col as "Con.Level Upper Bound (95%)"
from RQSYS.RQ_TEMP
WHERE ROWNUM=1', -- Output Definition
'R_ONE_SAMPLE_T_TEST' -- R Script
) as sts_one_sample_t_test_table
)
);
begin
v_sts_one_sample_t_test:=sts_one_sample_t_test(null,null,null,null,null,null);
open v_cursor;
loop
fetch v_cursor into v_sts_one_sample_t_test; --- [Error] PLS-00386 (49: 17): PLS-00386: type mismatch found at 'V_STS_ONE_SAMPLE_T_TEST' between FETCH cursor and INTO variables
exit when v_cursor%notfound;
pipe row(v_sts_one_sample_t_test);
end loop;
close v_cursor;
return;
end f_sts_one_sample_t_test;
But error is raised from compiler:
[Error] PLS-00386 (49: 17): PLS-00386: type mismatch found at 'V_STS_ONE_SAMPLE_T_TEST' between FETCH cursor and INTO variables
Please help me.
I have found solution.
Instead of using fetch into object type, I fetch into each object element explicitly.
fetch v_cursor into
v_sts_one_sample_t_test."Variable Name" ,
v_sts_one_sample_t_test. "Average" ,
v_sts_one_sample_t_test."T-Test" ,
v_sts_one_sample_t_test."P-Value" ,
v_sts_one_sample_t_test."Con.Level Lower Bound (95%)" ,
v_sts_one_sample_t_test."Con.Level Upper Bound (95%)" ;
The error is passed, but the function go to infinite loop when running.

ORACLE SQL Method Produces Warning

I'm having trouble getting the following member method to compile (count_single_buses). Would appreciate any advice on what might be wrong syntactically with my code.
CREATE OR REPLACE TYPE BodyModel2_Type AS OBJECT(
ModelID INTEGER,
ModelName VARCHAR2(45),
FloorType VARCHAR2(45),
Manufacturer VARCHAR2(45),
Length NUMBER(8,2),
Width NUMBER(8,2),
NoOfAxles INTEGER,
MEMBER FUNCTION count_single_buses(ModelID INTEGER) RETURN INTEGER);
/
CREATE OR REPLACE TYPE BODY BodyModel2_Type AS
MEMBER FUNCTION count_single_buses(ModelID INTEGER) RETURN INTEGER IS
N INTEGER;
BEGIN
N := (SELECT COUNT(BODYMODELREF) FROM SINGLEDECKBUS_TABLE S
WHERE S.BODYMODELREF = ModelID);
RETURN N;
END count_single_buses;
END;
--EDIT--
Thanks to #Ravi, I managed to solve the issue my correcting my SQL syntax and setting the resultset to a NUMBER, instead of INTEGER.
CREATE OR REPLACE TYPE BODY BodyModel_Type AS
MEMBER FUNCTION count_single_buses(thisModelID INTEGER) RETURN NUMBER IS
NUM NUMBER;
BEGIN
SELECT COUNT(S.BODYMODELREF) INTO NUM FROM SINGLEDECKBUS_TABLE S WHERE S.BODYMODELREF.MODELID = thisModelID;
RETURN NUM;
END count_single_buses;
END;
/
Still not sure why #Ravi's exact code still produced the warning, and thought that resultset when returning a count value could go into an integer. At any rate, the code works now. Thanks all.
Your BodyModel2_Type Type definition looks okay. However, the body definition is syntactically incorrect.
You cannot define a SQL statement directly to a variable, thus making this statement wrong.
N := (SELECT COUNT(BODYMODELREF) FROM SINGLEDECKBUS_TABLE S
WHERE S.BODYMODELREF = ModelID);
You will have to use Select... into statement in order to assign the result set of your SQL query into a variable. So, the right syntax should look like this
SELECT COUNT(BODYMODELREF) FROM SINGLEDECKBUS_TABLE S INTO N
WHERE S.BODYMODELREF = ModelID
AFAIK you don't have END the Type followed by the Type name like this END count_single_buses. It'll produce an error. So, overall your Type body specification should look like this:
CREATE OR REPLACE TYPE BODY BodyModel2_Type AS
MEMBER FUNCTION count_single_buses(ModelID INTEGER) RETURN NUMBER IS
N NUMBER;
BEGIN
SELECT COUNT(BODYMODELREF) FROM SINGLEDECKBUS_TABLE S INTO N
WHERE S.BODYMODELREF = ModelID;
RETURN (N);
END;
END;
/
I'm writing this off without any live environment available right now so please let me know if you come across any error in the above code.
Cheers.

Why am I getting PLS - 00382?

Here is my object def:
CREATE OR REPLACE TYPE FALCON.contacts AS OBJECT (phone VARCHAR2(50)
,phoneusage VARCHAR2(25)
,phonetype VARCHAR2(25)
,email VARCHAR2(150)
,phoneext VARCHAR2(25)
,anytext VARCHAR2(250))
Here is the table def:
CREATE OR REPLACE TYPE FALCON.contacttbl AS TABLE OF contacts
Here is my pipelined function
FUNCTION get_pcontacts(p_conttbl IN xmltypedefs_spec.conttbl)
RETURN falcon.contacttbl
PIPELINED
IS
l_contact falcon.contacts;
BEGIN
FOR n IN 1 .. p_conttbl.count
LOOP
PIPE ROW(**falcon.contacts**(p_conttbl(n).phone, p_conttbl(n).phoneusage, p_conttbl(n).phonetype, p_conttbl(n).email, p_conttbl(n).phoneext, p_conttbl(n).anytext));
END LOOP;
RETURN;
END get_pcontacts;
I am getting the error when I call the table function here:
FUNCTION get_pidxml(p_pidrec xmltypedefs_spec.pidtyp)
RETURN CLOB
IS
l_tmprec CLOB;
l_pxml xmltype;
l_bxml xmltype;
l_pcontacts xmltypedefs_spec.conttbl := p_pidrec.personalcont;
l_bcontacts xmltypedefs_spec.conttbl := p_pidrec.businesscont;
BEGIN
-- l_pxml := get_contacts(p_pidrec, 'p');
-- l_bxml := get_contacts(p_pidrec, 'b');
SELECT xmlelement("pid"
,xmlforest(p_pidrec.setid AS "setID"
,p_pidrec.patidexternal AS "patientIDExternal"
,p_pidrec.patientid AS "patientID"
,p_pidrec.patintasgnauth AS "patientIDInterAssignAuthority"
,p_pidrec.patinttypecd AS "patientIDInternalIDTypeCode"
,p_pidrec.patidalternate1 AS "patientIDAlernate1"
,p_pidrec.patlastname AS "patientLastName"
,p_pidrec.patfirstname AS "patientFirstName"
,p_pidrec.patmiddleinit AS "patientMiddleInitial"
,p_pidrec.patsuffix AS "patientSuffix"
,p_pidrec.patprefix AS "patientPrefix"
,p_pidrec.degree AS "degree"
,p_pidrec.familyname AS "familyName"
,p_pidrec.givenname AS "givenName"
,p_pidrec.mothermaidname AS "mothersMaidenName"
,p_pidrec.dob AS "dateOfBirth"
,p_pidrec.adminsex AS "administrativeSex"
,p_pidrec.patientalias AS "patientAlias"
,p_pidrec.race AS "race"
,p_pidrec.racetext AS "raceText"
,p_pidrec.pataddr1 AS "patientAddress1"
,p_pidrec.pataddr2 AS "patientAddress2"
,p_pidrec.patcity AS "patientCity"
,p_pidrec.patstate AS "patientState"
,p_pidrec.patzip AS "patientZip"
,p_pidrec.countrycode AS "countryCode"
,p_pidrec.addresstype AS "addressType"
,p_pidrec.othgeodesig AS "otherGeographicDesignation"
,p_pidrec.county AS "county"
,(SELECT xmlagg(xmlelement("contactInfo",
xmlforest(phone AS "phoneNumber",
phoneusage AS "telecomUseCode",
phonetype AS "telecomequiptype",
email AS "email",
phoneext AS "phonenumberextension",
anytext AS "anytext")))
FROM TABLE(**get_pcontacts(l_pcontacts**))) AS "personalContact"
http://pls-00382.ora-code.com/
PLS-00382: expression is of wrong type
Since I don't know how xmltypedefs_spec.conttbl is defined, I removed the input parameter from the pipelined function and just had it generate fake data on the fly:
CREATE OR REPLACE FUNCTION get_contacts
RETURN contacttbl PIPELINED
IS
-- converts some structure to pipe of contacts
BEGIN
FOR n IN 1 .. 5 LOOP
PIPE ROW(
contact(
'877-867-5309',
'Work',
'Cell',
'jenny#gmail.com',
n,
'WTF?'
)
);
END LOOP;
RETURN;
END get_contacts;
The subquery now executes without error:
SELECT
xmlagg(
xmlelement("contactInfo",
xmlforest(
phone AS "phoneNumber",
phoneusage AS "telecomUseCode",
phonetype AS "telecomequiptype",
email AS "email",
phoneext AS "phonenumberextension",
anytext AS "anytext"
)
)
)
FROM
TABLE( get_contacts( ) )
This tells me there is probably something wrong with xmltypedefs_spec.conttbl, perhaps in using a collection type within an SQL statement? Not sure. What if you changed xmltypedefs_spec.pidtyp to use the falcon.contacttbl instead of xmltypedefs_spec.conttbl. Seems like you've got one package type and one object type that are doing the same thing?
xmltypedefs_spec defines record types that correspond to XML elements. These record types are used to shred and build XML. Originally, the XML did not use repeating elements, but now must. I am attempting to take a table of xmltypedefs_spec.pidtyp and use the pipelined function to return 'rows' of data from an associative table. It is in this fashion that I want to send rows of array records to build xml.