MDX query in SSAS calculated member - ssas

I tried the MDX Query in SQL correctly
But I will insert that query in the VisualStudio(SSAS) in the Calculated Members section, but the process time will be faulty
Select
[Measures].[Fact Student Status Count]on columns
From [Cube_REG_Amozesh DW]
Where ( [Dim Student Status].[Student Status Key].[Student Status Key]-[Dim Student Status].[Student Status Key].&[0])
I get the following error:
Severity Code Description Project File Line Suppression State
Error MdxScript(Cube_REG_Amozesh DW) (2, 5) Parser: The syntax for
'Select' is incorrect. (CREATE DYNAMIC SET CURRENTCUBE.[RegSt] AS
Select [Measures].[Fact Student Status Count]on columns from
[Cube_REG_Amozesh DW] where ( [Dim Student Status].[Student Status
Key].[Student Status Key]-[Dim Student Status].[Student Status
Key].&[0]), DISPLAY_FOLDER = 'شاخص' ; ). 0

Based on your query, this is what you should be putting in Calculated measure
Sum({[Dim Student Status].[Student Status Key].[Student Status Key]-[Dim Student Status].[Student Status Key].&[0]},[Measures].[Fact Student Status Count]).
Plus make sure you uses Calculated measure not Set

its worked:
CREATE MEMBER CURRENTCUBE.[Measures].RegST
AS [Measures].[Fact Student Status Count]-([Measures].[Fact Student Status Count],[Dim Student Status].[Student Status Key].&[0])

Related

How to apply security policy on a table which depends information of inner join table?

I want to implement Row Level Security to my Database. In my database there are 2 table. First table consist id,name and status column. Status column is used to determine the level of a record. Second table consist wage,time and user_id column.
User Table
id
name
status
10
james
non-vip
11
mark
vip
12
edward
non-vip
Note: id is unique
Wage Table
userid
wage
month
10
100
jan
11
500
jan
12
250
jan
Normally when i run for "select * from wagetable where wage > 200" this query, it will return record 11 and 12. However i want that vip people's wage shouldn't be seen by HR analysts. To accomplish this task i will create a security policy on wage table but in wage table i dont have the information of status. I only have user code. How can i overcome this problem ?
I've solved the problem.By creating a view and applying policy on the view.
Firstly i have created a view inner joins the wage table with user table to obtain user's status information.
Secondly i defined a function
CREATE FUNCTION [dbo].[fn_RowLevelSecurity] (#status varchar(50))
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN SELECT 1 as fn_SecureData
Where #status = user_name()
And Finally i applied security policy by using function to my view.
CREATE SECURITY POLICY RowFilter
ADD FILTER PREDICATE dbo.fn_RowLevelSecurity(status)
ON dbo.MyView
WITH (STATE = ON);

SQL query to Manage Visas

I have Employees table, Visas table & mobilization table, I need to write a SQL query as a result: EmployeeIdno,Name,Surname,Designation,mobilizationidno,(Most recent Visa Exp Date based on country ) al in one row.
question is: Visa can be multiple and column must be added at the end ( multiple rows coming, I need column in each 1 Employee record )
example I am receiving:
'001','john','Doe','Developer','Mob1','Visa1','2018-02-18'
'001','john','Doe','Developer','Mob1','Visa2','2018-02-19'
Example I need:
'001','john','Doe','Developer','Mob1','Visa1','2018-02-18','Visa2','2018-02-19',Visa3,'2018-02-20'....
Visa assigned to person can be increase or decrease
Employees Table:
IDNO,NAME,SURNAME,DESIGNATION
VISAS table:
IDNO,VISATYPE,IDNO,ISSUEDATE,EXPIREDATE
VISATYPES table:
IDNO,NAME,COUNTRYIDNO
COUNTRIES table:
IDNO,NAME
Mobilization List:
IDNO,EMPLOYEEIDNO,MOBDATE
Mobilization Types:
IDNO,MOBTYPE,COMPANY,LOCATION,EXPECTEDMOBDATE

I have a table students where i created column average_grades and I have also table grades with columns

I have a table students where I created column average_grades and I also have a table grades with columns G_id, g_grades, g_course, g_student, g_data.
I want to add all average grades to column in average_grades in students table have same errors my query is
insert into students (average_grades)
select avg(grades.g_grade)
from grades
and my error
Error starting at line : 7 in command - insert into students (average_grades) select avg(grades.g_grade) from grades
Error report - SQL Error: ORA-01400: невозможно вставить NULL в ("DB_141ADM036"."STUDENTS"."ST_ID") 01400. 00000 - "cannot insert NULL into (%s)"
*Cause: An attempt was made to insert NULL into previously listed objects.
*Action: These objects cannot accept NULL values.
EDIT: translation from Russian:
ORA-01400: can not insert a NULL ("DB_141ADM036". "STUDENTS". "ST_ID")
its seems you are inserting new record in student table and guess there is more column in student table that's value also need to be provide .
insert into students (S_id,average_grades)
select XXX, avg(grades.g_grade) // logic to get student ID
from grades
I guess if you already inserted record in student table in that case you need to update average_grades column like
Update students
SET average_grades = ( select avg(grades.g_grade from grades )
WHERE Student_Id = XXX

How to combine records of same data?

I have to select students from database on the bases of their status,
status could be active or Inactive, there could be some students with the status active and some with status Inactive in db.
I have to select students with same status at a time.
can be done with a very simple query but problem is this query is generated automatically on run time and I can't give hard-coded status.
How to retrieve records of same status at a time?
EDIT: For Instance there is these records in db
Student A: Status Inactive
Student B: Status Inactive
Student C: Status Active
Student D: Status Inactive
Now I need to retrieve Students A,B and D in a single result set cause they have same status i-e "Inactive" while need to retrieve Student D in a seperate result set cause it has different status i-e "Active".
Hope now the scenario is clear.
If your "system" automatically alters the query, then you are out of luck entirely. There is nothing you can do other than change the system to allow you to alter the query.
But if you can, simply use an OR statement
ie.
select studentid, studentname, status
where status = 'value1' OR status = 'value2'
or more simply ...
select studentid, studentname, status
where status in ('value1','value2')
Simple.
If you need two recordset, use two query, otherwise simpy use the sorting:
ORDER BY status, name
then try this ...
select studentid, studentname, status where (status = 'value1' AND status <> 'value2') OR
(status <> 'value1' AND status = 'value2')

How to get hierarchical data

I want to execute a SQL sentence, there are two tables
Table Name Description
********* *********
H_person Employee Data
Modelorg Position Data
I want to write an sql that shows "who is connected to whom". In h_person table
Fıeld Name Description
********* *********
p_no Employee ID
P_ad Employee Name
P_soyad Employee Surname
P_pzsyn Employee Position ID
In Modelorg Table
Fıeld Name Description
********* *********
Pozkod Position ID
Pozad Position Name
USTPOZKOD the upper position (POZKOD connected to USTPOZKOD)
USTPOZKOD is also the POZKOD at the same time,there is a hierarchial connection.
I would like to form a report shows person ID, Name, Surname, Position Name, Upper Position Name, Manager ID and Maneger Name-Surname(helding Upper Position ID).
Additionally sometimes Upperpostion may be empty and when executing the report, it will be null. I would like to add a rule, If upperposition is null, bring 2 level upper manager
In H_person Table
First Data
*** ***
P_no=14556 P_ad=John
p_Soyad= Onel
P_Pzsyn= 72878 /Account Specialist
Second Data
P_no=14656
P_ad=Sara
p_Soyad= Yildiz
P_Pzsyn= 5455 /Account Manager
*** ***
In Modelorg Table
Pozkod=72878
Pozad=Account Specialist
Ustpozkod=5455 (Account Manager) USTPOZKOD is at the same time a pozkod.
select p_no, p_ad, p_soyad, Pozad, USTPOZKOD (we connected pozkod to USTPOZKOD and I woul like to bring "who held this USTPOZKOD(NAME, Surname) from h_person, modelorg.
USTPOZKOD also equals to POZKOD because someone helds this position
You need to use a common table expression (CTE) to build the recursive query.
An example:
WITH #rec AS
(
SELECT ID, ParentID
FROM RecTable
WHERE ParentID = 0
UNION ALL
SELECT R.ID, R.ParentID
FROM RecTable R
INNER JOIN #rec P ON R.ParentID = P.ID
)
SELECT *
FROM #rec