PostgreSQL: Return entries based on conditions and grouping - sql

Imagine the following table:
result_id student subject grade
1 Anne A 63
2 Anne B 63
3 Bob A 89
4 Bob B 51
5 Carla A 70
6 Carla B 70
7 Carla C 68
8 Dylan B 75
9 Dylan C 54
I would like to return the entries where a student got the exact same grade for subject A and B. So the ideal output would be:
result_id student subject grade
1 Anne A 63
2 Anne B 63
5 Carla A 70
6 Carla B 70
Can this even be achieved with queries? Struggling to find information about it.

A bit complicated but I hope easy to read.
select * from the_table
where student in
(
with t as
(
select student, count(*) cnt, min(grade) mig, max(grade) mag
from the_table
where subject in ('A', 'B') group by student
)
select student from t where mig = mag and cnt = 2
)
and subject in ('A', 'B')
order by student, subject;
result_id
student
subject
grade
1
Anne
A
63
2
Anne
B
63
5
Carla
A
70
6
Carla
B
70

Related

Using a top x count query as a where clause to show all qualifying records

I have a count of a top 2
My table has this data
Name Age price visited size
Jon 34 53 2018-01-01 9
Don 22 70 2018-03-01 15
Pete 76 12 2018-11-09 7
Jon 34 55 2018-09-13 9
Paul 90 64 2018-07-08 6
Pete 76 31 2018-03-25 7
Jon 75 34 2018-06-06 8
select top 2
name,
count(name) as cnt
from
tbl1
group by name
order by cnt desc
Which returns my top 2 names
Jon 3
Pete 2
This name will change dynamically as the query is run depending on who has made the most visits in total (this is very simplified the actual table has 1000's of entries).
What I would like to do is then use the result of that query to get the following all of which needs to be in a single query;
Name Age price visited size
Jon 34 53 2018-01-01 9
Jon 34 55 2018-09-13 9
Jon 75 34 2018-06-06 8
Pete 76 12 2018-11-09 7
Pete 76 31 2018-03-25 7
In summary, count who has visited the most and then display all the records under those names.
Thanks in advance
Here's one option using in:
select *
from yourtable
where name in (
select top 2 name
from yourtable
group by name
order by count(*) desc
)
order by name
Online Demo

How to join different table some column?

Student Table 1
ID Name Surname School Number Class Number ClassBranch
-----------------------------------------------------------------------------
113 Jane Smith 19 4 A
121 John Konl 42 5 B
331 Albert Smith 61 4 A
742 Jack Ronal 52 5 B
759 Jan Ronal 84 6 C
Student Table 2
ID Name Surname School Number Class Number Class Branch
-----------------------------------------------------------------------------
113 Jane Smith 11 4 D
151 John Konl 18 4 D
804 Albert Smith 26 5 F
605 Jack Ronal 32 5 F
785 Jan Ronal 87 8 L
Created Student Table
ID Name Surname School Number Class Number Class Branch
--------------------------------------------------------------------
113 Jane Smith NULL NULL NULL
151 John Konl NULL NULL NULL
804 Albert Smith NULL NULL NULL
605 Jack Ronal NULL NULL NULL
NULL NULL NULL 11 4 D
NULL NULL NULL 18 4 D
NULL NULL NULL 26 5 F
NULL NULL NULL 32 5 F
I want this table
ID Name Surname School Number Class Number Class Branch
113 Jane Smith 11 4 D
151 John Konl 18 4 D
804 Albert Smith 26 5 F
605 Jack Ronal 32 5 F
I want to Student Table 1 ---> ID,Name,Surname and Student Table 2 --> School Number,Class Number and ClassBranch joın.But joın is not successful.Class Branch A and B removed and D,F is adding.
First table ID,Name,Surname (3 columns) and Second table School Number,Class Number,Class Branch join.
Where conditions:
Removed column --> 4 - A AND 5 - B
Adding column --> 4- D AND 5- F
How can I write query?
Now that you've completely changed the original tables, this should be a simple JOIN.
SELECT t2.id, t1.name, t1.surname, t2.SchoolNumber, t2.ClassNumber, t2.ClassBranch
FROM Student1 AS t1
JOIN Student2 AS t2 ON t1.name = t2.name AND t1.surname = t2.surname
DEMO
To get those results?
1) The complicated way. By joining them.
SELECT
s2.ID,
s1.Name, s1.Surname,
s2."School Number", s2."Class Number", s2.ClassBranch
FROM Student1 AS s1
JOIN Student2 AS s2 ON (s2.Name = s1.Name AND s2.Surname = s1.Surname)
WHERE s1."Class Number" IN (4, 5);
2) The simple way, select only from the 2nd table
SELECT *
FROM Student2
WHERE "Class Number" IN (4, 5);
Test on SQL Fiddle here

SQL Multiple foreign keys referring to the same table

I have the following table Student:
Id FirstName LastName State Index Year Course1 Course2 Course3 Course4 Course5
622 Student622 LastName622 State17 62200 2 54 47 68 67 50
623 Student623 LastName623 State16 62300 3 17 99 37 99 32
624 Student624 LastName624 State2 62400 4 8 71 11 58 86
625 Student625 LastName625 State1 62500 2 39 75 33 77 17
626 Student626 LastName626 State15 62600 3 11 3 4 70 72
627 Student627 LastName627 State13 62700 2 84 77 65 62 76
628 Student628 LastName628 State13 62800 4 87 18 19 4 75
629 Student629 LastName629 State10 62900 2 96 67 14 97 31
The numbers in Course 1, 2, 3, 4, 5 are foreign key values that refer to the id of the following Courses table:
Id Name ProfessorId
1 Course 1 8
2 Course 2 14
3 Course 3 4
4 Course 4 3
5 Course 5 6
6 Course 6 2
7 Course 7 14
8 Course 8 4
9 Course 9 5
How can write script so that when i execute instead of foreign key numbers there is the Course name? Any thoughts?
You could LEFT JOIN to the Course table for each course id.
SELECT
s.Id, s.FirstName, s.LastName, s.State, s.[Index], s.[Year],
c1.Name as Course1Name,
c2.Name as Course2Name,
c3.Name as Course3Name,
c4.Name as Course4Name,
c5.Name as Course5Name
FROM Student s
LEFT JOIN Courses c1 ON c1.Id = s.Course1
LEFT JOIN Courses c2 ON c2.Id = s.Course2
LEFT JOIN Courses c3 ON c3.Id = s.Course3
LEFT JOIN Courses c4 ON c4.Id = s.Course4
LEFT JOIN Courses c5 ON c5.Id = s.Course5
Because those are all LEFT JOIN's, all the records of Student will be returned.
Even if some course id's aren't filled in.

How to add a column keeping incremental unique -letter(s)- for each record to a table in sql?

I have a table in sql server 2008 like
Id Name Volume
1 Jack 20
2 John 20
3 White 40
... (23 more)
27 Jennifer 12
28 Brown 33
29 Brown 55
... (23 more)
53 Alex 67
53 Smith 15
53 Mary 73
What I want is to add a column to this table, which keeps unique letter(s) for each different id. Example output:
Id Name Volume LetterKey
1 Jack 20 A
2 John 20 B
3 White 40 C
... (23 more) (...Z)
27 Jennifer 12 AA
28 Brown 33 AB
29 Brown 55 AC
... (23 more) (...AZ)
53 Alex 67 BA
53 Smith 15 BB
53 Mary 73 BC
... (...BZ)
We do not need 3-digit letters, ZZ would be the last possible one. I could not find a proper way to add such a column to the table. I appreciate if someone helps me with it. Thanks.
With only a two character code, you can use row_number() and some character arithmetic:
with t as (
select t.*, row_number() over (order by id) as seqnum
from t
)
select ((case when seqnum > 26
then char(ascii('A') + seqnum / 26 - 1)
else ''
end) +
char(ascii('A') + seqnum % 26)
)
from t

adding columns to get total and ranking total

this my table
student_numbers
ROLL_NO NAME CLASS HINDI MATHS SCIENCE
2 amit 11 91 91 81
3 anirudh 11 88 87 81
4 akash 11 82 81 85
5 pratik 10 81 99 98
7 rekha 10 79 97 82
6 neha 10 89 91 90
8 kamal 10 66 68 69
1 ankit 11 97 98 87
i want to add last three columns and rank on that total partitioned by class
this is what i tried
select roll_no,name,class,total,
rank() over (partition by class order by total desc) as rank
from student_numbers,(select hindi+maths+science total from student_numbers)
;
but this is showing a very large table,with duplicate student name having different total .
I'm not exactly sure what you are trying to accomplish -- order the highest grades by class? If so, something like this should work:
SELECT SN.Roll_No,
SN.Class,
SN2.Total,
RANK() OVER (PARTITION BY SN.Class ORDER BY SN2.Total DESC) as rank
FROM Student_Numbers SN
JOIN (
SELECT
Roll_no, hindi+maths+science as Total
FROM Student_Numbers
) SN2 ON SN.Roll_No = SN2.Roll_No
Here is the SQL Fiddle.
Good luck.