How to conditional count in MicroStrategy? - business-intelligence

Trying to perform the below statements in MicroStrategy to no avail.
Count(Distinct case when Insight <> 'Missing' then call_discuss_sk else null end)
and
Count(Distinct case when Quarters_to_this_quarter between 1 and 5 then call_discuss_sk else null end)
current MSTR code looks like below and can confirm that this doesn't work...
Count<Distinct=True, UseLookupForAttributes=False>(ApplySimple("case when #0='Missing' then NULL else #1 END",Insight,[Call Discuss Sk])){~}
Table I'm querying looks like the below

so this is how to do it, note that #0 and #1 refer to the fields after the "" statement so in this case #0 = Insight and #1 = [Call Discuss Sk]
ApplyAgg("Count(Case when #0='Missing' then null else #1 end)", Insight, [Call Discuss Sk]){~+}

Related

Selecting either in array or null in Hibernate query language

I would like to select Users that has group that has name in a given array or has null group.
This is my attempted query:
#Query("SELECT u FROM User u WHERE (u.group is null OR u.group.name IN :groups)")
Optional<Page<User>> findUsersByKeywordAndGroupIncludingNullGroup(Pageable pageable, #Param("groups") String... groups); but when I run I have an error in SQL syntax.
Therefore, is there a correct way to do this?
Thanks in advance.
Problem with your query is
If you will pass (null or empty) groups, then the resultant query will become
select * from users where users.group is null or users.group in ()
Thus results in Syntax Error near '('
You can do a hack using sPEL
#Query("SELECT U FROM User U " +
"WHERE U.group IS NULL " +
"OR (1=:#{ #groups == null || #groups.size() == 0 ? 1 : 0} OR U.group.name IN :#{#groups})")
List findUserWithoutGroupOrInGroup(#Param("groups") List<String> groups);
Here, I have used a proxy condition
1=:#{ #groups == null || #groups.size() == 0 ? 1 : 0}
Second part of this condition returns 1 if the parameter groups is null or empty, results in condition 1=1 being TRUE and thereby skipping U.group.name IN :#{#groups}
This approach is not a recommended one, and also not scalable.
In my opinion, you should fetch the Users without any Group and Users in given group separately. You can even fire there queries in parallel and control not to fire Users in given group if groups is empty.

Oracle IF inside CASE

How can I use an if elsif inside a case when using Oracle 11G?
I want to end with the type_pre value with 3 or 4 based on another column: gr.gt_id.
I try to run and the error message is missing keyword.
Is there a better way to do this task ?
CASE
WHEN certv.id IS NOT NULL THEN NULL
WHEN cert.id IS NOT NULL THEN
IF gr.gt_id = 0 THEN type_pre = 3
ELSIF gr.gt_id = 1 THEN type_pre = 4
END IF
WHEN not.id IS NOT NULL THEN NULL
END as type_pre,
CASE
WHEN certv.id IS NOT NULL THEN NULL
WHEN cert.id IS NOT NULL and gr.gt_id = 0 THEN 3
WHEN cert.id IS NOT NULL and gr.gt_id = 1 THEN 4
WHEN not.id IS NOT NULL THEN NULL
END as type_pre,
I see no one explained you mistakes, so let me tell me where you wen't wrong :
First, no need for nested IF's/Case's , it can be done with a single case each time like #mathguy answer.
IF is used in PL/SQL and cannot be used directly in oracle, so if you wanted to do it in your method, with nested conditions, then this should have been your query:
CASE
WHEN certv.id IS NOT NULL THEN NULL
WHEN cert.id IS NOT NULL THEN
CASE WHEN gr.gt_id = 0 THEN = 3
WHEN gr.gt_id = 1 THEN = 4 END
WHEN not.id IS NOT NULL THEN NULL
END as type_pre,
Your second problem beside the use of IF was the use of a statement in the THEN part. This is a CASE EXPRESSION , it can be used to put a value, not a statement, which is why i removed the type_pre part.

Using a cast and case query help, possible parenthesis issue?

I'm pretty new to this and I'm hoping it's a simple tweak but I've got a small query that keeps outputting errors in my report. The original query was:
CAST((CASE WHEN (AI.STATUS = 'F' OR AI.STATUS = '*') THEN 'CPL'
ELSE 'WIP' END) as varchar(10)) as AnalysisStatus,
and then I'm trying to modify it to the below. I've tried different iterations but no luck on anything. Any help is greatly appreciated.
cast(((CASE
WHEN AI.STATUS = 'F' THEN 'CPL'
WHEN (AI.STATUS = '*' AND AI.METHOD.FULL <> 'METHOD') THEN 'CPL'
ELSE 'WIP' END)) as varchar(10)) as AnalysisStatus,

LINQ: QuerySyntaxException

I've written some complex LINQ query which I'm trying to run against NHibernate, unfortunately this causes an exception when trying to evaluate the query (call .ToList()).
Here is the code, which is supposed to retrieve Issue objects with some extra data:
var list = from i in sess.Query<Issue>()
let readFlag = (from f in i.ReadFlags
where (f.User != null && f.User.Username == request.UserName)
||
(f.Device.Name == request.MachineName)
select f).FirstOrDefault()
let isUnread = readFlag == null
let unreadCommentsCount = isUnread ? 0 : (from c in i.Comments
where c.DateCreated > readFlag.LastSeenCommentDate
select c).Count()
let statusChanged = isUnread && i.Status != readFlag.LastSeenStatus
where isUnread || unreadCommentsCount > 0 || statusChanged
select new
{
Issue = i,
ReadFlag = readFlag,
IsUnread = isUnread,
UnreadCommentsCount = unreadCommentsCount
};
i.e. - for each Issue object look for related IssueReadFlag which matches current username or machine name. if the Flag exists, it also contains the "last seen comment date", so the code calculates the number of new comments for this Issue.
Unfortunately NHibernate is not able to handle this query (Exception details).
I'm looking for the most optimal solution for this problem.
Obtaining all the Issue objects and applying the query criteria on the client-side is unacceptable for me.
trying to find out what causes the issue: It started working as I've commented out evaluation of unreadCommentsCount and statusChanged both in "select" and "where" statements. Following this lead, It seems that referencing readFlag in any of these subqueries causes the issue (after removing reference to readFlag in "let unreadCommentsCount .." and "let statusChanged.." conditions, the query doesn't throw exceptions).
Now I need a tip on how to make it functional (working as expected) again ...
Further investigating the issue I've found out that NHibernate can't handle the uncertain state of readFlag existence.
I've ended up splitting the query into two parts, first of them takes all the Issues where readFlag does not exists, the second one queries the IssueReadFlag directly, calculating the Unread Comments Count and Changed Status.
This seems to be the only solution as for now.

How do I check if a SQL Server 2005 TEXT column is not null or empty using LINQ To Entities?

I'm new to LINQ and I'm trying to check whether a TEXT column is null or empty (as String.IsNullOrEmpty).
from c in ...
...
select new
{
c.Id,
HasBio = !String.IsNullOrEmpty(c.bio)
}
Trying to use the above query produces an SqlException:
Argument data type text is invalid for argument 1 of len function.
The SQL generated is similar to the following:
CASE WHEN ( NOT (([Extent2].[bio] IS NULL) OR (( CAST(LEN([Extent2].[bio]) AS int)) = 0))) THEN cast(1 as bit) WHEN (([Extent2].[bio] IS NULL) OR (( CAST(LEN([Extent2].[bio]) AS int)) = 0)) THEN cast(0 as bit) END AS [C1]
LEN is not applicable to TEXT columns. I know DATALENGTH should be used for them...
How can I force LINQ to produce such thing? Or any other workaround to test if a text column is null or empty???
Thanks!
Update
I came up with this
HasBio = c.bio.Substring(0, 1).Length > 0
but it's a little bit ugly though, any other options?
Well I've decided to convert the TEXT columns to VARCHAR(MAX) taking in mind the following article.
http://geekswithblogs.net/johnsPerfBlog/archive/2008/04/16/ntext-vs-nvarcharmax-in-sql-2005.aspx
powerCircuitList.Where(t => t.textProperty!= null && t.textProperty!= "")

Resources