What would be the SQL query for my problem? - sql

I need help with a SQL query for room within an android app. There is a table questions. This table has 3 columns:
id question id_sub_cateory
Furthermore there is table_sub_categories with folloing columns:
id sub_category id_category
And table_categories:
id category id_subject
Additionally there is a table_subject:
id subject id_questionnaire
Last but not least table_questionnaire:
id questionnaire
Now I would like to have a SQL query to get following information:
table_questionnaire.id table_questionnaire.questionnaire questionCount subjectCount
So basically a list which lists all questionnaires with number of questions and subjects included. What would be the SQL query for that?
EDIT: So far I have used this query:
SELECT table_questionnaire.id AS 'id', table_questionnaire.questionnaire_name AS 'name', " +
"COUNT(table_question.id_questionnaire) AS 'questionCount', COUNT(DISTINCT table_question.subject) AS 'subjectCount' " +
"FROM table_questionnaire " +
"LEFT JOIN table_question ON table_questionnaire.id = table_question.id_questionnaire GROUP BY table_questionnaire.id"
But this one is an old version because I saved each question with an ID of questionnaire, subject, category and sub_category. I recognized that this information is redundant. Now I save only the ID of sub_category. But now I have to change the query. I just don't know how!?

This is the query which works:
SELECT
table_categories.id,
table_categories.id_subject,
table_categories.category,
table_sub_categories.id AS 'id_sub_category',
table_sub_categories.id_category,
table_sub_categories.subCategory,
COUNT(id_category) AS 'questionsCount'
FROM table_question
LEFT JOIN table_sub_categories ON table_question.id_sub_category = table_sub_categories.id
LEFT JOIN table_categories ON table_categories.id = id_category
WHERE table_categories.id_subject = :subjectID
GROUP BY table_categories.id

Related

SQL - Returning fields based on where clause then joining same table to return max value?

I have a table named Ticket Numbers, which (for this example) contain the columns:
Ticket_Number
Assigned_Group
Assigned_Group_Sequence_No
Reported_Date
Each ticket number could contain 4 rows, depending on how many times the ticket changed assigned groups. Some of these rows could contain an assigned group of "Desktop Support," but some may not. Here is an example:
Example of raw data
What I am trying to accomplish is to get the an output that contains any ticket numbers that contain 'Desktop Support', but also the assigned group of the max sequence number. Here is what I am trying to accomplish with SQL:
Queried Data
I'm trying to use SQL with the following query but have no clue what I'm doing wrong:
select ih.incident_number,ih.assigned_group, incident_history2.maxseq, incident_history2.assigned_group
from incident_history_public as ih
left join
(
select max(assigned_group_seq_no) maxseq, incident_number, assigned_group
from incident_history_public
group by incident_number, assigned_group
) incident_history2
on ih.incident_number = incident_history2.incident_number
and ih.assigned_group_seq_no = incident_history2.maxseq
where ih.ASSIGNED_GROUP LIKE '%DS%'
Does anyone know what I am doing wrong?
You might want to create a proper alias for incident_history. e.g.
from incident_history as incident_history1
and
on incident_history1.ticket_number = incident_history2.ticket_number
and incident_history1.assigned_group_seq_no = incident_history2.maxseq
In my humble opinion a first error could be that I don't see any column named "incident_history2.assigned_group".
I would try to use common table expression, to get only ticket number that contains "Desktop_support":
WITH desktop as (
SELECT distinct Ticket_Number
FROM incident_history
WHERE Assigned_Group = "Desktop Support"
),
Than an Inner Join of the result with your inner table to get ticket number and maxSeq, so in a second moment you can get also the "MAXGroup":
WITH tmp AS (
SELECT i2.Ticket_Number, i2.maxseq
FROM desktop D inner join
(SELECT Ticket_number, max(assigned_group_seq_no) as maxseq
FROM incident_history
GROUP BY ticket_number) as i2
ON D.Ticket_Number = i2.Ticket_Number
)
SELECT i.Ticket_Number, i.Assigned_Group as MAX_Group, T.maxseq, i.Reported_Date
FROM tmp T inner join incident_history i
ON T.Ticket_Number = i.Ticket_Number and i.assigned_group_seq_no = T.maxseq
I think there are several different method to resolve this question, but I really hope it's helpful for you!
For more information about Common Table Expression: https://www.essentialsql.com/introduction-common-table-expressions-ctes/

SQL query how to show column based on data from a different column? [duplicate]

This question already has answers here:
SQL Server dynamic PIVOT query?
(9 answers)
Closed 1 year ago.
I have this query:
SELECT Students.StudentNumber,
Students.StudentSurname,
Students.StudentFirstNames,
Students.SchoolYear,
Students.Class,
Cycle,
Section,
MarksEntry.SubjectCode,
MarksEntry.AssessmentPeriod,
MarksEntry.SubjectMaxima,
MarksObtained,
Subject.SubjectName
FROM Students,
MarksEntry,
Subject
WHERE Students.StudentNumber = MarksEntry.StudentNumber
AND MarksEntry.SubjectCode = Subject.SubjectCode
AND Students.Class = MarksEntry.Class
AND MarksEntry.SchoolYear = '2020-2021'
AND MarksEntry.Class = '1ere LIT'
AND MarksEntry.AssessmentPeriod = '2รจ P'
ORDER BY Students.StudentSurname;
I get the results this way:
How can I modify this query so that I can get the data listing only the students with no duplication and the score marks under each subject, with the name of the subject as column name.
The subjects names are not the same, they differ from classes, so they have to be read dynamically from the subjectName column based on the query condition.
Something like this:
You can use LEFT JOIN with the table MarksEntry multiple times, where each MarksEntry corresponds to a different subject.
For example,
SELECT s.StudentNumber,
rel.MarksObtained ReligionMarks,
bio.MarksObtained BiologyMarks
FROM Students s
LEFT JOIN MarksEntry rel
ON s.StudentNumber = rel.StudentNumber
AND rel.SubjectName = 'Religion'
LEFT JOIN MarksEntry bio
ON s.StudentNumber = bio.StudentNumber
AND bio.SubjectName = 'Biology'
This will only work if there aren't any duplicated students in the same course, otherwise, each register will appear multiple times.
I'd also recommend you take a look at the GROUP BY clause in case there are duplicates

Select information from different table SQL

SELECT Boeking.reisnummer, (aantal_volwassenen + aantal_kinderen) AS totaal_reizigers
FROM Boeking
WHERE Boeking.reisnummer = Reis.Reisnummer
AND Reis.Reisnummer = Reis.Bestemmingscode;
Table 1 (Boeking) has aantal_volwassen and aantal_kinderen, and Reisnummer.
Table 2 (Reis) has Reisnummer and Bestemmingscode.
I have to show the total of aantal_volwassenen and aantal_kinderen. But i also have to show Reis.bestemmingscode which comes from a different table. Currently i have to enter a parameter, how can i solve this?
You need to specify all the tables in the FROM part of your query. The tables should then be joined (JOIN) to get the data you need.
SELECT Boeking.reisnummer
,(aantal_volwassenen + aantal_kinderen) AS totaal_reizigers
,Reis.Bestemmingscode
FROM Boeking INNER JOIN Reis
ON Boeking.reisnummer = Reis.Reisnummer

SQL - Remove Duplicates in Single Field

SELECT Company.CompanyName
,Student.Status
,Student.Level
,Student.PlacementYear
,Company.CompanyCode
,Company.HREmail
,Company.Telephone
,Company.HRContact
,PlacedStudents.DateAdded
FROM Student
RIGHT JOIN (Company INNER JOIN PlacedStudents
ON Company.CompanyCode = PlacedStudents.CompanyCode)
ON Student.StudentNo = PlacedStudents.StudentNo
WHERE (((Student.PlacementYear)=" & Year & "))
AND((Student.Status)<>'Still Seeking YOPE')
ORDER BY Company.CompanyName
I have this SQL Query which pulls HR Contacts from Companies where students are currently placed. However, there are multiple students at one company so when I run the query there are duplicates. I'm fairly new to SQL, I tried DISTINCT, however it didn't seem to do anything, the duplicates remained.
How can I remove duplicates in the CompanyCode field so that the Company only appears once when the query is run.
Below is an image of what happens when I run query. Hopefully this makes sense?
Any help would be appreciated.
This query should give you companies that have placed students:
SELECT Company.CompanyName
,Company.CompanyCode
,Company.HREmail
,Company.Telephone
,Company.HRContact
FROM Company
WHERE EXISTS (SELECT * FROM PlacedStudents INNER JOIN
Student ON Student.StudentNo = PlacedStudents.StudentNo
WHERE Company.CompanyCode = PlacedStudents.CompanyCode
AND Student.PlacementYear =" & Year & "
AND Student.Status <>'Still Seeking YOPE')
ORDER BY Company.CompanyName;
Your question is asking for HR Contacts from Companies where students are placed. I assume this means if you have 1, 2 or 1,000,000 students at a single company, you only want to see the company listed once?
Your current query is returning information from STUDENT and PLACEDSTUDENTS which is going to result in output like
COMPANY_A STUDENT01 .........
COMPANY_A STUDENT02 .........
COMPANY_A STUDENT03 .........
and so on.
If so, and taking a best guess (since I can't know what's in STUDENT or PLACEDSTUDENTS tables), try not including anything related to STUDENT in the SELECT.
SELECT DISTINCT Company.CompanyName, Company.CompanyCode, Company.HREmail,
Company.Telephone, Company.HRContact FROM
I'll be happy to help more if you can provide more information about the structure of the tables and some examples of data, AND what you actually want from the query.

How to use count correctly in sql?

I have tow tables 'matches' and 'forum' I need to get match information from the matches table which has comments in the forum table so I use the following query:
SELECT distinct forum.match_static_id, matches.*
from forum
INNER JOIN matches
ON forum.match_static_id = matches.static_id
WHERE forum.comments_yes_or_no = 1
I use distinct to avoid getting the same match twice if it has more than one comment in the forum table.
The problem is I want to get the count of each match comments with the same query is it possible? I use :
SELECT distinct forum.match_static_id, count(forum.comments), matches.*
from forum
INNER JOIN matches
ON forum.match_static_id = matches.static_i
WHERE forum.comments_yes_or_no = 1
but it give me just one record (which is wrong). What is the problem ?? does I need to use group by ? and if yes where to but it in this crowded query?
Please try this:
SELECT forum.match_static_id, count(matches.id), matches.*
from forum
INNER JOIN matches
ON forum.match_static_id = matches.static_i
WHERE forum.comments_yes_or_no = 1
GROUP BY forum.id