Hi I have two tables here:
Transcript
Grade Student_number Course_number Semester Year
A 8 MATH2410 Fall 07
A 8 CS1310 Fall 07
B 8 CS3320 Spring 08
B 17 MATH2410 Fall 08
C 17 CS1310 Fall 08
A 8 CS3380 Fall 08
Student
Name Student_number Class Major
Smith 17 1 CS
Brown 8 2 CS
As you can see, they have a common Student_number. I want a table that looks like this:
Grade Student_number Course_number Semester Year Name
A 8 MATH2410 Fall 07 Brown
A 8 CS1310 Fall 07 Brown
B 8 CS3320 Spring 08 Brown
B 17 MATH2410 Fall 08 Smith
C 17 CS1310 Fall 08 Smith
A 8 CS3380 Fall 08 Brown
How do I do this?
I tried using insert and left join but neither worked. How do I achieve the third table? Thanks!
18 people have seen it, but none could figure out this question, so I did it myself with a bit of research.
SELECT STUDENT.Name, TRANSCRIPT.Grade, TRANSCRIPT.Student_number, TRANSCRIPT.Course_number, TRANSCRIPT.Semester, TRANSCRIPT.Year
FROM TRANSCRIPT
LEFT JOIN STUDENT
ON TRANSCRIPT.Student_number = STUDENT.Student_number
If your first two tables are still in use, maybe view is a better choice, and if Transcript.Student_number is must from Student, just use INNER JOIN.
create or replace view view_students_transcripts as SELECT t.*,s.name FROM Transcript t inner join Student s ON s.Student_number = t.Student_number;
Related
Table reserve
sib boot_id day
22 101 10/10/1996
22 103 11/12/1996
Table Sailors
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5
58 rusty 10 35.0
28 yuppy 9 45.0
44 gruppy 8 55.5
The result I want is find name who borrow all boot ?
You should definetely try a basic tutorial, there is a good one at Khan Academy: https://www.khanacademy.org/computing/computer-programming/sql.
It is a simple join. Since one person has both items, I've also included a distinct not to repeat the name.
Your answer is :
select distinct ts.sname
from Table_Sailors as ts
inner join Table_Reserve as tr on ts.sid = tr.sib
Just change the table names, Table_Sailors and Table_Reserve, to whatever table name you have in your database.
ID Year Month Price
001 1990 JAN 6
001 1990 FEB 8
...
001 1990 DEC 4
001 1991 JAN 7
...
001 2000 DEC 6
002 1990 JAN 7
...
Given a table formatted like the one above, how can you find the average yearly price for each item (of each year)? So for example, I'd like to have a new table that looks like:
ID Year Avg_price
001 1990 7
001 1991 12
...
002 1990 11
...
I've tried the following code:
SELECT ID, Year, AVG(Price)
FROM DATA
GROUP BY ID, Year
But end up getting 0 for each of the averages. The ordering seems to be working correctly though, so I'm not sure why this is. Any help would be greatly appreciated.
EDIT: It turns out there was nothing wrong with my SQL code at all. I guess the answer was simply a bug. Thanks for all your replies, everyone.
Your SQL looks fine to me (Checked with MS SQL).
SQL Fiddle Demo
Please doublecheck with MySQL. ;-)
I hope someone can help. I'm trying to set up something along the below but am getting in a bit of a muddle. From what I understand, deriving a numeric ID variable (eg auto-incremented) for the primary key is more efficient that using a composite primary key of the 'natural' variables that define a record (especially if they're character variables (and more so if the collation is UTF-8))
As in the below example, each customer has a list of items (ITEMID) which are all members of a category (CATID), however, the problem is that I need customers to additionally be able to assign their items as a component of a collection set (SETID) which is a non-identifying reference table - any customer could have multiple versions of one SETID.
The items required for a set are specified by CATID. Therefore, in the example below which is for one customer, they could choose to assign Item 2, or 4 (or neither or both) to SET 001.
**ITEMS**
ITEMID CATID
1 04
2 02
3 01
4 02
5 05
**SETS**
SETID CATID
001 01
001 02
002 04
003 05
**CATEGORY**
CATID
01
02
03
04
05
**Wanted result:**
ITEMID CATID SETNUMBER SETID
1 04 (customer chose not to assign to SET 002)
2 02 1 001
3 01 1 001
4 02 2 001
5 05 003
Many thanks in advance!
From your description, it sounds like the "ITEM" table could stand to have a NULLable "SETID" foreign-key column.
I am using the following SELECT to get all the 2012 rushing plays from an NFL play by play database
Select gameid, season, off, description, qtr, down, ydstogoal
FROM 2010_12pxp
WHERE DESCRIPTION LIKE "%up the middle%" OR DESCRIPTION REGEXP "(left|right) (tackle|guard|end)" OR DESCRIPTION REGEXP " rushe(d|s) for "
AND season = 2012
GROUP BY id
However, it is returning ALL of the running plays in the database, not just those from 2012. Sample here:
gameid season off description qtr down ydstogoal
20100909_MIN#NO 2010 NO (13:36) R.Bush left end pushed ob at MIN 29 for 8 yards (M.Williams A.Allen). 1 2 37
20100909_MIN#NO 2010 MIN (12:56) A.Peterson right tackle to MIN 23 for 3 yards (R.Harper). 1 1 80
20100909_MIN#NO 2010 MIN (12:16) A.Peterson right tackle to MIN 28 for 5 yards (J.Vilma). 1 2 77
20100909_MIN#NO 2010 MIN (11:38) A.Peterson left guard to MIN 27 for -1 yards (S.Shanle J.Dunbar). 1 3 72
20100909_MIN#NO 2010 MIN (8:38) A.Peterson left tackle to MIN 41 for 2 yards (J.Vilma S.Shanle). 1 3 61
20100909_MIN#NO 2010 MIN (7:51) A.Peterson right end to NO 42 for 17 yards (R.Gay). PENALTY on MIN-V.Shiancoe Offensive Holding 10 yards enforced at MIN 41 - No Play. 1 2 59
How do I get it to filter by season (2012) and/or team only?
Thanks in advance.
Use extra parentheses
Select gameid, season, off, description, qtr, down, ydstogoal
FROM 2010_12pxp
WHERE
(
DESCRIPTION LIKE "%up the middle%"
OR DESCRIPTION REGEXP "(left|right) (tackle|guard|end)"
OR DESCRIPTION REGEXP " rushe(d|s) for "
)
AND season = 2012
GROUP BY id
If you don't then your query will be interpreted like this
WHERE DESCRIPTION LIKE "%up the middle%"
OR DESCRIPTION REGEXP "(left|right) (tackle|guard|end)"
OR
(
DESCRIPTION REGEXP " rushe(d|s) for "
AND season = 2012
)
looks easy but difficult for me. the src/dest path of idx 1, 2, 3 has the same values.
so I need only 1 row for them.
idx Src_path dest_path code
1 /abc/aaa.txt /abc/dec_aaa.txt 01
2 /abc/aaa.txt /abc/dec_aaa.txt 02
3 /abc/aaa.txt /abc/dec_aaa.txt 03
4 /abc/aaa.txt /abc2/dec_aaa.txt 04
5 /abc/bbb.txt /abc2/dec_bbb.txt 01
6 /abc/ccc.txt /abc2/dec_ccc.txt 01
the result rows should be like below..
idx Src_path dest_path code
3 /abc/aaa.txt /abc/dec_aaa.txt 03
4 /abc/aaa.txt /abc2/dec_aaa.txt 04
5 /abc/bbb.txt /abc2/dec_bbb.txt 01
6 /abc/ccc.txt /abc2/dec_ccc.txt 01
bit.. difficult for me..
naw... kinda lazy you are, but thank god its friday.
SELECT MAX(idx), src_path, dest_path, MAX(code)
FROM yourtable
GROUP BY src_path, dest_path
should work out.
use SELECT DISTINCT Src_path
otherwise you can use
GROUP_CONCAT(Src_path) but then you will have to GROUP_BY