How I select record that not appear in another table - sql

Table: Movie
mID title year director
101 Gone with the Wind 1939 Victor Fleming
102 Star Wars 1977 George Lucas
103 The Sound of Music 1965 Robert Wise
104 E.T. 1982 Steven Spielberg
105 Titanic 1997 James Cameron
106 Snow White 1937 <null>
107 Avatar 2009 James Cameron
108 Raiders of the Lost Ark 1981 Steven Spielberg
Table: Rating
rID mID stars ratingDate
201 101 2 2011-01-22
201 101 4 2011-01-27
202 106 4 <null>
203 103 2 2011-01-20
203 108 4 2011-01-12
203 108 2 2011-01-30
204 101 3 2011-01-09
205 103 3 2011-01-27
205 104 2 2011-01-22
205 108 4 <null>
206 107 3 2011-01-15
206 106 5 2011-01-19
207 107 5 2011-01-20
208 104 3 2011-01-02
I need to fetch movies which are not rate yet. In this case Titanic (mID 105) and Star Wars (mID 102) never get rate in rating table.
I figured out it with
select distinct movie.title from movie,rating where
rating.mid!=movie.mid except select distinct movie.title from
movie,rating where rating.mid=movie.mid
however I think it might have better (easier/cleaner) way to do.

Simple:
SELECT Movies.* FROM Movies LEFT JOIN Rating ON Movies.mID = Rating.mID WHERE Rating.mID IS NULL

If I understood your question properly, that looks like textbook application of outer joins.

You could do it like this:
SELECT * FROM Movie WHERE mid NOT IN (SELECT DISTINCT(mid) FROM Rating)
Basically it will select all records from the movie table that are not in the rating table, linking them on the 'mid' column, which I am assuming is a unique identifier.

I will add another possibility.
Select [list columns here]
from Movie m
where NOT exists (SELECT * FROM RATING r where m.mid = r.mid)

Related

How should I write the JOIN clause to make it work well?

I'm doing Q4 (Find the titles of all movies not reviewed by Chris Jackson. ) from SQL Movie-Rating Query Exercises Extras and I don't know why this code doesn't work:
SELECT DISTINCT movie.title
FROM movie
INNER JOIN rating ON movie.mid = rating.mID
INNER JOIN reviewer ON rating.rid = reviewer.rid
WHERE rating.mid NOT IN (SELECT rating.mid FROM rating WHERE rating.rid = (SELECT reviewer.rid FROM reviewer WHERE reviewer.name = 'Chris Jackson') )
Output:
title
Gone with the Wind
Snow White
Avatar
This output doesn't include movies that ARE in movie table but ARE NOT in rating table. So I suspect maybe this has something to do with JOIN clause.
TABLES:
Movie
mID title year director
101 Gone with the Wind 1939 Victor Fleming
102 Star Wars 1977 George Lucas
103 The Sound of Music 1965 Robert Wise
104 E.T. 1982 Steven Spielberg
105 Titanic 1997 James Cameron
106 Snow White 1937 <null>
107 Avatar 2009 James Cameron
108 Raiders of the Lost Ark 1981 Steven Spielberg
Reviewer
rID name
201 Sarah Martinez
202 Daniel Lewis
203 Brittany Harris
204 Mike Anderson
205 Chris Jackson
206 Elizabeth Thomas
207 James Cameron
208 Ashley White
Rating
rID mID stars ratingDate
201 101 2 2011-01-22
201 101 4 2011-01-27
202 106 4 <null>
203 103 2 2011-01-20
203 108 4 2011-01-12
203 108 2 2011-01-30
204 101 3 2011-01-09
205 103 3 2011-01-27
205 104 2 2011-01-22
205 108 4 <null>
206 107 3 2011-01-15
206 106 5 2011-01-19
207 107 5 2011-01-20
208 104 3 2011-01-02
Firstly you must use LEFT JOIN and then then use GROUP BY movie.mid, movie.title and put the condition in the HAVING clause:
SELECT movie.title
FROM movie
LEFT JOIN rating ON movie.mid = rating.mID
LEFT JOIN reviewer ON rating.rid = reviewer.rid
GROUP BY movie.mid, movie.title
HAVING SUM(CASE WHEN reviewer.name = 'Chris Jackson' THEN 1 ELSE 0 END) = 0
See the demo.
Results:
> | title |
> | :----------------- |
> | Avatar |
> | Gone with the Wind |
> | Snow White |
> | Star Wars |
> | Titanic |
Although you can use aggregation, I would recommend NOT EXISTS for this purpose. It is close to the phrasing of the logic you want:
SELECT m.*
FROM movie m
WHERE NOT EXISTS (SELECT 1
FROM rating r JOIN
reviewer re
ON r.rid = re.rid
WHERE m.mid = r.mID AND
re.name = 'Chris Jackson'
);
There should simply be no comparison from a performance perspective. This should have much better performance.

How to Capture Original "Start With" ID as a Column in Oracle SQL LEVEL Query

Working with a large database of material being consumed as "inputs" to create various "outputs" over multiple generations. The final output (the product for market) can have potentially a dozen+ generations of inputs in its history. Each of these inputs has its own record.
Currently I'm creating a top-down view of this genealogy (meaning starting with final product and working back through all inputs) using LEVEL.
Simplified/Conceptual example of the code as follows:
SELECT
OL.LOT_NAME AS output_id,
IL.LOT_NAME AS input_id,
LEVEL
FROM GENEALOGY_TABLE G
INNER JOIN LOT_TABLE OL
on G.OUTPUT_LOT_KEY = OL.LOT_KEY
INNER JOIN LOT_TABLE IL
on G.INPUT_LOT_KEY = IL.LOT_KEY
START WITH OL.LOT_NAME IN ('X', 'Y', etc...)
CONNECT BY NOCYCLE PRIOR IL.LOT_NAME = OL.LOT_NAME
ORDER BY LEVEL
I am looking to add another column to this output table that holds the original "START WITH" value that is the origin of any the given record. Meaning that even if the record has a level of 10, I won't just see the level 9 output that that material created, but which of the multiple final products ('X', 'Y', etc... in the above example) that was ultimately created downstream.
Does Oracle have a function that can handle this? Is there a simple trick for this I'm missing? Any suggestions would be wonderful.
You can use the connect_by_root operator:
SELECT
OL.LOT_NAME AS output_id,
IL.LOT_NAME AS input_id,
LEVEL,
CONNECT_BY_ROOT(OL.LOT_NAME) AS STARTED_WITH
FROM GENEALOGY_TABLE G
...
Quick demo using HR-schema tables:
SELECT employee_id, last_name, manager_id, connect_by_root(manager_id)
FROM employees
START WITH manager_id in (101, 102)
CONNECT BY PRIOR employee_id = manager_id;
EMPLOYEE_ID LAST_NAME MANAGER_ID CONNECT_BY_ROOT(MANAGER_ID)
----------- ------------------------- ---------- ---------------------------
108 Greenberg 101 101
109 Faviet 108 101
110 Chen 108 101
111 Sciarra 108 101
112 Urman 108 101
113 Popp 108 101
200 Whalen 101 101
203 Mavris 101 101
204 Baer 101 101
205 Higgins 101 101
206 Gietz 205 101
103 Hunold 102 102
104 Ernst 103 102
105 Austin 103 102
106 Pataballa 103 102
107 Lorentz 103 102
16 rows selected.
With Oracle there is always a way. Use CONNECT_BY_ROOT.

Trying to do sum math using Partition By and Row_Number

I'm trying to add a few columns to my table and I'm a bit of the way there but not clear why it's failing. This is an example starting table...
Date Name Amount
1/2/2015 Andy 148
2/5/2015 Andy 188
2/11/2015 Andy 154
1/15/2015 John 136
2/5/2015 John 176
1/7/2015 John 134
1/19/2015 John 251
2/21/2015 Carlos 120
2/15/2015 Carlos 211
1/8/2015 Carlos 120
1/2/2014 Andy 151
2/5/2014 Andy 281
2/11/2014 Andy 298
1/15/2014 John 292
2/5/2014 John 134
1/7/2014 John 281
1/19/2014 John 101
2/21/2014 Carlos 137
2/15/2014 Carlos 108
1/8/2014 Carlos 292
I want to take the above table and...
1) Sort by Year, Name, Then Value
2) Based on #1, Add the "Ordered" column which gives a number for each set of Year and Name where Value is set sorted ascending
3) Multiplied column is multiplying Amount by Ordered
4) Sum the multiplied column and a the sum to each set
Result...
Date Year Name Amount Ordered Multiplied Sum
1/2/2014 2014 Andy 151 1 151 1607
2/5/2014 2014 Andy 281 2 562 1607
2/11/2014 2014 Andy 298 3 894 1607
2/15/2014 2014 Carlos 108 1 108 1258
2/21/2014 2014 Carlos 137 2 274 1258
1/8/2014 2014 Carlos 292 3 876 1258
1/19/2014 2014 John 101 1 101 2380
2/5/2014 2014 John 134 2 268 2380
1/7/2014 2014 John 281 3 843 2380
1/15/2014 2014 John 292 4 1168 2380
1/2/2015 2015 Andy 148 1 148 1020
2/11/2015 2015 Andy 154 2 308 1020
2/5/2015 2015 Andy 188 3 564 1020
1/8/2015 2015 Carlos 120 1 120 993
2/21/2015 2015 Carlos 120 2 240 993
2/15/2015 2015 Carlos 211 3 633 993
1/7/2015 2015 John 134 1 134 1938
1/15/2015 2015 John 136 2 272 1938
2/5/2015 2015 John 176 3 528 1938
1/19/2015 2015 John 251 4 1004 1938
I have everything but the last column as I keep getting the error...
'Invalid expression near Row_Number'.
SQL for 'Ordered'...
ROW_NUMBER() OVER ( Partition BY Name, DATEPART(YEAR, Date) ORDER BY Amount ) AS 'Ordered'
SQL for 'Multiplied'...
Amount * Ordered AS Multiplied
Now I could be thinking of this naively but I thought I could just do add a line like this...
sum(Multiplied) OVER ( Partition BY Name, DATEPART(YEAR, Date) ORDER BY Amount ) AS 'Sum'
But I keep getting the error mentioned. Any ideas how to handle? I'm welcome to hearing other ways of handling the data. I only care about the last column
If your syntax worked, it would produce a cumulative sum. That doesn't appear to be what you want.
I think you can do what you want with a subquery:
select t.*,
(seqnum * amount) as multiplied,
sum(seqnum * amount) over (partition by name, year(date)) as thesum
from (select t.*,
row_number() over (partition by name, year(date) order by date) as seqnum
from table t
) t;

Copy rows of data in SQL Server

Please help me come up with a solution for the situation being explained below:
ID name address age hobby GPA
---------------------------------------------------------
101 James 100 Garfield St 21 reading 3.13
101 James 100 Garfield St 21 writing 2.63
101 James 100 Garfield St 21 running 3.81
109 Tom 19 Lily Ave 19 dating 3.54
109 Tom 20 Lily Ave 19 climbing 2.76
109 Tom 21 Lily Ave 19 watching 3.91
I want to copy the set of rows with the same ID (eg. 101) and assign each set a State abbreviation(s) by running a single sql query. For instance: adding states CA, NJ, and DE to rows with an ID of 101, the result set is expected to look like this:
ID name address age hobby GPA state
-----------------------------------------------------------------------
101 James 100 Garfield St 21 reading 3.13 CA
101 James 100 Garfield St 21 writing 2.63 CA
101 James 100 Garfield St 21 running 3.81 CA
101 James 100 Garfield St 21 reading 3.13 NJ
101 James 100 Garfield St 21 writing 2.63 NJ
101 James 100 Garfield St 21 running 3.81 NJ
101 James 100 Garfield St 21 reading 3.13 DE
101 James 100 Garfield St 21 writing 2.63 DE
101 James 100 Garfield St 21 running 3.81 DE
Please keep in mind that everything else remains the same way as they were before the addition of the state abbreviations. Also assume I have more than three states to add and integrate to the query, say, I have all 50 states. Thank you for your time and effort in advance!
This should produce that result set:
select x.*, y.st
from tbl x
join
(select 'CA' as st union all
select 'NJ' union all
select 'DE') y
where x.id = 101
Create a new table with IDs and States
ID ST
101 CA
101 NJ
101 DE
109 ..
then join that on your table
SELECT t.*, s.st
FROM tbl t
JOIN states s ON t.id = s.id

ORA 00918- Column ambiguosly defined error [duplicate]

This question already has answers here:
ORA-00918: column ambiguously defined in SELECT *
(4 answers)
Closed 9 years ago.
There are two tables in my Oracle database.
First table is (customers)-
customer_id Customer_name Customer_age Customer_address salary
103 Giriraj Rathi 22 Kolkata 12000
100 Subir Adhikari 22 Bolpur 10000
101 Rakesh Chatterjee 21 Tarkeshwar 8000
102 Jayanta Patra 20 Tarkeshwar 9000
104 Abhi Karmakar 22 Burdwan 8000
105 Mainak Manna 21 Burdwan 9000
106 Subho Gupta 20 Kolkata 10000
107 Aritra Das 23 Kolkata 7000
108 Pradip Paul 22 Kolkata 5000
109 Sourav Banerjee 22 Bolpur 9000
Second table is (Orders):
Order_id Order_date customer_id amount
200 12-03-13 100 1100
201 09-05-13 101 1400
202 07-04-12 103 2500
204 29-05-13 104 2400
203 09-02-13 105 9000
205 18-06-13 106 2100
206 09-07-13 107 1600
207 18-05-13 108 2900
209 18-04-13 109 2400
Now I wanted to join both the tables. So I used the query:
select customer_id,
customer_name,
customer_address,
order_id,order_date,
amount
from customers,
orders
where customers.customer_id=orders.customer_id;
I Googled about the error and found this happens when there is ambiguity in the SQL code itself, but in this case I see nothing.
It is always a good idea to add the table name/alias to the column like this
select c.customer_id,
c.customer_name,
c.customer_address,
o.order_id,
o.order_date,
o.amount
from customers c
inner join orders o on c.customer_id = o.customer_id
If you don't then the DB don't know which column to take and both tables have a column named customer_id.