SSRS Count Distinct Expression into a Yes or No - sql

I'm wondering if there's a way or an expression I could use in SSRS to show a Yes or No based on a Distinct Count function?
Example of what I'm currently using - RM_Name is the group
=CountDistinct(iif(Fields!qtrtradecnt.Value >= 15, 1, Nothing), "RM_Name")
I also would like to show if the qtrtradecnt is >= 15 to show a Y or N based on the RM_Name group, and maybe countdistinct is not the correct function to use...I've messed with this for days.
Thank you!

One way to do this is to the calculation in your your dataset.. with a case expression..
select
...
,case when your_table.qtrtradecnt >= 15 then 1 else 0 end as rm_count
from your_table
Then in your report just do a
=iif(sum(Fields!rm_count.value) >15 then 'Y','N')

Related

Subquery Returns Muiltiple Values, but I am using "IN" as my operator

Long story short, I have a Crosswalk table that has a column name MgrFilterRacf. There is a single line in the table I use to reference various things across a few queries, this method has worked fine for me. However, when I had to reference more than one value it gives me the error below. I am really at a loss as to why it is not working. I see a TON of topics/posts on this, but most are solutions stating to use the IN operator, which I already am. The mildly infuriating thing is that it works when I only call one value and delete the second line on the crosswalk table regardless of "In" or "=" being used.
Error for reference:
Msg 512, Level 16, State 1, Line 2
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Crosswalk table format:
Id | AttCeilingScore | AttFloorScore | MgrFilterRacf
----+-----------------+----------------+---------------
1 | 100 | 75 | Value1
2 | NULL | NULL | Value2
Query:
--\\*Perform calculation For Attendance Score Sa*\\
Select
MgrRacf,
MgrName,
Ctc.racf as EmpRacf,
Ctc.EmpName,
Case
when AttCntSegment is null then 0
else AttCntSegment
end as AttCntSegment,
Case
when AttSumDuration is null then 0
else AttSumDuration
end as AttSumDuration,
case
when AttCntSegment > 12
then (Select AttFloorScore from tblAvs1Scoring)
when AttCntSegment is null
then (Select AttCeilingScore from tblAvs1Scoring)
when 100 - ((AttCntSegment) * 2 + PercentReduction) < (Select AttFloorScore from tblAvs1Scoring)
then (Select AttFloorScore from tblAvs1Scoring)
else 100 - ((AttCntSegment)*2+PercentReduction)
end As AttScore,
Case
when AttCntSegment is null then 100
else 100 - ((AttCntSegment)*2+PercentReduction)
end as AttScoreRaw,
'RollSixSum' as ReportTag
From
(--\\*Get Total Occurrences from Rolling 6 months per advocate*\\
SELECT
EMP_ID,
COUNT(SEG_CODE) AS AttCntSegment,
SUM(DURATION) AS AttSumDuration
FROM
tblAttendance AS Att
WHERE
(START_DATE >= Getdate() - 180)
AND (SEG_CODE NOT IN ('FLEX2', 'FMLA'))
AND (DURATION > 7)
AND START_DATE IS NOT NULL
GROUP BY
EMP_ID) As Totals
INNER JOIN
tblCrosswalkAttendanceTime AS Time ON AttSumDuration BETWEEN Time.BegTotalTime AND Time.EndTotalTime
RIGHT JOIN
tblContactListFull AS Ctc ON Ctc.employeeID = Totals.EMP_ID
WHERE
--Ctc.Mgr2racf IN ('Value1','Value2') --This works
Ctc.Mgr2racf IN (SELECT MgrFilterRacf FROM tblAvs1Scoring) --This returns the same 2 values but doesn't work, note works with only 1 value present
AND (title LIKE '%IV%' OR Title LIKE '%III%' OR title LIKE '%Cust Relat%') --Going to apply same logic here once I have a solution
AND employeestatus2 <> 'Inactive'
Specific offending lines of code:
Ctc.Mgr2racf IN (Select MgrFilterRacf from tblAvs1Scoring) --This returns the same 2 values but doesnt work, note works with only 1 value present
AND (title LIKE '%IV%' or Title like '%III%' or title LIKE '%Cust Relat%') --Going to apply same logic here once I have a solution
It's not the IN clause that’s causing the error, since IN doesn't have to return only 1 value. Instead it's likely here:
CASE ... < (Select AttFloorScore from tblAvs1Scoring)
Try select count(AttFloorScore) from tblAvs1Scoring and see if it's > 1. I am sure it is.
This can be mitigated by what ever method is appropriate for your data.
Using TOP 1
Using an aggregate like MAX()
Correlating the sub-query to limit the rows returned.
Using the appropriate WHERE clause
The error was not where I thought it was, it was at the top of the query in those sub queries. Even though there as no values present, it still needs to be told since I added a line to return the max value. Once I updated that all was well and worked.
Bottom line, check all your sub queries.

SUM Query on sql server

I am newbie, I am very confused how to do sum query. Here is my query :
and this is an output from my query :
all i want is Sum my presentase field by month..
i already created sum query, but all i gonna got by my query just error code :((( ..
here my sum query :
SUM(IF( YEAR(PO_HARI) = 2016, PRESENTASE_SATUAN, 0)) AS TOTAL_JANUARY
and also i added TOTAL_JANUARY On my group_by but didnt work.
thank you for attention .
try this
select year(po_date) yeardate, month(po_date) monthdate,
sum(case Point_del
when 1 then 25
when 2 then 50
when 3 then 75
when 4 then 100
else 0
) TOTAL_JANUARY
from jbox2_po..POT01A
where month(po_date)=1
group by year(po_date) , month(po_date)
you can add 'and year(po_date)=2016' into clause where if you want only 2016 year
IF youre using sql server management studio then try this :::
select sum(presentase) from tablename
group by month(PO_HARI)
--here i am assuming that "presentase" is a summable field and 'po_hari' is a date column

finding range by comparing two tables

I have a table in database as "EXPERIENCE RANGE" with rows as (I can also edit this table according to my need)
0
0.5
1
2
3
5
10
20
I have total experience as integer. I need to display the range in which it lies.
Example - for experience of 8, Range will be 5 - 10
I need to write a sql query. Any ideas will be quite helpful as I am new to SQL.
I cannot hard code it..need to take values from tables only.
Assuming that you are using Oracle, the following query works fine with your existing table:
SELECT
( SELECT MAX( value ) FROM experience_range WHERE value <= :search_value ) AS range_start,
( SELECT MIN( value ) FROM experience_range WHERE value > :search_value ) AS range_end
FROM dual;
No need to hardcode the values, and no need to store the lower and upper bounds redundantly.
you can do it with CASE Expression, the syntax is:
SELECT
CASE
WHEN experience >= 0 and experience <= 4 THEN '0-4'
WHEN experience >= 5 and experience <= 10 THEN '5-10'
.....
ELSE 'No Range'
END as Range
FROM Table_Name
If you do need to store the ranges in a table, I would personally suggest altering the structure of the range table (Assuming you are able to), maybe something like:
|--------------------------------------|
|ID|DESCRIPTION|LOWER_LIMIT|UPPER_LIMIT|
|1 | 0 - 0.5 | 0 | 0.5 |
|2 | 0.5 - 1 | 0.5 | 1 |
...
Then you could get your range by running something like:
SELECT DESCRIPTION FROM [RANGES] WHERE <VALUE> >= LOWER_LIMIT AND <VALUE> < UPPER_LIMIT
EDIT - Mikhail's answer also works, defining the ranges within the query itself is also an option and probably simpler providing you don't need to get these ranges from several reports. (That would require editing every report/query individually)
EDIT 2 - I see you are not able to hardcode the ranges, in which case the above would be best. Can I ask why you are unable to hardcode them?

SQL COUNT between dates in two different column

Let's say, we have this table:
STUDENT | START | END
1 |1998-1-1 |2001-1-1
2 |1999-1-1 |2001-1-1
3 |2000-1-1 |2004-1-1
4 |2000-1-1 | NULL
I'm trying to do is:
Count number of students between start and end dates!
Looks like you need to use a basic COUNT aggregate:
SELECT COUNT(Student)
FROM YourTable
WHERE Start >= #Start
AND End <= #End
I've used >= and <= respectively around the start and end date fields. Feel free to change to > or < as needed. It was unclear from your question whether you wanted between a specific field or if you were checking for a range between those two fields.
Use the between Operator and COUNT aggregate function
SELECT COUNT(student) column_name(s)
FROM table_name
WHERE column_name
BETWEEN value1 AND value2
Between can be used with text so insert the dates where the values are,
Read more here if you still don't understand
EDIT : That should work, sorry about the error
http://www.w3schools.com/sql/sql_between.asp

How can I order results which have some value equals zero in the end, while other results are ascending?

Let me explain: I need to sort a list in ascending order, while results leave less than zero at the end.
Example:
**Field**
2
5
15
19
0
-5
-19
I think I can join result of two queries using UNION, but I want to do using only one, is it possible?
Any answer telling how to order thay way will be appreciated.
Use the following ORDER BY at the end of your normal query (no union)
order by (case when field>0 then 0 else 1 end), field
Or, if your database system's SQL flavor supports implicit conversion of booleans to integers:
order by (field <= 0), field