Counting the rows on the basis of row group - reporting-services

I am creating a report in SSRS 2008R2. Report looks like this:
NSN Learner Employer Completed Date
Qualification 1 123356 Tom Employer 1 12/2/2012
213134 Jerry Employer 2 25/2/2010
545456 Harry Employer 3 04/2/2011
Qualification 2 565557 Pete Employer 1 17/12/2001
154646 Susie Employer 5 12/13/2005
I want to calculate the total number of records in one qualification and put it as a row in the table after each qualification. Table is grouped by qualification field. Report should look like this:
NSN Learner Employer Completed Date
Qualification 1 123356 Tom Employer 1 12/2/2012
213134 Jerry Employer 2 25/2/2010
545456 Harry Employer 3 04/2/2011
**Total 3**
Qualification 2 565557 Pete Employer 1 17/12/2001
154646 Susie Employer 5 12/13/2005
**Total 2**
Thanks in advance.

You can add total after group and use expression CountDistinct(Completed_Date) to get desired value

Related

List all teams and team member details in a subject

I'm having some trouble working out how to write a query. I want to be able to make a list of all teams, and the students within the team that are in a certain subject.
Here are the sorts of tables I have.
Team (teamID, teamName, subjectID)
Student(studentID, Student Name)
AssignTeam(AssignID, studentID, teamID)
Subject(subjectID, subjectName)
This is what I would like the output to look like.
Team 1 - Team Name
Student 1 ID - Student 1 Name
Student 2 ID - Student 2 Name
Student 3 ID - Student 3 Name
Student 4 ID - Student 4 Name
Team 2 - Team Name
Student 1 ID - Student 1 Name
Student 2 ID - Student 2 Name
Student 3 ID - Student 3 Name
Student 4 ID - Student 4 Name
Student 5 ID - Student 5 Name
Team 3 - Team Name
Student 1 ID - Student 1 Name
Student 2 ID - Student 2 Name
Student 3 ID - Student 3 Name
I'm struggling to work out how to format it in such a way to include the breaks between the groups themselves. I only really know how to make a list that looks like this
Group StuID StuName
-- ---- ----
1 1 Mike
1 2 Stacey
1 3 Jenny
2 4 Rick
2 5 Sam
3 6 Larry
3 4 Anita
I want to build the list using mySQL but it will ultimately be outputting via PHP. I was hoping to create a stored procedure which I can then call and pass the subjectID into which will then create the list.
I haven't quite worked out if the one procedure would create the list and convert to string for output or if that should be two separate queries.
Any suggestions would be much appreciated.
Thanks
This is not a mysql solution but would it not be simple enough to read the rows from mysql in PHP, and when the Group changes output the header line for that group?
See the snippet below. You'll have to make outputRow() and outputGroupHeader() generate the output in the format you need.
<?php
$lastgroup = "";
while ($row = mysqli_fetch_assoc($query))
{
if ($row['group'] != $lastgroup) {
outputGroupHeader($row['group']);
$lastgroup = $row['group'];
}
outputRow($row);
}

Define table of choosed user row in multiple table list

I have control dashboard where multiple tables are listed with query
And in dashboard I can switch it to one table from ALLData to User1Table... and vice versa.
When there is only one table chosed I can easily manipulate data. However, I am struggling with updating rows when ALLData(all tables) are listed in dashboard. I can update it checking each table. I was wondering is there any better way to update it.
Tables have no DR. All tables have same column names.
//ALLData
SELECT * FROM users1
UNION ALL
SELECT * FROM users2...
user1
id name tel status
1 Bob 911 1
user2
id name tel status
3 Anna 11 0
3 Jack 12 1
//ALLData in dashboard
id name tel status
1 Bob 911 1
3 Anna 11 0
3 Jack 12 1
I can use id and status as PK

Join multiple table and sort the result

This is the first question i post in stackoverflow. Hope that you guys can help me resolve this problem. I have been stuck for 2 days.
I have 6 tables. It's all below:
students:
id name lastname
1 John Snow
2 Sansa Stark
3 T-Bag Bagwell
student_course:
id student_id course_id course_start course_end
1 1 1 2015-06-19 2015-08-20
2 2 3 2015-07-09 2015-09-15
3 3 1 2015-05-15 2015-08-22
payment:
payment_id student_id course_id
1 1 1
2 2 3
3 3 1
payment_initial:
payment_id payment_due
1 2015-06-12
3 2015-05-08
payment_installment:
payment_id payment_due int_payment_due
2 2015-07-02 2015-07-15
passport_visa:
student_id passport_expiry_date visa_expiry_date
1 2015-09-10 2015-10-12
2 2015-09-12 2015-09-15
3 2015-10-11 2015-9-28
And the result i want is: result will be sort by date combined form 3 tables which have "Date" field. "Date" field after sorting include only date after present.
How can I make query string that bring me the result like this:
student_id(1) course_id(1) course_start(2015-06-19)
student_id(2) course_id(3) payment_id(2) payment_due(2015-07-02)
student_id(2) course_id(3) course_start(2015-07-09)
student_id(2) course_id(3) payment_id(2) int_payment_due(2015-07-15)
student_id(1) course_id(1) course_end(2015-08-20)
student_id(3) course_id(1) course_end(2015-08-22)
student_id(3) passport_expiry_date(2015-09-12)
.....
I want to add Name and Lastname at the result but it show too long. So I just write like that.
Last result i want to get is the date field (sorted), and which event of date will happen (course start, course end, payment due...)
Sorry if my English grammar is bad. Please help me. Thank you all.
I just collect all result after insert data (which has date field) into one table, and sort them in that table. That makes more insert query but easy to sort.
So I do it on that way.

Auto sum in a mysql table when a data is entered in another mysql table

I have 2 mysql tables :- entry and result
The structure of entry is as follows :-
Name Department Units_Sold Month
John Sales 3 January
John Sales 6 January
Ana Retail 11 January
Rick Marketing 1 February
Vicky Sales 4 March
Ana Retail 2 March
Vicky Sales 9 March
Vicky Sales 1 March
Each user enters the number of unit sold. Name, Department and Month are fetched from the server, when a user logs in and enters the "Units Sold".
When this entry is being made by the user I want another table to automatically update the monthly report. The 2nd table "result" is having the following structure :-
Name Department January February March
John Sales 9 0 0
Ana Retail 11 0 2
Rick Marketing 0 1 0
Vicky Sales 0 0 14
In this table, the fields:- Name, Department, January, February,... December are static. I want to update the sum of "Units Sold" by each user for month when he/she makes an entry into the 1st table.
Can anyone, please suggest on this ?
Thanks !!
What you are looking to do is produce what's called a Pivot Table, using the data stored in your first table. It's a relatively complex collection of aggregate functions that is well worth learning - especially if you have anything to do with financial reporting. This site has a good intro into doing what you are asking.

Why this sentence in SQL is working? COUNT and WHERE to get Students from Teachers

I tried to create a VIEW that merge some tables in order to have the "hottest" teacher in a educational platform.
First, I have a table with the users (some teachers, some students),
then in other I have the lessons created by the teachers,
finally, other one that has the relation between students and lesson.
when I use this SQL sentence
CREATE OR REPLACE VIEW `skn_teachers` AS
select
`u`.`id_skn_users` AS `id_skn_users`,
`u`.`firstName` AS `firstName`,
`u`.`lastName` AS `lastName`,
COUNT(`ls`.`createdBy`) AS `countLessons`
from (`skn_users` AS `u`, `skn_rolesxusers` AS `rxu`, `skn_roles` AS `ro`, `skn_approved_lessons` AS `ls`)
where ((`rxu`.`id_skn_users` = `u`.`id_skn_users`) and (`rxu`.`id_skn_roles` = `ro`.`id_skn_roles`) and (`ro`.`name` = 'teacher') and (`ls`.`createdBy` = `rxu`.`id_skn_users`))
group by `u`.`id_skn_users`
the row countLessons show me the number of lessons per teacher eg.
id | firstName | lastName | countLessons
1 Pepito Perez 1
2 Julián Figueroa 7
3 Daniel Aguirre 2
which is correctly the number of lessons per teacher.
but I need the number of students that have the lessons created by each teacher (all of them, sum of all students in all lessons of THAT teacher), countStudentsByTeacher, in one of my attempts, get this SQl and it was a surprise when I got the number of students by teacher but I don't understand clearly what I did.
new SQL sentence:
CREATE OR REPLACE VIEW `skn_teachers` AS
select
`u`.`id_skn_users` AS `id_skn_users`,
`u`.`firstName` AS `firstName`,
`u`.`lastName` AS `lastName`,
COUNT(`ls`.`createdBy`) AS `countStudents`
from (`skn_users` AS `u`, `skn_rolesxusers` AS `rxu`, `skn_roles` AS `ro`, `skn_approved_lessons` AS `ls`, `skn_lessonsxusers` AS `lxu`)
where ((`rxu`.`id_skn_users` = `u`.`id_skn_users`) and (`rxu`.`id_skn_roles` = `ro`.`id_skn_roles`) and (`ro`.`name` = 'teacher') and (`ls`.`createdBy` = `rxu`.`id_skn_users`) and (`ls`.`id_skn_lessons` = `lxu`.`id_skn_lessons`))
group by `u`.`id_skn_users`
//
id | firstName | lastName | countLessons
1 Pepito Perez 10
2 Julián Figueroa 15
3 Daniel Aguirre 8
here, column countLessons shows really the number of students into all lessons created by each teacher, exactly what I wanted but I want to know why this works.
Thanks in advance!
In your 2nd query, you added at join to the table skn_lessonsxusers. This causes the 2nd query to return additional rows per each student that was assigned the lesson. The first query only returned the lessons that were created by each teacher.
Example:
Looking at the teacher "Daniel Aguirre", the underlying non-aggregated data for the first query might look like this:
id | firstName | lastName | Lesson
3 Daniel Aguirre Math 101
3 Daniel Aguirre CS 104
So there are 2 lessons that he teaches.
Now if you add in the students for each lesson, i.e. the 2nd query, then the data might look like this.
id | firstName | lastName | Lesson | Student
3 Daniel Aguirre Math 101 John
3 Daniel Aguirre Math 101 Bob
3 Daniel Aguirre Math 101 Sara
3 Daniel Aguirre Math 101 Mary
3 Daniel Aguirre CS 104 John
3 Daniel Aguirre CS 104 Bob
3 Daniel Aguirre CS 104 Dan
3 Daniel Aguirre CS 104 Sally
So now when you aggregate this 2nd set of data, it will show a count of 8 because there are 8 student/lesson combination for Daniel Aguirre.
Now assuming that John and Bob in the above 2nd data set are the same student, there are not really 8 different students taking lessons from Daniel Aguirre, there are 6 (John, Bob, Sara, Mary, Dan, and Sally). If you wanted to show 6 in this example you would use:
COUNT(DISTINCT `lxu`.`student_id`) AS `countStudents`
where student_id is the column that represents a unique student in the skn_lessonsxusers table. NOTE: your column is probably a different name than "student_id", but I don't know your table structure for this example so I just used "student_id".
It works because you multiply each lesson by each student in this lesson
join... 'skn_approved_lessons' AS 'ls'
where... and ('ls'.'createdBy' = 'rxu'.'id_skn_users')