I am looking for an output like:
| BOOK | ANALYST | SUPERVISOR |
|-------|----------------|--------------------|
| BookA | (null) | Dani Sant |
| BookB | (null) | North Andre Miles |
| BookC | Andrea Plus | Andrea Plus |
| BookD | Jeff Dron Math | Jeff Dron Math |
| BookE | Theo Phillip | Julian Rhode |
What I am getting is:
| BOOK | ANALYST | SUPERVISOR |
|-------|----------------|--------------|
| BookA | (null) | dani.sant |
| BookB | (null) | north.miles |
| BookC | Andrea Plus | andrea.plus |
| BookD | Jeff Dron Math | jeff.math |
| BookE | Theo Phillip | julian.rhode |
I can do the join with one column, but when I try for both, the result isn't showing like it should. Thanks for any information on this.
SQL Fiddle
MS SQL Server 2008 Schema Setup:
CREATE TABLE books
(
book varchar(10),
analyst varchar(100),
supervisor varchar(100)
);
INSERT INTO books (book, analyst, supervisor)
VALUES
('BookA', NULL, 'dani.sant'),
('BookB', NULL, 'north.miles'),
('BookC', 'andrea.plus', 'andrea.plus'),
('BookD', 'jeff.math', 'jeff.math'),
('BookE', 'theo.phil', 'julian.rhode');
CREATE TABLE names
(
username varchar(100),
fullname varchar(500)
);
INSERT INTO names (username, fullname)
VALUES
('dani.sant', 'Dani Sant'),
('north.miles', 'North Andre Miles'),
('andrea.plus', 'Andrea Plus'),
('jeff.math', 'Jeff Dron Math'),
('theo.phil', 'Theo Phillip'),
('julian.rhode', 'Julian Rhode');
Query 1:
SELECT
books.book AS Book,
names.fullname AS Analyst,
books.supervisor AS Supervisor
FROM
books left join names on books.analyst = names.username
Results:
| BOOK | ANALYST | SUPERVISOR |
|-------|----------------|--------------|
| BookA | (null) | dani.sant |
| BookB | (null) | north.miles |
| BookC | Andrea Plus | andrea.plus |
| BookD | Jeff Dron Math | jeff.math |
| BookE | Theo Phillip | julian.rhode |
You need a second join to the names table to get the supervisor's full name:
SELECT b.book AS Book, bn.fullname AS Analyst,
sn.fullname AS Supervisor
FROM books b left join
names bn
on b.analyst = bn.username left join
names sn
on b.supervisor = sn.username;
Below will provide the output you desire.
SELECT
b.book AS Book,
n.fullname AS Analyst,
(SELECT fullname FROM names where username=b.Supervisor) AS Supervisor
FROM
books b left join names n on b.analyst = n.username
Related
I have the following table,
CREATE TABLE AGENT (
A_CODE int,
A_FNAME varchar(15),
S_CODE int /* supervisor */
);
INSERT INTO AGENT VALUES(1,'John',1);
INSERT INTO AGENT VALUES(2,'Nancy',1);
INSERT INTO AGENT VALUES(3,'Lottie',2);
INSERT INTO AGENT VALUES(4,'Jennie',1);
INSERT INTO AGENT VALUES(5,'Robert',3);
INSERT INTO AGENT VALUES(6,'Cary',1);
INSERT INTO AGENT VALUES(7,'Roberto',3);
INSERT INTO AGENT VALUES(8,'Elizabeth',1);
INSERT INTO AGENT VALUES(9,'Jack',2);
INSERT INTO AGENT VALUES(10,'Rose',4);
INSERT INTO AGENT VALUES(11,'Tom',3);
INSERT INTO AGENT VALUES(12,'Alan',2);
INSERT INTO AGENT VALUES(13,'Peter',3);
INSERT INTO AGENT VALUES(14,'Sherry',4);
INSERT INTO AGENT VALUES(15,'Howard',5);
INSERT INTO AGENT VALUES(16,'Barry',5);
INSERT INTO AGENT VALUES(17,'Jeanine',4);
INSERT INTO AGENT VALUES(18,'Andrew',4);
INSERT INTO AGENT VALUES(19,'Peter',4);
INSERT INTO AGENT VALUES(20,'Robert',1);
INSERT INTO AGENT VALUES(21,'Jennifer',3);
My goal is to create a supervisor view in which there is a 4th column that has the name of the supervisor that matches the S code.
My query returns to me either nothing except the column headers or a never ending recursion that loops into the 100,000's.
What I've written is
WITH SUPERVISOR AS
(
SELECT A_CODE, A_FNAME, S_CODE, CAST('' AS VARCHAR(50)) S_NAME FROM AGENT
UNION ALL
SELECT A.A_CODE,A.A_FNAME, A.S_CODE,CAST(S.A_FNAME AS VARCHAR(50)) S_NAME FROM AGENT AS A
INNER JOIN SUPERVISOR AS S ON S.A_CODE=A.S_CODE
)
SELECT * FROM SUPERVISOR
option (maxrecursion 0);
Returns endlessly,
or
WITH SUPERVISOR AS
(
SELECT A_CODE, A_FNAME, S_CODE, CAST('' AS VARCHAR(50)) S_NAME FROM AGENT WHERE S_CODE is NULL
UNION ALL
SELECT A.A_CODE,A.A_FNAME, A.S_CODE,CAST(S.A_FNAME AS VARCHAR(50)) S_NAME FROM AGENT AS A
INNER JOIN SUPERVISOR AS S ON S.A_CODE=A.S_CODE
)
SELECT * FROM SUPERVISOR
option (maxrecursion 0);
I've been trying to read about this and figure it out on my own but at this point I've struggled for long enough that I think I need it explained to me as to why it's not working or how wildly wrong I'm going about this.
Please try the following solution. It is using self-join approach with two aliases for the same table: e(mployee) and s(upervisor).
SQL
-- DDL and sample data population, start
DECLARE #AGENT TABLE (A_CODE INT PRIMARY KEY, A_FNAME varchar(15), S_CODE int /* supervisor */);
INSERT INTO #AGENT VALUES
(1,'John',1)
,(2,'Nancy',1)
,(3,'Lottie',2)
,(4,'Jennie',1)
,(5,'Robert',3)
,(6,'Cary',1)
,(7,'Roberto',3)
,(8,'Elizabeth',1)
,(9,'Jack',2)
,(10,'Rose',4)
,(11,'Tom',3)
,(12,'Alan',2)
,(13,'Peter',3)
,(14,'Sherry',4)
,(15,'Howard',5)
,(16,'Barry',5)
,(17,'Jeanine',4)
,(18,'Andrew',4)
,(19,'Peter',4)
,(20,'Robert',1)
,(21,'Jennifer',3);
-- DDL and sample data population, end
SELECT e.*, s.A_FNAME AS Supervisor
FROM #AGENT AS e INNER JOIN
#AGENT AS s ON s.A_CODE = e.S_CODE;
Output
+--------+-----------+--------+------------+
| A_CODE | A_FNAME | S_CODE | Supervisor |
+--------+-----------+--------+------------+
| 1 | John | 1 | John |
| 2 | Nancy | 1 | John |
| 3 | Lottie | 2 | Nancy |
| 4 | Jennie | 1 | John |
| 5 | Robert | 3 | Lottie |
| 6 | Cary | 1 | John |
| 7 | Roberto | 3 | Lottie |
| 8 | Elizabeth | 1 | John |
| 9 | Jack | 2 | Nancy |
| 10 | Rose | 4 | Jennie |
| 11 | Tom | 3 | Lottie |
| 12 | Alan | 2 | Nancy |
| 13 | Peter | 3 | Lottie |
| 14 | Sherry | 4 | Jennie |
| 15 | Howard | 5 | Robert |
| 16 | Barry | 5 | Robert |
| 17 | Jeanine | 4 | Jennie |
| 18 | Andrew | 4 | Jennie |
| 19 | Peter | 4 | Jennie |
| 20 | Robert | 1 | John |
| 21 | Jennifer | 3 | Lottie |
+--------+-----------+--------+------------+
I am having trouble querying some data. The table I am trying to pull the data from is a LOG table, where I would like to see changes in the values next to each other (example below)
Table:
+-----------+----+-------------+----------+------------+
| UNIQUE_ID | ID | NAME | CITY | DATE |
+-----------+----+-------------+----------+------------+
| xa220 | 1 | John Smith | Berlin | 2020.05.01 |
| xa195 | 1 | John Smith | Berlin | 2020.03.01 |
| xa111 | 1 | John Smith | München | 2020.01.01 |
| xa106 | 2 | James Brown | Atlanta | 2018.04.04 |
| xa100 | 2 | James Brown | Boston | 2017.12.10 |
| xa76 | 3 | Emily Wolf | Shanghai | 2016.11.03 |
| xa20 | 3 | Emily Wolf | Shanghai | 2016.07.03 |
| xa15 | 3 | Emily Wolf | Tokyo | 2014.02.22 |
| xa12 | 3 | Emily Wolf | null | 2014.02.22 |
+-----------+----+-------------+----------+------------+
Desired outcome:
+----+-------------+----------+---------------+
| ID | NAME | CITY | PREVIOUS_CITY |
+----+-------------+----------+---------------+
| 1 | John Smith | Berlin | München |
| 2 | James Brown | Atlanta | Boston |
| 3 | Emily Wolf | Shanghai | Tokyo |
| 3 | Emily Wolf | Tokyo | null |
+----+-------------+----------+---------------+
I have been trying to use FIRST and LAST values, however, cannot get the desired outcome.
select distinct id,
name,
city,
first_value(city) over (partition by id order by city) as previous_city
from test
Any help is appreciated!
Thank you!
Use the LAG function to get the city for previous date and display only the rows where current city and the result of lag are different:
WITH cte AS (
SELECT t.*, LAG(CITY, 1, CITY) OVER (PARTITION BY ID ORDER BY "DATE") LAG_CITY
FROM yourTable t
)
SELECT ID, NAME, CITY, LAG_CITY AS PREVIOUS_CITY
FROM cte
WHERE
CITY <> LAG_CITY OR
CITY IS NULL AND LAG_CITY IS NOT NULL OR
CITY IS NOT NULL AND LAG_CITY IS NULL
ORDER BY
ID, "DATE" DESC;
Demo
Some comments on how LAG is being used and its values checked are warranted. We use the three parameter version of LAG here. The second parameter means the number of records to look back, which in this case is 1 (the default). The third parameter means the default value to use should a given record per ID partition be the first. In this case, we use the default as the same CITY value. This means that the first record would never appear in the result set.
For the WHERE clause above, a matching record is one for which the city and lag city are different, or for where one of the two be NULL and the other not NULL. This is the logic needed to treat a NULL city and some not NULL city value as being different.
The concerned tables are as follows:
students(rollno, name, deptcode)
depts(deptcode, deptname)
course(crs_rollno, crs_name, marks)
The query is
Find the name and roll number of the students from each department who obtained
highest total marks in their own department.
Consider:
i) Courses of different department are different.
ii) All students of a particular department take same number and same courses.
Then only the query makes sense.
I wrote a successful query for displaying the maximum total marks by a student in each department.
select do.deptname, max(x.marks) from students so
inner join depts do
on do.deptcode=so.deptcode
inner join(
select s.name as name, d.deptname as deptname, sum(c.marks) as marks from students s
inner join crs_regd c
on s.rollno=c.crs_rollno
inner join depts d
on d.deptcode=s.deptcode
group by s.name,d.deptname) x
on x.name=so.name and x.deptname=do.deptname group by do.deptname;
But as mentioned I need to display the name as well. Accordingly if I include so.name in select list, I need to include it in group by clause and the output is as below:
Kendra Summers Computer Science 274
Stewart Robbins English 80
Cole Page Computer Science 250
Brian Steele English 83
expected output:
Kendra Summers Computer Science 274
Brian Steele English 83
Where is the problem?
I guess this can be easily achieved if you use window function -
select name, deptname, marks
from (select s.name as name, d.deptname as deptname, sum(c.marks) as marks,
row_number() over(partition by d.deptname order by sum(c.marks) desc) rn
from students s
inner join crs_regd c on s.rollno=c.crs_rollno
inner join depts d on d.deptcode=s.deptcode
group by s.name,d.deptname) x
where rn = 1;
To solve the problem with a readable query I had to define a couple of views:
total_marks: For each student the sum of their marks
create view total_marks as select s.deptcode, s.name, s.rollno, sum(c.marks) as total from course c, students s where s.rollno = c.crs_rollno group by s.rollno;
dept_max: For each department the highest total score by a single student of that department
create view dept_max as select deptcode, max(total) max_total from total_marks group by deptcode;
So I can get the desidered output with the query
select a.deptcode, a.rollno, a.name from total_marks a join dept_max b on a.deptcode = b.deptcode and a.total = b.max_total
If you don't want to use views you can replace their selects on the final query, which will result in this:
select a.deptcode, a.rollno, a.name
from
(select s.deptcode, s.name, s.rollno, sum(c.marks) as total from course c, students s where s.rollno = c.crs_rollno group by s.rollno) a
join (select deptcode, max(total) max_total from (select s.deptcode, s.name, s.rollno, sum(c.marks) as total from course c, students s where s.rollno = c.crs_rollno group by s.rollno) a_ group by deptcode) b
on a.deptcode = b.deptcode and a.total = b.max_total
Which I'm sure it is easily improvable in performance by someone more skilled then me...
If you (and anybody else) want to try it the way I did, here is the schema:
create table depts ( deptcode int primary key auto_increment, deptname varchar(20) );
create table students ( rollno int primary key auto_increment, name varchar(20) not null, deptcode int, foreign key (deptcode) references depts(deptcode) );
create table course ( crs_rollno int, crs_name varchar(20), marks int, foreign key (crs_rollno) references students(rollno) );
And here all the entries I inserted:
insert into depts (deptname) values ("Computer Science"),("Biology"),("Fine Arts");
insert into students (name,deptcode) values ("Turing",1),("Jobs",1),("Tanenbaum",1),("Darwin",2),("Mendel",2),("Bernard",2),("Picasso",3),("Monet",3),("Van Gogh",3);
insert into course (crs_rollno,crs_name,marks) values
(1,"Algorithms",25),(1,"Database",28),(1,"Programming",29),(1,"Calculus",30),
(2,"Algorithms",24),(2,"Database",22),(2,"Programming",28),(2,"Calculus",19),
(3,"Algorithms",21),(3,"Database",27),(3,"Programming",23),(3,"Calculus",26),
(4,"Zoology",22),(4,"Botanics",28),(4,"Chemistry",30),(4,"Anatomy",25),(4,"Pharmacology",27),
(5,"Zoology",29),(5,"Botanics",27),(5,"Chemistry",26),(5,"Anatomy",25),(5,"Pharmacology",24),
(6,"Zoology",18),(6,"Botanics",19),(6,"Chemistry",22),(6,"Anatomy",23),(6,"Pharmacology",24),
(7,"Sculpture",26),(7,"History",25),(7,"Painting",30),
(8,"Sculpture",29),(8,"History",24),(8,"Painting",30),
(9,"Sculpture",21),(9,"History",19),(9,"Painting",25) ;
Those inserts will load these data:
select * from depts;
+----------+------------------+
| deptcode | deptname |
+----------+------------------+
| 1 | Computer Science |
| 2 | Biology |
| 3 | Fine Arts |
+----------+------------------+
select * from students;
+--------+-----------+----------+
| rollno | name | deptcode |
+--------+-----------+----------+
| 1 | Turing | 1 |
| 2 | Jobs | 1 |
| 3 | Tanenbaum | 1 |
| 4 | Darwin | 2 |
| 5 | Mendel | 2 |
| 6 | Bernard | 2 |
| 7 | Picasso | 3 |
| 8 | Monet | 3 |
| 9 | Van Gogh | 3 |
+--------+-----------+----------+
select * from course;
+------------+--------------+-------+
| crs_rollno | crs_name | marks |
+------------+--------------+-------+
| 1 | Algorithms | 25 |
| 1 | Database | 28 |
| 1 | Programming | 29 |
| 1 | Calculus | 30 |
| 2 | Algorithms | 24 |
| 2 | Database | 22 |
| 2 | Programming | 28 |
| 2 | Calculus | 19 |
| 3 | Algorithms | 21 |
| 3 | Database | 27 |
| 3 | Programming | 23 |
| 3 | Calculus | 26 |
| 4 | Zoology | 22 |
| 4 | Botanics | 28 |
| 4 | Chemistry | 30 |
| 4 | Anatomy | 25 |
| 4 | Pharmacology | 27 |
| 5 | Zoology | 29 |
| 5 | Botanics | 27 |
| 5 | Chemistry | 26 |
| 5 | Anatomy | 25 |
| 5 | Pharmacology | 24 |
| 6 | Zoology | 18 |
| 6 | Botanics | 19 |
| 6 | Chemistry | 22 |
| 6 | Anatomy | 23 |
| 6 | Pharmacology | 24 |
| 7 | Sculpture | 26 |
| 7 | History | 25 |
| 7 | Painting | 30 |
| 8 | Sculpture | 29 |
| 8 | History | 24 |
| 8 | Painting | 30 |
| 9 | Sculpture | 21 |
| 9 | History | 19 |
| 9 | Painting | 25 |
+------------+--------------+-------+
I take chance to point out that this database is badly designed. This becomes evident with course table. For these reasons:
The name is singular
This table does not represent courses, but rather exams or scores
crs_name should be a foreign key referencing the primary key of another table (that would actually represent the courses)
There is no constrains to limit the marks to a range and to avoid a student to take twice the same exam
I find more logical to associate courses to departments, instead of student to departments (this way also would make these queries easier)
I tell you this because I understood you are learning from a book, so unless the book at one point says "this database is poorly designed", do not take this exercise as example to design your own!
Anyway, if you manually resolve the query with my data you will come to this results:
+----------+--------+---------+
| deptcode | rollno | name |
+----------+--------+---------+
| 1 | 1 | Turing |
| 2 | 6 | Bernard |
| 3 | 8 | Monet |
+----------+--------+---------+
As further reference, here the contents of the views I needed to define:
select * from total_marks;
+----------+-----------+--------+-------+
| deptcode | name | rollno | total |
+----------+-----------+--------+-------+
| 1 | Turing | 1 | 112 |
| 1 | Jobs | 2 | 93 |
| 1 | Tanenbaum | 3 | 97 |
| 2 | Darwin | 4 | 132 |
| 2 | Mendel | 5 | 131 |
| 2 | Bernard | 6 | 136 |
| 3 | Picasso | 7 | 81 |
| 3 | Monet | 8 | 83 |
| 3 | Van Gogh | 9 | 65 |
+----------+-----------+--------+-------+
select * from dept_max;
+----------+-----------+
| deptcode | max_total |
+----------+-----------+
| 1 | 112 |
| 2 | 136 |
| 3 | 83 |
+----------+-----------+
Hope I helped!
Try the following query
select a.name, b.deptname,c.marks
from students a
, crs_regd b
, depts c
where a.rollno = b.crs_rollno
and a.deptcode = c.deptcode
and(c.deptname,b.marks) in (select do.deptname, max(x.marks)
from students so
inner join depts do
on do.deptcode=so.deptcode
inner join (select s.name as name
, d.deptname as deptname
, sum(c.marks) as marks
from students s
inner join crs_regd c
on s.rollno=c.crs_rollno
inner join depts d
on d.deptcode=s.deptcode
group by s.name,d.deptname) x
on x.name=so.name
and x.deptname=do.deptname
group by do.deptname
)
Inner/Sub query will fetch the course name and max marks and the outer query gets the corresponding name of the student.
try and let know if you got the desired result
Dense_Rank() function would be helpful in this scenario:
SELECT subquery.*
FROM (SELECT Student_Total_Marks.rollno,
Student_Total_Marks.name,
Student_Total_Marks.deptcode, depts.deptname,
rank() over (partition by deptcode order by total_marks desc) Student_Rank
FROM (SELECT Stud.rollno,
Stud.name,
Stud.deptcode,
sum(course.marks) total_marks
FROM students stud inner join course course on stud.rollno = course.crs_rollno
GROUP BY stud.rollno,Stud.name,Stud.deptcode) Student_Total_Marks,
dept dept
WHERE Student_Total_Marks.deptcode = dept.deptname
GROUP BY Student_Total_Marks.deptcode) subquery
WHERE suquery.student_rank = 1
Suppose I have the following tables:
vs_tblMovies
MOVIEID MOVIENAME
11 Star Wars
vs_tblGenreBridge
MOVIEID GENREID
11 878
11 28
11 12
vs_tblGenres
GENREID GENRETITLE
28 Action
12 Adventure
878 Science Fiction
vs_tblActors
ACTORID STAGELNAME STAGEFNAME
1 Lucas George
vs_tblCastMembers
CASTMEMBERROLEID MOVIEID ACTORID
351 11 1
352 11 1
353 11 1
vs_tblCastMemberRoles
CASTMEMBERROLEID CASTMEMBERROLETITLE CASTMEMBERROLEDESC
351 Directing Director
352 Production Executive Producer
353 Writing Writer
I want display all of the roles a given actor has had, with the result set being in the following format:
GENRETITLE MOVIENAME ACTORID STAGELNAME STAGEFNAME CASTMEMBERROLEID CASTMEMBERROLEDESC
To do this, I wrote the following query (getting the roles for ActorID = 1, which is George Lucas):
SELECT vs_tblGenres.GenreTitle,
vs_tblMovies.MovieName,
vs_tblActors.ActorID,
vs_tblActors.StageLName,
vs_tblActors.StageFName,
vs_tblCastMembers.CastMemberRoleID,
vs_tblCastMemberRoles.CastMemberRoleDesc
FROM vs_tblCastMembers
INNER JOIN vs_tblActors ON vs_tblCastMembers.ActorID = vs_tblActors.ActorID
INNER JOIN vs_tblMovies ON vs_tblCastMembers.MovieID = vs_tblMovies.MovieID
INNER JOIN vs_tblGenreBridge ON vs_tblMovies.MovieID = vs_tblGenreBridge.MovieID
INNER JOIN vs_tblGenres ON vs_tblGenreBridge.GenreID = vs_tblGenres.GenreID
INNER JOIN vs_tblCastMemberRoles ON vs_tblCastMembers.CastMemberRoleID = vs_tblCastMemberRoles.CastMemberRoleID
WHERE vs_tblActors.ActorID = 1
GROUP BY vs_tblGenres.GenreTitle,
vs_tblMovies.MovieName,
vs_tblActors.ActorID,
vs_tblActors.StageLName,
vs_tblActors.StageFName,
vs_tblCastMembers.CastMemberRoleID,
vs_tblCastMemberRoles.CastMemberRole Desc
Which Outputs:
| GENRETITLE | MOVIENAME | ACTORID | STAGELNAME | STAGEFNAME | CASTMEMBERROLEID | CASTMEMBERROLEDESC |
|-----------------|-----------|---------|------------|------------|------------------|--------------------|
| Science Fiction | Star Wars | 1 | Lucas | George | 352 | Executive Producer |
| Adventure | Star Wars | 1 | Lucas | George | 352 | Executive Producer |
| Action | Star Wars | 1 | Lucas | George | 351 | Director |
| Adventure | Star Wars | 1 | Lucas | George | 351 | Director |
| Science Fiction | Star Wars | 1 | Lucas | George | 353 | Writer |
| Science Fiction | Star Wars | 1 | Lucas | George | 351 | Director |
| Action | Star Wars | 1 | Lucas | George | 353 | Writer |
| Adventure | Star Wars | 1 | Lucas | George | 353 | Writer |
| Action | Star Wars | 1 | Lucas | George | 352 | Executive Producer |
What I want to do is to merge the cases where only the GenreTitle is different, so it doesn't list the role multiple times for each genre. The ideal ouput would be something like this:
| GENRETITLE | MOVIENAME | ACTORID | STAGELNAME | STAGEFNAME | CASTMEMBERROLEID | CASTMEMBERROLEDESC |
|------------------------------------|-----------|---------|------------|------------|------------------|--------------------|
| Action, Adventure, Science Fiction | Star Wars | 1 | Lucas | George | 352 | Executive Producer |
| Action, Adventure, Science Fiction | Star Wars | 1 | Lucas | George | 351 | Director |
| Action, Adventure, Science Fiction | Star Wars | 1 | Lucas | George | 353 | Writer |
What is the simplest way to do this in Oracle 12c?
You are almost there. All you have to do is pull out Oracle LISTAGG aggregate function to agglutinate all GenreTitles together, with respect to other GROUP BY fields. Accordingly, you need to remove GenreTitle from the GROUP BY clause.
From the docs :
For a specified measure, LISTAGG orders data within each group specified in the ORDER BY clause and then concatenates the values of the measure column.
Updated query :
SELECT LISTAGG(vs_tblGenres.GenreTitle, ', ') WITHIN GROUP (ORDER BY vs_tblGenres.GenreTitle) AS GenreTitle,
vs_tblMovies.MovieName,
vs_tblActors.ActorID,
vs_tblActors.StageLName,
vs_tblActors.StageFName,
vs_tblCastMembers.CastMemberRoleID,
vs_tblCastMemberRoles.CastMemberRoleDesc
FROM vs_tblCastMembers
INNER JOIN vs_tblActors ON vs_tblCastMembers.ActorID = vs_tblActors.ActorID
INNER JOIN vs_tblMovies ON vs_tblCastMembers.MovieID = vs_tblMovies.MovieID
INNER JOIN vs_tblGenreBridge ON vs_tblMovies.MovieID = vs_tblGenreBridge.MovieID
INNER JOIN vs_tblGenres ON vs_tblGenreBridge.GenreID = vs_tblGenres.GenreID
INNER JOIN vs_tblCastMemberRoles ON vs_tblCastMembers.CastMemberRoleID = vs_tblCastMemberRoles.CastMemberRoleID
WHERE vs_tblActors.ActorID = 1
GROUP BY vs_tblMovies.MovieName,
vs_tblActors.ActorID,
vs_tblActors.StageLName,
vs_tblActors.StageFName,
vs_tblCastMembers.CastMemberRoleID,
vs_tblCastMemberRoles.CastMemberRole Desc
Let consider example: I have following tables - TableA with people and TableB containing language skills of these people. Each row describing person can have none, one or more rows in TableB. Example below:
People
+-----+--------+
| pId | Name |
+-----+--------+
| 0 | Thomas |
| 1 | Henry |
| 2 | John |
+-----+--------+
Skills
+-----+-----+----------+---------------+
| lID | pId | Language | LanguageSkill |
+-----+-----+----------+---------------+
| 0 | 0 | Dutch | 0 |
| 1 | 0 | French | 4 |
| 2 | 0 | Italian | 2 |
| 3 | 2 | Italian | 2 |
+-----+-----+----------+---------------+
Thomas knows dutch, french and italian, Henry doesn't know any foreign language and John knows italian.
What I want to get is the best known language for each person from TableA:
+--------+----------+
| Name | Language |
+--------+----------+
| Thomas | French |
| Henry | NULL |
| John | Italian |
+--------+----------+
I have feeling that is quite easy thing, but don't have idea how to achieve it in a simple way.
Thanks for your responses.
You need to get the best language for each person using the following query:
SELECT pid, language
from TableB
group by pid
having languageskill = max(languageskill)
Then you join it onto the People table:
SELECT a.name, b.language
from TableA a
LEFT JOIN
(
SELECT pid, language, languageskill
from TableB
group by pid
having languageskill = max(languageskill)
) b
ON a.pid = b.pid
Of course, this method would not give more than one row if the person had a 'tied' best language, and you would lose that data about the 'tied' best language.