MDX query for fact count - ssas

I want MDX(Query) expression for namedset or calculated measures for below
Tsql:
When [product].[Name] in (car1,car2,car3,car4,car5) Then [Measure].[Factcount] =0 ELSE [Measure].[Factcount] =0
I tried below MDX
IIF ({[product].[Name].&[car1],
[product].[Name].&[car2], [product].[Name].&[car3],[product].[Name].&[car4], [product].[Name].&[car5]},[Measure].[Factcount] =0,[Measure].[Factcount] =1)

Take a look at the sample below
with member measures.test
as
case
when [Product].[Subcategory].currentmember is [Product].[Subcategory].&[18] then 0
when [Product].[Subcategory].currentmember is [Product].[Subcategory].&[2] then 0
when [Product].[Subcategory].currentmember is [Product].[Subcategory].&[22] then 0
when [Product].[Subcategory].currentmember is [Product].[Subcategory].&[17] then 0
when [Product].[Subcategory].currentmember is [Product].[Subcategory].&[11] then 0
else 1
end
select measures.test
on columns,
[Product].[Subcategory].[Subcategory]
on rows
from
[Adventure Works]

Related

ABAP CDS view how to use CASE and SUM together

I have a scenario where I have to use CASE and SUM in an CDS VIEW SQL statement and I cannot add net_val to the group by statement. Is there a way to use CASE and SUM together
case when sum( net_val ) < 0 then 'DEBIT'
when sum( net_val ) > 0 then 'CREDIT'
end as DEBIT_CREDIT_STATUS

Using calculated column within SQL query

I need help with a subquery. I think I’m close but not quite sure. I want to use a calculated column within another calculation. In the example want to use “numerator” in the calculation of the “denominator”. I’m new to subqueries.
SELECT
sum(case when denominator=1 and q10=1 then 1 else 0 end) as numerator
FROM (
SELECT sum(case when q5=1 and q6=1 then 1 else 0 end) as denominator
FROM datasource
q5
q6
q10
1
1
0
0
1
0
0
0
1
I want to calculate a “denominator” column where it’s 1 if q5 and q6 are equal to 1 or else “denominator”=0. Then calculate a “numerator” column if the calculated denominator column is 1 and q10 is 1 then “numerator”=1 or else “numerator”=0. You can ignore the sum command cause I just wanted to sum the columns at the end but for the purpose of the question it isn’t important
Update…
I tried the below query as suggested and get the error “Expression not in GROUP BY key”
SELECT sum(case when denominator=1 and q10 = 1 then 1 else 0 end) as numerator
FROM (
SELECT q10,
sum(case when q5=1 and q6=1 then 1 else 0 end) as denominator
FROM datasource) t
I haven't added the aggregation part, but I think this will get you close to what you are trying to do.
SELECT case when denominator=1 and q10 = 1 then 1 else 0 end as numerator
FROM (
SELECT q10,
case when q5=1 and q6=1 then 1 else 0 end as denominator
FROM datasource
GROUP BY q10) t

Trying to add as SUM with Case to cognos

I am trying to add a Sum to a data item with a case statement. I am trying to create the results in cognos from the SQL query below.
Select
,SUM(CASE WHEN reas_desc = 'misapplied funds' THEN 1 ELSE 0 END) AS Misapplied
,SUM(CASE WHEN reas_desc = 'lump sum payment' THEN 1 ELSE 0 END) AS Lumps
,SUM(CASE WHEN proc_desc = 'title/registration' THEN 1 ELSE 0 END) AS "Title/Reg"
I want the end product to have 3 columns titled "Misapplied", "lumps", and "title/Reg"with the total of those items. The SQL code works perfectly but I need to build in cognos for others to view and use.
You define your first data item like this:
CASE WHEN reas_desc = 'misapplied funds' THEN 1 ELSE 0 END
From that you should be able to figure out the rest.
Apparently more detail is needed:
Data item 1:
Expression: CASE WHEN reas_desc = 'misapplied funds' THEN 1 ELSE 0 END
Name: Misapplied
Detail Aggregation: Total
Data item 2:
Expression: CASE WHEN reas_desc = 'lump sum payment' THEN 1 ELSE 0 END
Name: Lumps
Detail Aggregation: Total
Data item 3:
Expression: CASE WHEN proc_desc = 'title/registration' THEN 1 ELSE 0 END
Name: Title/Reg
Detail Aggregation: Total

Choosing one value from partially duplicate rows

I am trying to choose between two rows of data in order to get a total count of type.
Table - Evaluations
EvaluationID (link to minEval_ID and max_EvalID)
EstablishedDelays
Table - Outcome
min_EvalID
max_EvalID
EligTypeRecalc
This is what my current query is:
SELECT "NewEligType"=
COUNT (*),
SUM (CASE WHEN a.EligTypeRecalc IN (1,4,5,7) Then 1 Else 0 END) 'Established Condition',
SUM (CASE WHEN a.EligTypeRecalc=6 Then 1 Else 0 END) 'Established Delay & At-Risk',
SUM (CASE WHEN a.EligTypeRecalc=2 and b.EstablishedDelays=1 Then 1 Else 0 END) 'Established Delay only: One Delay',
SUM (CASE WHEN a.EligTypeRecalc=2 and b.EstablishedDelays=2 Then 1 Else 0 END) 'Established Delay only: Two Delays',
SUM (CASE WHEN a.EligTypeRecalc=2 and b.EstablishedDelays>=3 Then 1 Else 0 END) 'Established Delay only: Three+ Delays',
SUM (CASE WHEN a.EligTypeRecalc=8 Then 1 Else 0 END) 'Clinical Judgement'
from Outcome a
join Evaluations b
on a.max_EvalID=b.evaluationid and a.min_evalID=b.evaluationID
where a.EligTypeRecalc<>3
The problem I'm encountering is picking the correct Eval_ID to choose the correct number of delays and not count the other. The EstablishedDelays associated with max_EvalID is correct unless the EligTypeRecalc is 0, then it should count the delays associated with min_EvalID.
So far I've come up with this basic logic but I'm having a mind block on how to get it to the next step:
CASE WHEN EligTypeRecalc=max_EvalID
THEN EstablishedDelays=max_EvalID
ELSE EstablishedDelays=min_EvalID
Bonus points: I only have Read access to the database.
**What's the proper query syntax to use to select the row associated and exclude the other?
Thanks for your advice!
You might try wrapping your summary of the Evaluations table into a CTE and then join that to the Outcome table.
Here is the code I used, its a little long:
WITH min_eval AS
(
SELECT
a.DataMatch,
a.max_EvalID,
a.EligTypeRecalc,
b.evaluationid,
b.EstablishedDelays
FROM Outcomes a
JOIN Evaluations b
ON a.min_EvalID=b.evaluationid
WHERE a.EligTypeRecalc<>3
),
max_eval AS
(
SELECT
a.DataMatch,
a.max_EvalID,
a.EligTypeRecalc,
b.evaluationid,
b.EstablishedDelays
FROM Outcomes a
JOIN Evaluations b
ON a.max_EvalID=b.evaluationid
WHERE a.EligTypeRecalc<>3
)
SELECT "NewEligType"=
COUNT (*),
SUM (CASE WHEN a.EligTypeRecalc IN (1,4,5,7) THEN 1 ELSE 0 END) 'Established Condition',
SUM (CASE WHEN a.EligTypeRecalc=6 THEN 1 ELSE 0 END) 'Established Delay & At-Risk',
SUM (CASE WHEN (a.EligTypeRecalc=2 AND a.EstablishedDelays=1) OR (a.EligTypeRecalc=2 AND a.EstablishedDelays=0 AND b.EligTypeRecalc=2 AND b.EstablishedDelays=1) THEN 1 ELSE 0 END) 'Established Delay only: One Delay',
SUM (CASE WHEN (a.EligTypeRecalc=2 AND a.EstablishedDelays=2) OR (a.EligTypeRecalc=2 AND a.EstablishedDelays=0 AND b.EligTypeRecalc=2 AND b.EstablishedDelays=2) THEN 1 ELSE 0 END) 'Established Delay only: Two Delays',
SUM (CASE WHEN (a.EligTypeRecalc=2 AND a.EstablishedDelays>=3) OR (a.EligTypeRecalc=2 AND a.EstablishedDelays=0 AND b.EligTypeRecalc=2 AND b.EstablishedDelays>=3) THEN 1 ELSE 0 END) 'Established Delay only: Three+ Delays',
SUM (CASE WHEN a.EligTypeRecalc=8 THEN 1 ELSE 0 END) 'Clinical Judgement'
FROM max_eval a
JOIN min_eval b
ON a.DataMatch=b.DataMatch

Sum of single column for different conditions

I have two columns in my table colum#1 is varchar(MAX) and column#2 is int.
In column#2 I have negative and positive entries I want sum of positive entries in one column and sum of negative entries in other column in my select query
To achieve this I did
SELECT SUM(atbl.M),SUM(atbl.p)
from (select M=case when column#2<0
then column#2 else 0 end,
P=case when column#2 > 0
then column#2 else 0 end
from testTable) atbl
or
select SUM(case when column#2<0 then column#2 else 0 end) as M
,SUM(case when column#2 > 0 then column#2 else 0 end) as P
from testTable
Is there any better way to achieve this.
If you do it frequently on that column then use a view:
create view testView as
select
sum(case when column#2 < 0 then column#2 else 0 end) as M,
sum(case when column#2 > 0 then column#2 else 0 end) as P
from testTable