SSRS conditional expression help to count distinct by group - reporting-services

I've got a report where I want to return the distinct count of [Above MRL]
e.g. Canola counts 13 being the sum of the number of times all the Sample IDs are [Above MRL].
Sample ID 330174 has a value of two, because this sample has been above MRL twice. I only want to count Sample ID 330174 once. Then roll this number up to the Canola level. Then I want to roll these products like almond, apples, canola to the plant level. So everything needs to be counted only once and summed.
How do I achieve this with an expression?

It sounds like you want to count the number of Sample IDs if the Sample Above MR is equal to 1.
=CountDistinct(IIF(Fields!AboveMRL.Value = 1, Fields!SampleID.Value, NOTHING))
This will check if the Sample if AboveMRL and, if so, count the ID. NOTHING is like NULL and is not counted in a Count Distinct calculation. If there are two of the same sample ID, it will only be counted once.

Related

Display value and number of duplicates in MySQL

I have a MySQL database row named ID and it goes
ID
18464762
3936573
3936573
3936573
374749502
374749502
374749502
374749502
374749502
3746325
9705732
9705732
9705732
9705732
476870382
476870382
3746574
37264
37264
And I want to make a MySQL Query that displays the information in two columns, the ID, and how many occurrences of the ID exist. That way I can sort it by number of occurrences.
The ideal output would be
id occurences
374749502 5
9705732 4
3936573 3
32764 2
476870382 2
18464762 1
3746325 1
3746574 1
This is just a small example as I have thousands of entries.
Everything I have already found from searching online tells me how to find which ids have duplicates, or the number of id's that have duplicates, but I have been unable to display the information like this.
Thank you in advance
You need to GROUP BY id, use aggregate function COUNT for counting occurences, and at the end order by second column.
SELECT id, COUNT(*) AS occurences
FROM your_tab
GROUP BY id
ORDER BY 2 DESC

MS Access count query does not produce wanted results

I have a table (tblExam) showing exam data score designed as follow:
Exam Name: String
Score: number(pecent)
Basically I am trying to pull the records by Exam name where the score are less than a specific amount (0.695 in my case).
I am using the following statement to get the results:
SELECT DISTINCTROW tblExam.name, Count(tblExam.name) AS CountOfName
FROM tblExam WHERE (((tblExam.Score)<0.695))
GROUP BY tblExam.name;
This works fine but does not display the exam that have 0 records more than 0.695; in other words I am getting this:
Exam Name count
firstExam 2
secondExam 1
thirdExam 3
The count of 0 and any exams with score above 0.695 do not show up. What I would like is something like this:
Exam Name count
firstExam 2
secondExam 1
thirdExam 3
fourthExam 0
fifthExam 0
sixthExam 2
.
..
.etc...
I hope that I am making sense here. I think that I need somekind of LEFT JOIN to display all of the exam name but I can not come up with the proper syntax.
It seems you want to display all name groups and, within each group, the count of Score < 0.695. So I think you should move < 0.695 from the WHERE to the Count() expression --- actually remove the WHERE clause.
SELECT
e.name,
Count(IIf(e.Score < 0.695, 1, Null)) AS CountOfName
FROM tblExam AS e
GROUP BY e.name;
That works because Count() counts only non-Null values. You could use Sum() instead of Count() if that seems clearer:
Sum(IIf(e.Score < 0.695, 1, 0)) AS CountOfName
Note DISTINCTROW is not useful in a GROUP BY query, because the grouping makes the rows unique without it. So I removed DISTINCTROW from the query.
Do I detect a contradiction? The query calls for results <0.695 but your text says you are also looking for results >0.695. Perhaps I don't understand. Does this give you what you are looking for:
SELECT DISTINCTROW tblExam.ExamName, Count(tblExam.ExamName) AS CountOfExamName
FROM tblExam
WHERE (((tblExam.Score)<0.695 Or (tblExam.Score)>0.695))
GROUP BY tblExam.ExamName;

Reporting Services How to get the Count() value from IIF

I have this table with a column named Open_Time which contain a datetime value. I would like to have another column named Total Ticket In June with the total count of ticket in June, so I've inserted the expression like below:
=Count((IIF(DatePart("m",Fields!Open_Time.Value,0,0) = "6",1,0)))
but there seems to be an error. To make myself clear, the table should look like this:
Assigned Name Ticket ID Open_Time Total Ticket in June
Ivan 001 3/28/2014 2
002 6/24/2014
003 6/11/2014
I would like to get value "2", which is the total number of ticket in June. Any idea? :)
You need to switch to a Sum aggregate instead of Count (based on your IIF, Count will count every row instead of giving you a total number of occurences):
=Sum(IIF(DatePart("m",Fields!Open_Time.Value,0,0) = 6, 1, 0))
From your table it's hard to tell your row groupings, but keep in mind that you'll only get the sum you expect if that expression is on a totals row (i.e. if you use that expression on the detail row it will simply list 1 or 0 for each date).
Try this:
=Count((IIF(DatePart(dateinterval.Month,Fields!Open_Time.Value,0,0) = 6,1,0)))

GroupBy and get percentage for each

I have my SQL table like this:
**CLIENTS:**
id
country
I want to echo a table with all countries I have with percentage fo each.
For example, if I have 2 Canadians and 1 French in my table, I want:
1 - Canada - 66%
2 - France - 33%
What I tried:
SELECT country FROM `mytable` GROUP BY `Country`;
It works, but how to have the percentage for each ?
Thanks.
You can use subquery:
SELECT
country,
COUNT(id) * 100 / (SELECT COUNT(id) FROM `mytable`) AS `something`
FROM
`mytable`
GROUP BY
`Country`;
You don't specify a falvor of SQL, but years ago microsoft posted their suggested solution:
select au_id
,(convert(numeric(5,2),count(title_id))
/(Select convert(numeric(5,2),count(title_id)) from titleauthor)) * 100
AS "Percentage Of Total Titles"
from titleauthor group by au_id
To calculate the percentage of total records contained within a group
is a simple result that you can compute. Divide the number of records
aggregated in the group by the total number of records in the table,
and then multiply the result by 100. This is exactly what the
preceding query does. These points explain the query in greater
detail:
The inner nested query returns the total number of records in the
TitleAuthor table: [ Select convert(numeric(5,2),count(title_id)) from
titleauthor ]
The value returned by the COUNT(title_id) in the outer
GROUP BY query returns the number of titles written by a specific
author.
The value returned in step 2 is divided by the value returned
in step 1, and the result is multiplied by 100 to compute and display
the percentage of the total number of titles written by each author.
The nested SELECT is executed once for each row returned by the outer
GROUP BY query
The CONVERT function is used to cast the values
returned by the COUNT aggregate function to the numeric data type with
a precision of 5 and a scale of 3 to achieve the required level of
precision.

ssrs count error getting 1's instead of total

Rookie mistake,
here it is:
I got Regions and MembersID, I grouped the regions and want to count the members. When I add the count(MembersID) instead of geeting the total members, I get a bunch of 1:
RegionA 1
1
1
RegionB
1
1
what I want:
RegionA
3
RegionB 2
what I did: Grouped Regions and Count[MembersID]
Please help!
It's a bit tough to tell what you actually need here.
In Group Header rows, you can use aggregate functions, which will look at all rows in that current scope.
Distinct MembersID count in group: =CountDistinct(Fields!MembersID.Value)
Total rows in group: =CountRows()
In detail rows, these aggregate functions will only report on the current rows, so =CountRows() will only ever return one; to get the number of rows in the group you need to set the scope of the function to the group level, e.g. something like =CountDistinct(Fields!MembersID.Value, "Group1") or =CountRows("Group1").
So based on a Dataset:
And a report which incorporates these aggregate function in the group header and detail rows:
You can get the following results:
If this is not sufficient you will need to supply your Dataset details, some sample data and how you need this sample data presented.