SSRS Count Record Based on Field Value (Status) in a Group - sql-server

I have the data table below:
--> Click here for the picture.
I want to add new column and count how times an employee is present in the office something like this:
--> Click here for the picture.
How can I count it in expression given that I have group in Employee.
Thank you.

You can use LookupSet() function to get the In-Office count. Add a tablix with the Employee Name group.
Note the Days Worked column is inside the Employee group scope but outside the details group scope.
Use this expression to get the count of Attendance In-Office per employee:
=LookupSet(Fields!Employee.Value & "-" & "In-Office",
Fields!Employee.Value & "-" & Fields!Attendance.Value,
Fields!Attendance.Value,"DataSetName"
).Length
Replace DataSetName by the actual name of your dataset. It will produce the below tablix:
UPDATE: Based on OP's comment.
Replace the LookupSet expression and use this instead to add multiple criteria to the filtered count.
=COUNT(IIF(Fields!Attendance.Value="In-Office" OR
Fields!Attendance.Value="Out for Official Business",
Fields!Attendance.Value,Nothing))
It counts In-Office and Out for Official Business rows in the given group.
Let me know if this helps.

You can do it via SQL script.
SELECT A.EMPLOYEE_NAME,A.DATE,A.ATTENDANCE,B.DAYS_WORKED
FROM TABLE A
LEFT JOIN
(SELECT EMPLOYEE_NAME,COUNT(ATTENDANCE) DAYS_WORKED
FROM (
SELECT EMPLOYEE_NAME,DATE,ATTENDANCE
FROM TABLE
WHERE ATTENDANCE = 'IN-OFFICE')
) B
ON A.EMPLOYEE_NAME = B.EMPLOYEE_NAME
You now have the Field DAYS_WORKED then just add it to your table.

=Count(Fields!EmployeeName.Value,"DataSet1")

Related

need to filter data based on no. of days for one record from column along with all the actual records

I have two tables. Caseclaims and claimstates , from these tables i am getting the resultant , claimcount and conditional counf for claimstates such as SENT, ACCEPTED , CLAIM STATUS REQUESTED .
NOW , MY requirement is along with count for all claimstates , i need to filter the count for 'SENT' BASED ON primaryADJUDICATIONDAYS (THIS IS FROM ANOTHER TABLE)
I am not sure what you are looking for.Add table structure by using DESC <table_name> with your question which will greatly help contributors.
just ansering on assumptions: try GROUP BY statement on which want to count. i.e.:
SELECT COUNT(status) FROM ...... .... .... GROUP BY (on which column u want to count)

SSRS Report Table Add Column Expression Record x of y?

I have an SSRS, which is compiled of report tables, each tied to a stored procedure, all keying off the same ID. I pass the ID automatically, through the UI.
Because there is so much information in the report, For each section, I wanted to add a column to each table, with an expression. It would give the record count. This would mean, that if the table had an artist's albums, there would be a column for Album Record, and it would be something like this:
Arist: Pink
Album Record Album Name Album Release Date Etc
1 of 5
2 of 5
3 of 5
To build the pieces of the Album record, I added a row counter to the stored procedure. This is that SQL line:
,ROW_NUMBER() OVER (PARTITION BY pc.ID ORDER BY ARID.ID) 'Album Record Number'
pc.id is the ID parameter that is passed, which means, we only pass and retrieve 1 pc.ID. The ARID.ID is for the sub-record ID's.
This creates the first piece. Record 1,2,3.
In the SSRS table, I add a column and I make an expression:
=Fields!Album_Record_Number.Value & " of " & Count(Fields!ARID.Value,"ID")
This should give me "1 of 3", "2 of 3", "3 of 3".
When I attempt to preview the report, I get an error:
An error occurred during local report processing. The definition of
the report '/MainReport' is invalid. The Value expression for the text
box 'XYZ' has a scope parameter that is not valid for an aggregate
function. The scope parameter must be set to a string that is equal to
either the name of a containing group, the name of a containing data
region or the name of a dataset.
Anyone know how to do what I'm trying to do? I'm dreading that I may have to add the total count to the stored procedure, which means I would have to add that to all my stored procs. I was hoping an expression in a table column would do the trick.
Any suggestions would be appreciated!
I think you already using Groups so you can use a similar expression:
=RowNumber("YourAlbumGrouping") & " of " & CountRows("YourAlbumGrouping")
RowNumber function returns a running count of the number of rows for the specified scope.
CountRows function returns the number of rows in the specified scope, including rows with null values.

laravel - get subtotal from rows having similar column values under another column with same values

The DB has a table namely records. Among the fields are product_id,quantity,store_id.
I need to get the sum total of quantity of those rows which belong to a certain store_id and then have same product_id values.
For example , the table has values : (2,3,4),(2,1,5),(1 2,2,4) Then I need to get the sum total of quantity along with other columns from 1st and 3rd rows. And the 2nd row will also be present in the result.
Let us assume the Controller name to be RecordController and the model name to be Record.
How should I write the query ?
Edit : the table has values : (2,3,4),(2,1,5),(1 2,2,4)
It can be done using a single query.
Try This:-
DB::table('records')
->select('product_id',DB::raw('sum(quantity) as quantity'),'store_id')
->groupBy('product_id')
->groupBy('store_id')
->get();
Explanation:
This query will fetch all the records & group them by product_id & store_id so will only get a single row for a single product in the store. And then it will display the sum of quantity in the output.
Update:-
Using ORM:-
\App\YourModelName::select('product_id',DB::raw('sum(quantity) as quantity'),'store_id')
->groupBy('product_id')
->groupBy('store_id')
->get();
But you need to write the raw query in any case, it can't be done using default ORM functionality

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

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.

Custom use of count function in ms access

I have two tables Patient_tbl and Consult_tbl in MS access (with the fields shown below). The first one is used to record patient info, and the second one (Consult_tbl) is used to record patient visits. They are related in a one-to-many relationship using a Patient_id field.
What I need to do is to count the visitors based on gender for a given period of time, based on the consult table using Patient_id. I don’t know how to do it. Would you please help?
Patient_tbl has the following fields:
{Patient_id
P_add
P_tel
P_gender
Other fields}
Consult_tbl has the following fields:
{Consult_id
Patient_id
C_date
C_ref
Other fields}
Join the tables on Patient_id, add a where clause for the date range and use the count() function and group by P_gender:
select p.P_gender, count(*) as "count"
from Patient_tbl p
inner join Consult_tbl c on p.Patient_id = c.Patient_id
where c.C_date >= '2015-01-10'
and c.C_date <= '2015-01-20'
group by p.P_gender

Resources