SSRS - Count total numbers of each categorie within same column - reporting-services

Is there a certain way an expression can be written in SSRS which counts how many times a certain product occurs in one column?
Table
Table
Table
Chair
Chair
Cup
Chair
Chair
So at the end we can have something that displays
Table: 3
Chair: 4
Cup: 1
I do want to mention though, that I don't want to hardcode the values to be counted, (Chair, Cup, Table).
The report should look like the below having the Summary Section and below (what I already have is the actual report)
Thank you

Can you not just group by product and sum?
This is the official documentation.

I just wanted to update that i was able to somewhat accomplish what was needed using the Table Wizard.
To count the rows, I used the following expression: =count(Fields!Item_Types.Value) it did however require me to have all the parts listed again to perform a count. For now, I just contracted / hid all those rows.

Related

MySQL subquery with if conditionals

I'm querying a table from a third party plugin so I don't have control over how the data is being inputted. It's a course plugin broken into 4 different quizzes. One of the questions is being used in all four quizzes.
THERE IS NO "quiz_id" which is why I think they only way to query data is with some sort of if conditional. There IS a date field and a unique id field.
This is what my subquery looks like:
(SELECT y.post_content
FROM wp_posts AS y
WHERE 137 = y.post_author
AND y.post_title LIKE '%If you checked "Other" in "What are your organization’s primary goals related to hiring people with autism?", please explain%'
ORDER BY y.post_author ASC limit 1)
This works to query the answer (y.post_content) for the first quiz, but not for all 4 quizzes. Is there a conditional I can use for this? i.e. let's say i'm querying results for the 2nd quiz: if there are four answers, pick the 2nd one,if there are 3 answers pick the 2nd one, if there are 2 answers, pick the most recent one

Adding values to a Report when there is no Data in query SSRS

I have a query that returns Sales representatives number, Category, Sales.
The result is something like this:
There are 4 categories called G1,G2,G3,G4.
As you can see the Sales representative 11 sold 10 each category (Yellow rows).
But Representative 12 sold only for category G3 and G4.
The idea is to show in the report all the categories and populate with 0 all those who did not sell on that particular category.
It must be grouped by Sales Representative so if you make a tablix grouping by Sales Representatives you will have something like this:
But you want something like this:
Is there any expression I could use to add these?
What I did so far is to create a group, that group of course are my Sales representatives and combine the cells for that Column and created a Row group for each category, is something like this:
But if you execute that report it will repeat all categories G1,G2... For each time that category exists for that particular Sales Representative.
Another problem is, how can you evaluate The hardcoded category in your report if it does not exist in your datasource you cant make Iif("G1" = Fields!Category.Value,Fields!Sales.Value,"0") as you are not comparing G1 with Null or IsNothing, you are comparing what it exists.
I think you can achieve this smoothly using T-SQL at query level. I don't know why you don't use the simplest way to apply this kind of logic since in T-SQL you can use almost every logic.
However I like this kind of challenges so I come with this possible solution.
This is my sample dataset:
In SSRS dataset (not in T-SQL) I've added a calculated field called Another
Another field is set to the below expression:
=Fields!SalesRep.Value & "-" & Fields!Category.Value
I've added a tablix with the following data arrangement
As I mentioned before category field is hardcoded, the right column with Sales
is set to this expression:
=iif(IsNothing(lookup(Fields!SalesRep.Value & "-" & ReportItems!Textbox62.Value,
Fields!Another.Value,Fields!Sales.Value,"DataSet7")),0,
lookup(Fields!SalesRep.Value & "-" & ReportItems!Textbox62.Value,
Fields!Another.Value,Fields!Sales.Value,"DataSet7"))
Note: ReportItems!Textbox62.Value corresponds to textbox where G1
was hardcoded. You have to replace the textbox reference for the
corresponding in your tablix for every category.
It will preview the below tablix.
Let me know if this was helpful.

SSRS report - add extra table and chart to show occurence of character string in one of the main report's columns

I have an SSRS report based on stored procedure dataset. The report shows employees and their performance rating and uses bunch of parameters to filter the data.
Now I would like to add a table below that would dynamicaly count and show occurence of given mark in the main report. The table data should update according to what is visible in the main report after filtering it.
I wanted also to add a chart that would visualize this.
It would be feasible to do if the extra table and chart could run from the same dataset as the main report. This however seems impossible as this dataset does not always contain all the possible marks. It can happen that some marks are missing (when filtered or missing at all), and I would like to show the mark with zero value (and zero value bar in the chart) instead of just skipping it.
So far I was able to produce the table by hardcoding the headers and using SUM(IIF...) expressions under each header
Here is the expression for the "C" column.
=Sum(IIf(Fields!current_performance_rating.Value = "C", 1, 0))
It shows correctly the number of "C" marks appearing in the main report.
Now I am stuck with creating a chart that would show this.
I am not able to hardcode expressions similar to the ones in the table
and can't make the chart run from the main dataset, because the categories
would be missing after filtering the report (and not showing zero).
I tried linking datasets with Lookup function, but that did not work.
Which way should I go now? What is the best practice in such case?
Thank you for any hints!
Thanks trubs.
I have right joined a view that contains
all the marks and that solves the issue
of missing categories.
The join is on something like
tb.current_performance_rating = vw.performance_rating_code
I can now add the value series that counts
the occurence of current_performance_rating
per category. This works all fine.
However there is another table joined
(on employee_id) that stores last year's rating.
This rating obviously may differ to the current one.
On the same chart I would like to add another
series that counts last year's rating per category.
The category is there already, joined to the current
rating.
So you can have row like:
curren rating | last year's rating | category
C | H | C
So I am stuck, because when SSRS groups per category
it counts last year's H rating and displays
i the C category, while it should display it in H.
Sorry I can't post any pictures, seems like I
need more reputation points.
Hope you can understand what I mean.
Regards!
You're likely better off changing your query to return results for marks where there are no values.
So instead of inner joining to your Performance Rating table, use a Right Join or Full Outer Join, so that all the data is always available.
eg: Instead of...
SELECT p.PersonId, PersonName, g.Grade
FROM Person p
INNER JOIN PersonGrades pg ON p.PersonId = pg.PersonId
INNER JOIN Grades g ON pg.GradeId = pg.GradeId
Use
SELECT p.PersonId, PersonName, g.Grade
FROM Grades g
LEFT JOIN PersonGrades pg ON pg.GradeId = g.GradeId
LEFT JOIN Person p ON p.PersonId = pg.PersonId
If that's not clear, post your query and we could help get the data in the right format.
The way to go was to simplify the dataset that the main report was based on.
Then I took the query from that dataset, used COUNT and GROUB BY to count number of occurence of each current year mark in the main report. Then I right joined (thx trubs!) with the view that contains all the possible marks (so that my extra table headers/chart categories would not disappear if they do not exist). As a result I got the number of occurence in current year per mark (table A).
Then I did almost exactly the same for the last year's rating, just used last year's mark. I got the number of occurence of last year's mark per mark (table B).
I inner joined table A and B on common column (the mark, which will always appear thanks to the RIGHT joins). This gave me a table (dataset eventualy) where I had:
mark | current year mark count | last year mark count.
Making a table and chart basing on this was really easy then.
There was some more fun of adding all the parameters from the main report to both the count queries, so that the counts would change when report is filtered. I also needed to make sure that count works not only when my filter criteria (in WHERE stetement) equal the parameter provided from the report, but also when they are NULLs (so I added OR column_i_filter_on IS NULL).
This works smoothly - the table content and chart changes when filtering changes (although runs slowly, as parameters are passed to two big dataset, one of which uses them twice).
Thanks for all the help!!
psh

MYSQL Group By Returning Duplicate Values

I am seeing a weird problem with MYSQL GROUP BY.
I have a query...
SELECT schools.schoolregion,
Count(schools.schoolregion) AS regioncount,
(
SELECT Count(jobs_jobsubject)
FROM 'jobs'
WHERE 'jobs_createdDate' BETWEEN '$startofyear'
AND '$endofyear') AS regionjobstotal
FROM 'jobs'
LEFT JOIN 'schools'
ON 'jobs_schoolID'='SID'
WHERE 'jobs_createdDate' BETWEEN '$startofyear'
AND '$endofyear'
GROUP BY 'schoolRegion'
...in which I am attempting to total the number of job postings listed per region and group by region. I have two tables, one with a list of schools and another with job information that has a column value that joins back to the school. I need the region total, and the overall total of jobs within a time period (hence the sub query).
When I run this query, I get everything that I expect - except that I am getting a duplicate region listing in the returned results of the GROUP BY function.
For example, here is the table that I am getting but not sure why the duplicate for the Middle East.
schoolRegion regioncount regionjobstotal
Africa 1 38
Asia 6 38
Middle East 20 38
Middle East 11 38
I thought maybe there was an extra character or something, but I could not find/see anything different about the values within the tables - which for that column is being stored as type "text". Is there anything I can check for? Is it something to do with the query?
Any help would be fantastic and much appreciated!!
My guess is that the data is not ordered by schoolRegion. I would add an ORDER BY schoolRegion ASC to your query to ensure that they are organized thusly. :)
OMG, do I feel like a noob!!
When I adjusted the query to list the schools, there was only one school that was not included in the GROUP BY. Initially when I looked at this hours ago, inline editing in PHPMYADMIN didn't show that there was a character return AFTER the text - so I wrote off that it was the text of the value being stored. But when I checked the box to edit the row individually and not inline and went to that column value - low and behold - a carriage return!!! Sometimes it's the little things like that which kill and humble me.
First, i do not think you can supply a child select statement as a column in your parent select statement "(SELECT COUNT(jobs_jobSubject)...".
Also since the where clause for your child and parent select are thesame, why not use a single select statement and get the count of both.
SELECT schools.schoolRegion,
COUNT(schools.schoolRegion) AS regioncount,
COUNT(jobs_jobSubject) AS regionjobstotal
FROM 'jobs' jb
INNER JOIN 'schools' sc ON jb.jobs_schoolID=sc.SID
WHERE 'jobs_createdDate'
BETWEEN '$startofyear' AND '$endofyear'
GROUP BY 'schoolRegion'

Find first, second, third, and so forth record per person

I have a 1 to many relationship between people and notes about them. There can be 0 or more notes per person.
I need to bring all the notes together into a single field and since there are not going to be many people with notes and I plan to only bring in the first 3 notes per person I thought I could do this using at most 3 queries to gather all my information.
My problem is in geting the mySQL query together to get the first, second, etc note per person.
I have a query that lets me know how many notes each person has and I have that in my table. I tried something like
SELECT
f_note, f_person_id
FROM
t_person_table,
t_note_table
WHERE
t_person_table.f_number_of_notes > 0
AND t_person_table.f_person_id = t_note_table.f_person_id
GROUP BY
t_person_table.f_person_id
LIMIT 1 OFFSET 0
I had hoped to run this up to 3 times changing the OFFSET to 1 and then 2 but all I get is just one note coming back, not one note per person.
I hope this is clear, if not read on for an example:
I have 3 people in the table. One person (A) has 0 notes, one (B) with 1 and one (C) with 2.
First I would get the first note for person B and C and insert those into my person table note field.
Then I would get the second note for person C and add that to the note field in the person table.
In the end I would have notes for persons B and C where the note field for person C would be a concatination of their 2 notes.
Welcome to SO. The thing you're trying to do, selecting the three most recent items from a table for each person mentioned, is not easy in MySQL. But it is possible. See this question.
Select number of rows for each group where two column values makes one group
and, see my answer to it.
Select number of rows for each group where two column values makes one group
Once you have a query giving you the three rows, you can use GROUP_CONCAT() ... GROUP BY to aggregate the note fields.
You can get one note per person using a nested query like this:
SELECT
f_person_id,
(SELECT f_note
FROM t_note_table
WHERE t_person_table.f_person_id = t_note_table.f_person_id
LIMIT 1) AS note
FROM
t_person_table
WHERE
t_person_table.f_number_of_notes > 0
Note that tables in SQL are basically without a defined inherent order, so you should use some form or ORDER BY in the subquery. Otherwise, your results might be random, and repeated runs asking for different notes might unexpectedly return the same data.
If you only aim for a concatenation of notes in any case, then you can use the GROUP_CONCAT function to combine all notes into a single column.