How to solve following Relational Algebra query - relational-algebra

On the relational algebra exam I had yesterday there was a question I couldn't answer and want to know how it would be solved. The constraint on the question was I wasn't allowed to use aggregate functions which I found difficult. The schema is as follows.
EMPLOYEE = {id, name, phone} with id PK
COURSE = {course_no, title, subject} with course_no PK
COMPLETED = {course_no, student_id, grade, semester} with {course_no,id,semester} PK
The question went: List the pairs of employees who have completed the same courses and have always completed these same courses in the same years and have never received grade 'D' in any of these courses. List each pair?
If any could shed some light that would be great.

Basically, you first build a query that joins the tables together to form the desired list of properties per employee.
Then you copy those two queries and join the results on course_no and - I guess - semester (and remember to exclude the rows where the same employee id appears on both sides).
Finally you filter this result by grade.
There are other variations possible, but this is the general idea.

Related

laravel 6 - how to sort by column in a foreign table

I'm sorry if this has been answered to death, but for a week I cannot figure out how to get this to work.
I'm using Laravel 6, and I have 3 tables
Facts
id
fact
Likes
id
fact_id
Dislikes
id
fact_id
I've created one to many relationships between the Fact model and the Like / Dislike models. I'm having issues trying to query all 3 tables, and sort the results descending by the total amount of rows from the likes table.
This is the query I am using to at least be able to get all the information from each table
$facts = \App\Fact::with(['likes', 'dislikes'])->paginate(25);
But i am completely lost on how to get it to sort in desc order, for the likes table.
Any help would be greatly appreciated, and if I have left out information, I will reply right away with it.
Thanks.
Use withCount for that
$facts = Fact::with(['likes', 'dislikes'])
->withCount('likes')
->orderByDesc('likes_count')
->paginate(25);
https://laravel.com/docs/6.x/eloquent-relationships#counting-related-models

Is there a way to concatenate across rows and columns in QuickBase?

As it stands, I have a student table (where each student has a unique row) and an exam table (where each student's exam attempt has a row; the student table is one to many to the exam table).
I want to see if it's possible to add a column to the students table that captures all of the student's exam attempts for a particular exam (as you see below). I was thinking this might require a for loop, but I'm not sure how to do that in QuickBase.
Create a Combined Text Summary field on the Students table for each Exam type.

Laravel eloquent for non-related tables

I have tables as seen here
An Athlete has many test days (e.g. one for each year), an Institution has teams e.g. Redham High School Rugby U14 Level. The team table is composed of the Institution, a sport and a level. Sport and Level are maintained in Codelist, which is just a table to maintain entities that are not big enough to be a table. Almost like replacing the hard coding of droplists.
Question 1: I don't see that the Codelist is related to the Team table, since Code list store unrelated info as well, like Country names, province names etc. Example of Codelist data below. Should they be related?
Question 2:
When creating a Test day record, I need the user to select for which team this athlete is getting tested for. I'm getting the Team records, but how do I now access the sport and level names from the Codelist?
I can't do $team->sport->name as they're not related tables.
Below is my current code. How do I get user friendly values for sportcode and levelcode e.g. Rugby U14.
$teams = Team::whereHas('Institution', function($query){
$query->whereClient_id(1);
})->get();
I can with $teams loop get the sportcode and level code and now write separate queries to fetch the names from the codelist. This would send 3 collections back to the view and doesn't seem the eloquent way. Advice appreciated.

How to save different surveys to database

I'm assigned to make a web based survey application on ASP.NET MVC3.
And I have three different surveys. I have to the way best way to store survey answers on database. I'm came up with only one solution: To make an answer table for each survey type.
Can you suggest better solutions?
You can have single table itself for Answers with Survey type as a Column.
perhaps something like this?
table survey_answers
id (this is the pk) : qid (question id): answer (varchar MAX) : pid (person id)
what you can do with this is make a questions table, a person table. in the question table, put the qid, the question, and the group of questions it belongs to (the survey id). Persons table is obvious, you put a pid (person id) and info about the person (such as name, age, gender etc.)
the reason that a three table approach is bad is because let's say the amount of surveys you have grows. let's see it becomes 100 different types of surveys or more - which is a definite possibility depending on where you work - are you going to have 100 different tables? 1000? no way.

LINQ group by parent and parent's parents

I have two tables:
Work
Date
Hours
EmployeeId
Employee
EmployeeId
EmployeeBossId
EmployeeBossId refers to another Employee, who may also have a boss and so on.
The owners can be in an infinite number of hierarchies (although in reality, it's just in 3 levels). I'm looking for a clever database call in the shape of a LINQ statement, that can receive an EmployeeId and tell me the sum amount of ALL hours for that employee, including any 'sub' employees - grouped by each immediate sub-employee!
Example
Bob
Jim
Joey
Anthony
Ben
Rita
Sid
Dan
Harry
Sally
Mike
If I provide the EmployeeId of Jim I want the total hours for Joey, Anthony (including hours for Ben and Rita) and for Sid.
I know I'm asking a bit much, but I am having trouble deciding how to attack this one and hoped it was clear to someone else out there. Thanks.
I'm looking for the entire thing to be done in ONE database call.
SQL Server supports something called Common Table Expressions, which basically allows you to do 'recursive queries', which is just what you need to solve the parent/parent-parent thing.
This unfortunately is not supported by Linq. But there is a way to do it, but it requires some SQL in your C# code (in the context to be precise). See this question and this howto.

Resources