Display Only the Most Current Record on Report - reporting-services

We have a report that outputs Table A. We added Table B to the report model (we added some fields to the report from Table B), which is a many-to-one relationship to Table A. Now when we run the report, we are getting multiple rows, which is to be expected because of the relationship between Table A and Table B.
The problem is, we only want to show on the report the latest record of Table B, based on "creation date".
I tried to set a MAX(!fields.CreationDate)
I found information such as: https://social.msdn.microsoft.com/forums/sqlserver/en-US/2bc16c90-21d6-4c03-a17f-4a5922db76fe/displaying-records-by-max-date-in-ssrs
But when I do something like this, I get a "cannot use aggregate function......" error.
If this was a SQL Statement for TableB, it would be along these lines to display only the most recent record:
SELECT DISTINCT
[ID], [PID], [InputDate], [Changed_Date]
FROM
(SELECT
[ID], [PID], [InputDate], [Changed_Date],
dense_rank() over (partition by PID order by Changed_Date desc) as MyRank
from TableB
) as RankSelect
WHERE MyRank = 1
ORDER BY PID
This gives me the most recent record for TableB. I know technically I could add a view or something to the report model, but I do not want to do this, as another report ran might want a historical of all records in TableB. So I am hoping to somehow incorporate the above results into the report without touching the report model. In which only the latest record from TableB is incorporated into the report.
Would appreciate any help on how we can limit the report to only displaying the latest date record from Table B.

For your table B, set a FILTER for the CreationDate based on the MAX date over the dataset.
This will only display the records where the CreationDate matches the MAX CreationDate from your dataset.

I couldn't find a resolution to filtering the data in the report itself, so I removed TableB from the report model and replaced it with a view of TableB that only returns the most recent record as linked to TableA. Not ideal, as I know at some point they will ask for a report to "show all entries" from TableB, but will cross that bridge later :-)

Related

SSRS: how to display 20 groups per page on SSRS report

Requirement: I have used grouping on SSRS report 1 parent group and 2 child groups and I Need to display 20 parent level groups per page in report, irrespective of number of rows in child groups rows or parent group rows.
"SSRS Limiting groups per page on SSRS report"
Below how the Dataset and Grouping looks:
Now I need to Display 20 Student records in each page of Report (group StudentID).
Please let me know if anybody aware of solution/faced of this similar type of issue and found solution.
The Dataset's also includes the fields which are in the Row Groups.
This is probably not the most optimised way of doing this, but without seeing your dataset query or stored proc this is the best I can do.
Assuming you dataset query is a simple SELECT and not a stored proc then you can do the steps below. If it is a stored proc, then you can either modify the proc to follow the steps below or dump the results of the stored proc into a temp table in the same way the existing query is shown below.
Taking a simplified version of your data, if you original query was SELECT StudentID, SubjectID, ClientName FROM myTable then we can change this to something like ....
DROP TABLE IF EXISTS #t
CREATE TABLE #t (StudentID INT, SubjectID int, ClientName varchar(50))
INSERT INTO #t
SELECT StudentID, SubjectID, ClientName FROM myTable -- <== YOUR ORIGNAL QUERY OR STORED PROC HERE
SELECT
, ((DENSE_RANK() OVER(ORDER BY StudentID)-1)/20) as myPageNumber
, StudentID, SubjectID, ClientName
FROM #t
The result will give you your original data plus a column that calculates the correct page number (change the /20 if you want to change the number of unique StudentIDs on the page).
Now in the report, right click your outer row group (StudentID) and choose "Add Group => Parent Group", then just choose the myPageNumber field as the field to group by.
This will probably add an extra column to your report, you can delete this , but NOT the group , if prompted.
Finally, right-click your new group in the row group panel, go to "Properties", the "Page Breaks" and set "Between each instance of a group".
That should be it. This is done mainly from memory so it might not be perfect but hopefully should help you get what you want.

DISTINCT work differntly in MYSQL & POSTGRESQL

I have created a sample table report in SQL and filled sample data in it using the following command.
create table report(id int primary key,vistor_id int, branch_id int,date int);
insert into report values (1,1,3,27),(2,1,2,27),(3,1,1,28),(4,1,4,30),(5,1,1,30);
I need to find the list of recently visited(based on date column) branches with out duplication.
So I used the following query
select distinct branch_id from report order by date desc;
It works on MYSQL but shows the following error on POSTGRESQL. How to fix this? Or How can I obtain the same result in POSTGRESQL?(The error is from sqlfiddle.com).
ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select
list Position: 48
Your query is not valid standard SQL. It works in MySQL only if you have option ONLY_FULL_GROUP_BY disabled.
The problem is that there may be multiple dates per branch_id: which one should be used for ordering?
You can use aggregation and be explicit about what you ask for. Say you want to order by the latest date per branch_id:
select branch_id from report group by branch_id order by max(date) desc

How to find and remove duplicate records from multiple column,keep only one full record

I am trying to find duplicates from a table based on multiple columns. I have a table which has a columns like Email(Office),Email(Personal1) & Email_Personal2, Mobile_Personal1, Mobile_Personal2, FirstName,MiddleName,LastName,CompanyName,Designation etc. It has millions of records. There are so many duplicates for a specific record.
The folowing image represents the sample Table:
Now, I want to find the record using sql query which has the full values in its columns, want to keep this record and delete remaining all.
select *,count(*) from mytable where first_name!="" group by First_Name,Email_Office,Email_Personal1,Email_Personal2,Personal_Mobile1,Personal_Mobile2 having count(*)>1
But its showing me a specific record with total number of occurrence in last count(*) column only. Guide me in above query how can I see a record which has all the details along with number of occurrences of that specific record? How do I keep that one complete record and delete remaining all from a table?
I've removed "having count(*) >1" to see each record from above query but its taking so much time to show the output,almost feel like its getting hanged.
select t.* from mytable t inner join (select first_name,middle_name,last_name,designation,company_name,email_office,email_personal1,email_personal2,personal_mobile1,personal_mobile2,count(*) as NoDuplicates from mytable group by first_name,middle_name,last_name,designation,company_name,email_office,email_personal1,email_personal2,personal_mobile1,personal_mobile2 having NoDuplicates > 1) tsum on t.first_name=tsum.first_name and t.Middle_Name=tsum.middle_name and t.Last_Name=tsum.last_name and t.Designation=tsum.designation and t.Company_Name=tsum.company_name and t.Email_Office=tsum.email_office and t.Email_Personal1=tsum.email_personal1 and t.Email_Personal2=tsum.email_personal2 and t.Personal_Mobile1=tsum.personal_mobile1 and t.Personal_Mobile2=tsum.personal_mobile2

How would I display two separate tables within the same query/report without combining each entry?

As of right now, creating a query with all records from both tables I want to display gives me every record for table b for the first record of table a, then every record of table b for the second record of table a, and so on.
SELECT *
FROM tblSales, tblRepair;
I want to be able to format these tables so that records from each table are displayed within a report, but separately (not joined). Both these tables contain sales data that need to be displayed and calculated together on a daily basis, but my problem right now is getting the data out of these tables and together in a format that doesn't join each record together.
Thanks in advance.
You can use a UNION query to combine both tables. I've added a dummy column to distinguish between the two tables:
SELECT *,'Sales' AS TheTable FROM tblSales
UNION ALL SELECT *, 'Repairs' FROM tblRepairs;
This will list all the Sales records first, followed by all the Repairs. You can add an ORDER BY clause to change this.
Alternatively, depending on the type of report you are creating, you could base the main report on one table and add a subreport based on the second.

how to get values monthly from mysql table

I have a table lead and there is a field called added_on (datatype timestamp), I want to to fetch only the leads which are interested in a particular product and the reports should come monthly.
interested_in is a field in the lead table where the interested product's id will be stored as a comma separated values.
and $prod_id is stored with a product id which has to be checked.
the below query works fine just to fetch out the leads which are interested in a particular product. but i want the results to come month by month.
select*from lead where find_in_set('$prod_id',interested_in)
Please guide me what i have to do to achieve that
TRY
WHERE MONTH(added_on) = $giveMonthNumber
OR
WHERE MONTHNAME(added_on) = $givenMonthName;
Reference :
MySQL date time functions
Do this:
select * from lead where find_in_set('$prod_id',interested_in) group by added_on