I am trying to distinct count all values in the Incident_ID column where a number of IIF statements are met. The SSTS expression is running but the result is far from correct. When using SQL on SSMS, I am getting totally different results which makes me feel my expression is incorrect. I am also looking at the data and the data backs-up my expression isn't right.
Kindly see below. Many thanks.
=CountDistinct(IIF(Fields!Last_month.Value = 1
AND Fields!Clinic_group.Value = "AAA"
AND Fields!Incident_approval_Flag.Value = 1
AND (Fields!Incident_severity_code.Value <> "BBB"
AND NOT ISNOTHING(Fields!Incident_severity_code.Value))
,Fields!Incident_ID.Value, Nothing)
)
Related
I've written a query that attempts to return a record from a table. It will either return 1 record or nothing.
I've then written a second query and am trying to check whether the first query returned a record or not.
In order to do this I'm trying to use the IsNull function on the primary key of the record in the first query.
I would expect that when the query 1 returns a record, query 2 will return false and when query 1 returns nothing, query 2 will return True.
Instead, when no record is returned by query 1, query 2 returns nothing, just a blank field - why is it not returning True? This is the SQL for query 2:
SELECT IsNull([query 1].fieldA) AS NullorNot
FROM [query 1];
NULL is an undefined column value WITHIN A RECORD. It is still a column value. A query not returning anything has no columns, no values, so has nothing to do with NULL.
I don't think that your on the right track and should rethink your solution (like just testing for the number of records in whatever code you are using the query), but if this is actually what you need, it is this:
SELECT NOT EXISTS(SELECT * FROM [query 1]) AS NullorNot;
OR
SELECT NOT EXISTS([the full SELECT sql of query 1]) AS NullorNot;
I have came across one pretty problem in which I need to convert the nested if statement (like we used to write in excel) to dynamic SQL query.
I want to convert this
select 'IIF(A>B,A,IIF(B>C,B,C))'
to like this
select 'CASE WHEN A>B THEN A ELSE CASE WHEN B > C THEN B ELSE C END END'.
Why I am doing this I have formula column in the table user can insert formula in that and I need to convert this to SQL statement.
Do anyone has idea how can I do that?
The above is for only example I will be having 150 rows for different formula's entered by user.
Note : Here do not interpret IIF as SQL server IIF, it can be IF.
Assuming that A,B,C are numbers we can do something like this
SELECT
(
Select MAX(cols)
from
(VALUES (ColA), (ColB), (ColC))tbl(cols)
) as greatest
from yourtable
I have an SQL query to get results from my database table. When I run this query in Jasper Studio it should bring me following.
Nocte & Mane are boolean type colomns in the table. If the Nocte column is true then, it could show as a string ("Nocte") and this should same for the mane.
SQL Query
select * from medication where $X{IN,idmedication,list} order by `LastUpdated` desc
This is the query I am using in Jasper report and idmedication and list are parameters. LastUpdated is a timestamp. This list includes Integer values, which are the idMedications.
Have any ideas to do above task in Jasper Studio?
I can think of two approaches at the moment:
Use expressions when you design the report, it could be something like ($F{Nocte}?"Nocte":"something else")
Modify the query so that it returns varchar instead of boolean. For example:
select case when Nocte = 1 then "Nocte" else "something else" end Nocte
from your_table ...
the solution is something like bellow
(
${your_filed_name}==true?"Nocte"":"mane"
)
Hope this answer your question. And you can chain the condition or get into more complex expression.
I am developing an SSRS report and am having trouble with an expression. I want to apply 1 IIF statement with 2 conditions to a cell in a table. I am correctly do this, but the first expression only gives me the First value in the dataset. I took out the 'First' in the expression and tried to apply the RunningValue but i am getting stuck. I get an #Error in the cell. It just says #Error. How to apply the same IIF statement but loop through all the values in the dataset?
Can anyone shed some light on what I am doing wrong? All I am trying to do is apply a statement and if it meets the conditions then return only the value without summing. Any help is most appreciated.
First Script that works only for the first value.
=IIF(First(Fields!Title.Value, "getData")=1 And First(Fields!Day.Value, "getData")=DateAdd("d",-6,Parameters!WeekEnding.Value),Fields!Value.Value, "")
Second script trying to apply the RunningValue
=RunningValue(IIF(Fields!Title.Value=1 And Fields!Day.Value=DateAdd("d",-11,Parameters!WeekEnding.Value),Fields!Value.Value, ""),Sum,"getData")
Dataset query:
SELECT
a.PkId, a.EmpId, a.WkEnd, a.Day, a.Title, a.Description,
a.Value as Val
FROM
EmployeeTimeSheets a
INNER JOIN
Employees b ON a.EmpId = b.RowID
WHERE
Employee = #SelectEmployee
AND (WkEnd >= #WeekEnding OR WkEnd <= #WeekEnding)
DataSet
SQL table
SSRS table
Desired results
I am trying to Sum() the column Status where Status = 'operational'. I am having trouble figuring out how to sum the actual word "operational".
I have tried multiple different variations of the statement below (the one I posted is the most basic form) but I get the error: data type varchar is invalid for sum operator.
Can anybody help?
SELECT SUM(status) As 'TotalOperationalSTIDevices'
FROM netinfo_device_details
WHERE LoopBackAddress LIKE '10.12%' AND Status = 'Operational'
Try
Select COUNT(*) As 'TotalOperationalSTIDevices' from netinfo_device_details where LoopBackAddress Like '10.12%' and Status = 'Operational'
You need to use COUNT:
SELECT COUNT(*) As TotalOperationalSTIDevices
FROM netinfo_device_details
WHERE LoopBackAddress LIKE '10.12%' AND Status = 'Operational';
The SUM aggregation function really does a SUM of a set of numbers. COUNT just counts the number of rows.
Since the actual content of the row is not relevant, you can use COUNT(*) instead of COUNT(status) if you want.