How to find the most frequently occurring value in SSRS tablix dataset? - sql-server

Consider following dataset that is displayed in tablix in SSRS report:
GroupID | ProductID
---------------------
Group 1 | Product1
Group 2 | Product10
Group 1 | Product2
Group 3 | Product27
Group 2 | Product12
Group 2 | Product14
I added new row via Insert Row/Outside Group - Below.
On this row I display total number of rows - achieved via CountRows(), number of distinct Groups - achieved via =CountDistinct(Fields!GroupID.Value)
I also want to display the name of the group with the most number of rows, in this case it would be "Group 2" (in case if there is more than one group with the same number of rows I only need to display one of them).
How can this be achieved? I think I should use some of aggregate or lookup functions but so far can't figure out how.
PS This report is being ported from Crystal Reports to SSRS. In Crystal Reports this is easily achieved via "Nth most frequent" summary with N=1 but there is nothing like this in SSRS as far as I can tell.

Add a tablix and set GroupId as Row Group.
For Rows Count use:
=Count(Fields!GroupID.Value)
Right click GroupID group in the Row Groups pane and go to group properties, in the Filters tab use the following settings:
For Expression use:
=Count(Fields!GroupID.Value)
It will filter the top 1 group with the greatest Rows count. The result is something like this:
UPDATE: The previous solution doesn't work if there is more than one group with the same number of occurencies.
Add a tablix, delete the details (default group) and add the GroupID field in the first column.
For the Rows Count column use the following expression replacing DataSetName by the actual name of your dataset:
=LookupSet(
Fields!GroupID.Value,
Fields!GroupID.Value,
Fields!GroupID.Value,
"DataSetName"
).Length
Right click your tablix and go to tablix properties, in the Sorting tab select Z to A order and use the previous expression in the Sort By textbox.
It should show the only one group even if a second group with same number of occurencies is present.
Let me know if this helps.

Related

Report Builder Multiple Group Matrix Calculation

I have a Matrix created using Matrix Wizard in Report Builder 3.0(2014), having 2 row groups ,1 column groups and 2 values. After I create the matrix (included total and subtotal), I have a matrix that look just nice. But now I want to add one more cell for each columns groups (one row), to store the below value.
Value = Total of 1st row group + Total of 2nd row group - Total of 3rd row group ...
The matrix built just show me the subtotal of each row group which I don't need.
I want to ask how do I retrieve the result of total calculated by matrix itself and how do I identify them based on their row group value using expression? And also, how do I do this for every column groups which have different data?
I tried to look at the expression in design view of the matrix, it just shows [SUM(MyField)] for every cell in the Matrix (total & subtotal).
Or should I do it at another dataset using another query? If so, what query should I use and how do I put two dataset into one matrix?
My Matrix looks something like this :
Column Group
ROW GROUP 1 | ROW GROUP 2 | VALUE 1 | VALUE 2
Row Group 1 | Row Group 2 | [Sum(MyField)] | [Sum(MyField)]
| TOTAL OF ROW GROUP 1 | [Value] | [Value]
| ROW PLAN TO ADD | [Value(0)+Value | [Value(0)+Value
| (1)-Value(2)] | (1)-Value(2)]
CAPITALS : Column name, constant
[sqrbrkted] : Calculated Value
Normal : data inside table
I am new to Report Builder, sorry if I made any mistake. In case I didn't make myself clear, please do comment and let me know. Thank you in advance.
EDIT: I have figured out an approach to achieve my purpose at the answer section below. If anyone have other solution, please feel free to answer it. Thanks.
I solved this problem by changing my SQL query to make another dummy column which shows negative result if its respective row group is meant to deduct the subtotal (Value(2)), and place it inside the matrix. And inside the expression, I have another IIF statement to convert it back to positive for display purpose.
SQL:
SELECT *, (CASE WHEN COL_B = 'VAL_2' THEN -COL_A ELSE COL_A END)AS DMY_COL_A FROM TABLE
Expression:
=IIF(Sum(Fields!DMY_COL_A.Value)<0,-Sum(Fields!DMY_COL_A.Value),Sum(Fields!DMY_COL_A.Value))

Display 240 Pictures, 9 per Page in SQL

I have the code bellow:
SELECT *,
(ROW_NUMBER() OVER (ORDER BY person_id) -1 )/3 AS RowGrpNo,
(ROW_NUMBER() OVER (ORDER BY person_id) -1 )%3 AS ColGrpNo,
(ROW_NUMBER() OVER(ORDER BY person_id)-1)/9 AS PageGrpNo
FROM
(
SELECT DISTINCT
People.person_id,
People.ActivePassive,
PeoplePicture.person_id,
PeoplePicture.picture_id,
PeoplePicture.Picture
FROM People
Right Join PeoplePicture
On People.person_id = PeoplePicture.person_id
WHERE People.ActivePassive = 'Active'
)t​
In addition I have 240 images. I want to display them in SSRS (9 images per page). But I only get the first three as shown below:
Sample picture
How do I make the remaining 6 pictures visible, they where suppose to be where there are boxes in red. Please help.
Bellow is the report in report builder 3.0:
It can be achieved by doing the following:
Modify your inner select to include a rownumber column. This is simply to number your rows from 1 through to how many ever is returned in your dataset.
On your outer select, add another column (lets call it row_display).. set this to be rownumber % 3 -- since you want 3 images per line.
Now in your report designer, place 3 tablix of the same dimensions side by side to hold the photos
Lets call them tablix1, tablix2 and tablix3
they all display the same data.. except, each tablix has a different set of filters.
On tablix1, add a filter and set row_display = 1
On tablix2, add a filter and set row_display = 2
On tablix3, add a filter and set row_display = 0
You may need to adjust the size of the tablix depending on how many images are returned per page..
Preview the report and watch the magic happen.. hopefully.
Additionally. if you did another rownumber and call it pagebreak.. set this pagebreak column to increment by 1 every 9 rows .. you can then group all the tablix by the pagebreak column add a pagebreak between each instance of the group.. you would get 3 images across and 9 lines of images..

Getting top 20 rows and the rest in 21st row in an SSRS Matrix Report

I want top 20 rows from an SSRS Matrix report and rest of the rows should be aggregated in 21st row with the row name hard coded as “Others”.
I have created the SSRS Matrix Report with row grouping based on "Category_Name" and Column grouping based on "Creation_Time". Column Group "Creation_Time" is formatted to show date in "M/yyyy" format. I am aggregating the “Id_Number” as Count(Id_Number) for each grouping and I want top 20 records sorted by count(Id_Number) in highest to the lowest order or descending order.
I have set the visibility condition as showed in the image:
I have given the following expression for the row "Others"
=count(iif(RunningValue(Fields!Category_Name.Value,countdistinct,nothing)>20,Fields!Id_Number.Value,0)
But it didn't work. I got the error saying "Running functions cannot be specified as nested aggregates".
I have earlier tried using TOP N filter on SSRS Row grouping based on count(Id_Number) which gives Top 20 rows.But if we add the " group total" to the row grouping, it will give the total of all rows and not just the top 20 rows. The logic that I thought of was to subtract the total of top 20 rows from the total of all rows. But I am not able to get the total of top 20 rows.
I used basic sql query with 2 parameters, #Begin AND #End :
SELECT Category_Name, Id_Number, Creation_Date
FROM Tbl
WHERE Creation_Date BETWEEN #Begin AND #End
The desired output of this report is this:
I am getting everything as per the desired output except for the last row "Others".
Amend your sql select script to include the following:
CASE WHEN ROW_NUMBER() OVER (ORDER BY SUM(Id_Number)DESC) >20 then 'Top 20' else 'Others' End AS 'Type'
Then in your report add in another separate tablix that is filtered by "type" = "others"

How to show data in column in SSRS

I'm using SSRS for my reporting, my reporting solution is in Visual Studio 2008 Business Intelligence Development Studio.
I have a report in which the data should be displayed in this format.
I have added a Column Group in my table which is having the values of Customer Name and details, the data is coming fine in the vertical format i.e column after column.
My Issue :
There should be only three columns in each row, after three records the next row should begin and again not more than three records should be displayed as shown in the image above.
My attempts :
I tried to add a row group and in that gave the expression
= Ceiling(Fields!Row_Count.Value/3)
here Row_Count is a field which is coming from my query which holds the serial number of the records.
My SQl Query
SELECT Row_Number() over(order by table_ID) AS Row_Count, Field_1,Field_2 from MyTable
In my Column group i have Customer Name and in my Row Group i have other details of the customer. The data is getting populated column wise but the issue is its not breaking the current row after three records.
Below is my table of report.
You were on the right track. Say you have data like this:
I have created a tablix like this:
The Row Group expression is:
=Ceiling(Fields!Row_Count.Value / 3)
This works together with the Column Group expression to split over three columns:
=(Fields!Row_Count.Value - 1) Mod 3
The other thing to note compared to your tablix is that CustomerName is not in a table header row, but rather there are two row header rows, one for CustomerName and one for Details.
This is looking OK to me, obviously you can format to taste:

Outputting Results from complicated database structure (SQL Server)

This will be a long question so I'll try and explain it as best as I can.
I've developed a simple reporting tool in which a number of results are stored and given a report id, these results were generated from a particular quote being used on the main system, with a huge list of these being stored in a quotes table. Here are the current batch:
REPORTS
REP_ID DESC QUOTE_ID
-----------------------------------
1 Test 1
2 Today 1
3 Last Week 2
RESULTS
RES_ID TITLE REFERENCE REP_ID
---------------------------------------------------
1 Equipment Toby 1
2 Inventory Carl 1
3 Stocks Guest 2
4 Portfolios Guest 3
QUOTE
QUOTE_ID QUOTE
------------------------------------
1 Booking a meeting room
2 Car Park Policy
3 New User Guide
So far, so good, a simple stored procedure was able to pull all the information necessary.
Now, the feature list has been upped to include categories and groups of the quotes. In the Reports table quote_id has been changed to group_id to link to the following tables.
REPORTS
- REPORT_ID
- DESC
- GROUP_ID
GROUP
- GROUP_ID
- GROUP
GROUP_CAT_JOIN
- GCJ_ID
- CAT_ID
- GROUP_ID
CATEGORIES
- CAT_ID
- CATEGORY
CAT_QUOTE_JOIN
- CQJ_ID
- CAT_ID
- QUOTE_ID
The idea of these changes is so that instead of running a report on a quote I should now write a report for a group where a group is a set of quotes for certain occasions. I should also be able to run a report on a category where a category is also a set of quotes for certain departments. The trick is that several categories can fall into one group.
To explain it further, the results table has a report_id that links to reports, reports has a group_id that links to groups, groups and categories are linked through a group_cat_join table, the same with categories and quotes through a cat_quote_join table.
In basic terms I should be able to pull all the results from either a group of quotes or a category of quotes. The query will aim to pull all the results from a certain report under either a certain category, a group or both. This puzzle has left me stumped for days now as inner joins don't appear to be working and I'm struggling to find other ways to solve the problem using SQL.
Can anyone here help me?
Here's some extra clarification.
I want to be able to return all the results within a category, but as of right now the solution below and the ones I've tried always output every solution within a description, which is not what I want.
Here's an example of the data I have in there at the moment
Results
RES_ID TITLE REFERENCE REP_ID
---------------------------------------------------
1 Equipment Toby 1
2 Inventory Carl 1
3 Stocks Guest 2
4 Portfolios Guest 3
Reports
REP_ID DESC GROUP_ID
-----------------------------------
1 Test 1
2 Today 1
3 Last Week 2
GROUP
GROUP_ID GROUP
---------------------------------
1 Standard
2 Target Week
GROUP_CAT_JOIN
GCJ_ID GROUP_ID CAT_ID
----------------------------------
1 1 1
2 1 2
3 2 3
CATEGORIES
CAT_ID CAT
-------------------------------
1 York Office
2 Glasgow Office
3 Aberdeen Office
CAT_QUOTE_JOIN
CQJ_ID CAT_ID QUOTE_ID
-----------------------------------
1 1 1
2 2 2
3 3 3
QUOTE
QUOTE_ID QUOTE
------------------------------------
1 Booking a meeting room
2 Car Park Policy
3 New User Guide
This is the test data I am using at the moment and to my knowledge it is similar to what will be run through once this is done. In all honesty I'm still trying to get my head around this structure.
The result I am looking for is if I choose to search by group I'll get everything within a group, if I choose everything inside a category I get everything just inside that category, and if I choose something from a category in a group I get everything inside that category. The problem at the moment is that whenever the group is referenced everything inside every category that's linked to the group is pulled.
The following will get the necessary rows from the results:
select
a.*
from
results a
inner join reports b on
a.rep_id = b.rep_id
and (-1 = #GroupID or
b.group_id = #GroupID)
and (-1 = #CatID or
b.cat_id = #CatID)
Note that I used -1 as the placeholder for all Groups and Categories. Obviously, use a value that makes sense to you. However, this way, you can specify a specific group_id or a specific cat_id and get the results that you want.
Additionally, if you want Group/Category/Quote details, you can always append more inner joins to get that info.
Also note that I added the Group_ID and Cat_ID conditions to the Reports table. This would be the SQL necessary if and only if you add a Cat_ID column to the Reports table. I know that your current table structure doesn't support this, but it needs to. Otherwise, as my grandfather used to say, "Boy, you can't get there from here." The issue here is that you want to limit reports by group and category, but reports only knows about group. Therefore, we need to tie something to the category from reports. Otherwise, it will never, ever, ever limit reports by category. The only thing that you can limit by both group and category is quotes. And that doesn't seem to be your requirement.
As an addendum: If you add cat_id to results instead of reports, the join condition should be:
and (-1 = #CatID or
a.cat_id = #CatID)
Is this what you are looking for?
SELECT a.*
FROM Results a
JOIN Reports b ON a.REP_Id = c.REP_Id
WHERE EXISTS (
SELECT * FROM CAT_QUOTE_JOIN c
WHERE c.QUOTE_ID = b.QUOTE_ID -- correlation to the outer query
AND c.CAT_ID = #CAT_ID -- parameterization
)
OR EXISTS (
-- note that subquery table aliases are not visible to other subqueries
-- so we can reuse the same letters
SELECT * FROM CAT_QUOTE_JOIN c, GROUP_CAT_JOIN d
WHERE c.CAT_ID = d.CAT_ID -- subquery join
AND c.QUOTE_ID = b.QUOTE_ID -- correlation to the outer query
AND d.GROUP_ID = #GROUP_ID -- parameterization
)

Resources