Loop through a table based on multiple conditions - sql

Students table
student_id student_name
1 John
2 Mary
Grades table
student_id year grade_level school Course Mark
1 2015 10 Smith High Algebra 95
1 2015 10 Smith High English 96
1 2016 11 Smith High Geometry 85
1 2016 11 Smith High Science 88
2 2015 10 Smith High Algebra 98
2 2015 10 Smith High English 93
2 2016 11 Smith High Geometry 97
2 2016 11 Smith High Science 86
I'm trying to show results for each year and what class a student took with the grade.
So the final output i'm looking for is something like:
[student_id1] [year1] [grade1] [school1]
[course1] [mark1]
[course2] [mark2]
[course3] [mark3]...
[student_id1] [year2] [grade2] [school1]
[course1] [mark1]
[course2] [mark2]
[course3] [mark3]...
[student_id2] [year1] [grade1] [school1]
[course1] [mark1]
[course2] [mark2]
[course3] [mark3]...
This would all go in one column/row. So in this particular example, this would be my result:
1 2015 10 Smith High
Algebra 95
English 96
1 2016 11 Smith High
Geometry 85
Science 88
2 2015 10 Smith High
Algebra 98
English 93
2 2016 11 Smith High
Geometry 97
Science 86
So anytime a student id, year, grade, or school name changes, I would have a line for that and loop through the classes taken within that group. And all of this would be in one column/row.
This is what I have so far but I'm not sure how I can properly loop through course and grades for each group. I'd appreciate it if I can be pointed in the right direction.
select s.student_id + '' + year + '' + grade_level + '' + school
from students
join grades on students.student_id = grades.student_id

If you want to do it in your SQL Enviromnment, it depends on the Database Management System you are using.
For example, if you are using Transact SQL you can try to look at this link.
Generally this kind of loops and interactions are done in the programming language that is coupled with the SQL DB.
Anyway, you should look at Stored Procedures and Cursors if you really want to do this in SQL.

You are trying to mix presentation with retrieval of data from database tables. Looping through the resultset in sql can be achieved via cursor but that isn't adviced. You are better off by pulling the required data using two queries and later print it using a language of your choice.

Related

Postgres rank() without duplicates

I'm ranking race data for series of cycling events. Racers win various amounts of points for their position in races. I want to retain the discrete event scoring, but also rank the racer in the series. For example, considering a sub-query that returns this:
License #
Rider Name
Total Points
Race Points
Race ID
123
Joe
25
5
567
123
Joe
25
12
234
123
Joe
25
8
987
456
Ahmed
20
12
567
456
Ahmed
20
8
234
You can see Joe has 25 points, as he won 5, 12, and 8 points in three races. Ahmed has 20 points, as he won 12 and 8 points in two races.
Now for the ranking, what I'd like is:
Place
License #
Rider Name
Total Points
Race Points
Race ID
1
123
Joe
25
5
567
1
123
Joe
25
12
234
1
123
Joe
25
8
987
2
456
Ahmed
20
12
567
2
456
Ahmed
20
8
234
But if I use rank() and order by "Total Points", I get:
Place
License #
Rider Name
Total Points
Race Points
Race ID
1
123
Joe
25
5
567
1
123
Joe
25
12
234
1
123
Joe
25
8
987
4
456
Ahmed
20
12
567
4
456
Ahmed
20
8
234
Which makes sense, since there are three "ties" at 25 points.
dense_rank() solves this problem, but if there are legitimate ties across different racers, I want there to be gaps in the rank (e.g if Joe and Ahmed both had 25 points, the next racer would be in third place, not second).
The easiest way to solve this I think would be to issue two queries, one with the "duplicate" racers eliminated, and then a second one where I can retain the individual race data, which I need for the points break down display.
I can also probably, given enough effort, think of a way to do this in a single query, but I'm wondering if I'm not just missing something really obvious that could accomplish this in a single, relatively simple query.
Any suggestions?
You have to break this into steps to get what you want, but that can be done in a single query with common table expressions:
with riders as ( -- get individual riders
select distinct license, rider, total_points
from racists
), places as ( -- calculate non-dense rankings
select license, rider, rank() over (order by total_points desc) as place
from riders
)
select p.place, r.* -- join rankings into main table
from places p
join racists r on (r.license, r.rider) = (p.license, p.rider);
db<>fiddle here

SQL join tables with wildcard (MS Access)

how do i join following tables with wildcards? I would like to get all distinct rows from People table which contains SearchedName from SearchedPeople table.
SearchedPeople:
SearchedName
--------
Andrew
John
John Smith
People:
ID PersonName Attribute Age
----------------------------------------
1 John Smith 1 23
2 John Smith Jr 3 25
3 John Smith Jr II 4 73
4 Kevin 2 21
5 Andrew Smith 1 14
6 Marco 5 90
Desired Output:
PersonName Attribute Age
----------------------------------------
John Smith 1 23
John Smith Jr 3 25
John Smith Jr II 4 73
Andrew Smith 1 14
Code i got so far which doesnt wor. It returns three empty rows(why is that?).
SELECT b.PersonName, b.Attribute, b.Age
FROM SearchedPeople a
LEFT JOIN People b ON "%"&a.SearchedName&"%" like b.PersonName
It returns three empty rows because you don't have any columns from table a (SearchedPeople) and the LEFT JOIN didn't produce a match.
The reason is your criteria is in the wrong order you are searching for PersonName in the string %Searchedname% you need to switch that around. Also Access doesn't like the % as much as it likes the asteriks * for wilcard unless you make some changes to the query or configuration of MS-Access see below comment from Parafait.
I just tested this:
SELECT a.SearchedName
,b.PersonName, b.Attribute, b.Age
FROM
SearchedPeople a
LEFT JOIN People b
ON b.PersonName LIKE ("*" & a.SearchedName & "*")
Edit:
Good Ms Access specific information from a comment from #Parafait pasting in answer in case comment every got deleted.:
Use ALIKE and percents work. And if OP connects to MS Access via OLEDB and not the GUI .exe program, the % operator is required for LIKE statements in coded SQL. OP can also change database settings to ANSI-92 mode to always use % wildcards.

questions in SQL Oracle

I'm trying to answer these questions but I couldn't and I need here
1) List the number of days that have elapsed since each student joined.
this what I did
Select FR_FIRSTNAME,
FR_LASTNAME,
trunc(sysdate - FR_DATEJOINED) / 7 DAYS
from alharbi_bandar5_FRESHMEN;
no rows selected
2) List the student names and city in upper case.
This what i did
Select FR_FIRSTNAME, FR_LASTNAME, CITY FROM alharbi_bandar5_FRESHMEN
where UPPER (FR_FIRSTNAME, FR_LASTNAME, CITY) like 'SMITH%';
> where UPPER (FR_FIRSTNAME, FR_LASTNAME, CITY) like 'SMITH%'
*
ERROR at line 2:
ORA-00909: invalid number of arguments
3) List the no and last name of the student(s) with the highest ACT score.
This what i did
Select FR_NO, FR_LASTNAME, ACT from alharbi_bandar5_FRESHME
where ACT = MAX(ACT);
where ACT = MAX(ACT)
*
ERROR at line 2:
ORA-00934: group function is not allowed here
this is my table
FR_ FR_FIRSTNAME FR_LASTNAME FR_DATEJO ACT CITY
--- ------------------------------ ------------------------------ --------- ---------- ------------------------------
100 Mark Ramon 12-JUL-13 21 Florence
101 John Wright 13-JUN-13 31 Edgewood
102 Peter Sellers 06-JAN-13 30 Blue Ash
103 Eric Bates 14-MAY-13 24 Milford
104 Theresa Boyers 23-APR-13 22 Covingtion
105 Alex William 04-MAR-13 24 Edgewood
106 Eric Byrd 23-MAR-13 19 Alexandria
107 Steve Norris 21-DEC-12 21 Highland
108 Lisa Nkosi 13-FEB-13 33 Florence
109 Bradley Rego 21-FEB-12 29 Covington
110 Kathy Thomas 15-OCT-12 27 Milford
111 Catherine Jones 17-APR-13 34 Edgewood
112 Emily Hess 15-NOV-12 36 Highland
113 Josha Hunter 19-MAY-14 31 Florence
A lot of these questions have answers in the Oracle SQL reference and are mostly syntax issues.
1) trunc(sysdate - FR_DATEJOINED) / 7 DAYS
Oracle gies out the number of days in the units of difference, so sysdate - FR_DATEJOINED would gie you number of days, which could also involve fractional component (2.5 days for example, if it has been 2 days and 12 hours since the candidate joined). Trunc would get rid of the fractional component, but "/7" would convert the result into number of weeks instead. why are you doing this?
Either way, i don't believe this query is being fired against the table below, otherwise you'd not get zero rows as you are not filtering anything at all.
Check these out for more info on Oracle's date functions.
http://docs.oracle.com/cd/E17952_01/refman-5.1-en/date-and-time-functions.html
https://www.youtube.com/watch?v=H18UWBoHhHY
2) UPPER function accepts a column name or an expression, so if you need multiple columns. you'd need to use UPPER around each column.
3) For this example, you'll need to use a subquery to get the max value first and then use the query on top.
getting the max value
Select max(act) from alharbi_bandar5_FRESHME;
so, final query would be...
Select FR_NO, FR_LASTNAME, ACT from alharbi_bandar5_FRESHME
where ACT = (select MAX(ACT) from alharbi_bandar5_FRESHME);
Or, you could use the oracle rank function..
select fr_no,
fr_last_name,
act
from (
select fr_no, fr_lastname, act,
rank () over (order by act desc) rnk
from alharbi_bandar5_FRESHME
) where rnk = 1

How to query DBpedia online using SQL?

DBpedia just released their data as tables, suitable to import into a relational database. How can I query this data online using SQL?
Dataset:
http://wiki.dbpedia.org/DBpediaAsTables
I took the raw data, uploaded it to BigQuery, and made it public. So far I've done it with the 'person' and the 'place' table. Check them at https://bigquery.cloud.google.com/table/fh-bigquery:dbpedia.person.
Now is easy to know what are the most popular alma maters, for example:
SELECT COUNT(*), almaMater_label
FROM [fh-bigquery:dbpedia.person]
WHERE almaMater_label != 'NULL'
GROUP BY 2
ORDER BY 1 DESC
It's a little more complicated than that, as some people have more than one alma mater - and the particular way DBpedia encodes that. I left the complete query at http://www.reddit.com/r/bigquery/comments/1rjee7/query_wikipedia_in_bigquery_the_dbpedia_dataset/.
Btw, the top alma maters are:
494 Harvard University
320 University of Cambridge
314 University of Michigan
267 Yale University
216 Trinity College Cambridge
You can also do joins between tables.
For example, for each building (from the place table) that has an architect: What year was that architect born? How many buildings with an architect born that year are listed in DBpedia?
SELECT COUNT(*), LEFT(b.birthDate, 4) birthYear
FROM [fh-bigquery:dbpedia.place] a
JOIN EACH [fh-bigquery:dbpedia.person] b
ON a.architect = b.URI
WHERE a.architect != 'NULL'
AND birthDate != 'NULL'
GROUP BY 2
ORDER BY 2
Results:
...
8 1934
13 1935
9 1937
7 1938
17 1939
7 1941
1 1943
15 1944
10 1945
12 1946
7 1947
9 1950
20 1951
1 1952
...
(Google BigQuery has a free monthly quota to query, up to a 100GB each month)
(DBpedia data from version 3.4 on is licensed under the terms of the Creative Commons Attribution-ShareAlike 3.0 license and the GNU Free Documentation License. http://dbpedia.org/Datasets#h338-24)

VBA/SQL recordsets

The project I'm asking about is for sending an email to teachers asking what books they're using for the classes they're teaching next semester, so that the books can be ordered. I have a query that compares the course number of this upcoming semester's classes to the course numbers of historical textbook orders, pulling out only those classes that are being taught this semester. That's where I get lost.
I have a table that contains the following:
Professor
Course Number
Year
Book Title
The data looks like this:
professor year course number title
--------- ---- ------------- -------------------
smith 13 1111 Pride and Prejudice
smith 13 1111 The Fountainhead
smith 13 1222 The Alchemist
smith 12 1111 Pride and Prejudice
smith 11 1222 Infinite Jest
smith 10 1333 The Bible
smith 13 1333 The Bible
smith 12 1222 The Alchemist
smith 10 1111 Moby Dick
johnson 12 1222 The Tipping Point
johnson 11 1333 Anna Kerenina
johnson 10 1333 Everything is Illuminated
johnson 12 1222 The Savage Detectives
johnson 11 1333 In Search of Lost Time
johnson 10 1333 Great Expectations
johnson 9 1222 Proust on the Shore
Here's what I need the code to do "on paper":
Group the records by professor. Determine every unique course number in that group, and group records by course number. For each unique course number, determine the highest year associated. Then spit out every record with that professor+course number+year combination.
With the sample data, the results would be:
professor year course number title
--------- ---- ------------- -------------------
smith 13 1111 Pride and Prejudice
smith 13 1111 The Fountainhead
smith 13 1222 The Alchemist
smith 13 1333 The Bible
johnson 12 1222 The Tipping Point
johnson 11 1333 Anna Kerenina
johnson 12 1222 The Savage Detectives
johnson 11 1333 In Search of Lost Time
I'm thinking I should make a record set for each teacher, and within that, another record set for each course number. Within the course number record set, I need the system to determine what the highest year number is - maybe store that in a variable? Then pull out every associated record so that if the teacher ordered 3 books the last time they taught that class (whether it was in 2013 or 2012 and so on) all three books display. I'm not sure I'm thinking of record sets in the right way, though.
My SQL so far is basic and clearly doesn't work:
SELECT [All].Professor, [All].Course, Max([All].Year)
FROM [All]
GROUP BY [All].Professor, [All].Course;
Use your query as a subquery and INNER JOIN it back to the [ALL] table to filter the rows.
SELECT
a.Professor,
a.Year,
a.Course,
a.title
FROM
[ALL] AS a
INNER JOIN
(
SELECT [All].Professor, [All].Course, Max([All].Year) AS MaxOfYear
FROM [All]
GROUP BY [All].Professor, [All].Course
) AS sub
ON
a.Professor = sub.Professor
AND a.Course = sub.Course
AND a.Year = sub.MaxOfYear;