Need assistance with an SQL Query - sql

Here are the associated tables:
movie
(
mvnumb int,
mvtitle char(100),
yrmde int,
mvtype char(9),
crit int,
mpaa char(6),
noms int,
awrd int,
dirnumb int
)
director
(
dirnumb int,
dirname char(36),
dirborn int,
dirdied int
)
My goal is to construct an SQL query that lists the name of the director who has received the maximum number of awards (awrd). I can't seem to get this to work ... any help would be greatly appreciated.. thanks so much.

Something like this (SQL Server):
select top 1 d.dirname,sum(awrd) awrd
from director d
inner join movie m
on m.dirnumb=d.dirnumb
group by d.dirname
order by sum(awrd) desc
Or, on Oracle:
select * from (
select d.dirname, sum(awrd) awards
from director d
inner join movie m
on m.dirnumb=d.dirnumb
group by d.dirname
order by sum(awrd) desc )
where rownum<2;
EDIT: modified Oracle query, as #pilcrow suggested.

Related

Useage of aggregate function but without having clause

Im have the following two tables created:
create table partei(
id int not null primary key ,
name varchar(20),
vorsitzender varchar(20)
);
create table abgeordneter(
name varchar(20),
partei int references partei ,
wahlkreis varchar(20)
)
How can I change this Select-Statement:
SELECT a.Partei
FROM Abgeordneter a, Partei p
WHERE a.Partei = p.ID
GROUP BY a.Partei
HAVING COUNT(a.Name) < 5
Into a statement which doesn't use the having clause, but delivers exactly the same results? Is it even possible?
You can use a subquery an eliminate the JOIN:
SELECT Partei
FROM (SELECT a.Partei, COUNT(*) as cnt
FROM Abgeordneter ap
GROUP BY a.Partei
) a
WHERE cnt < 5;

List the names of adopters who have adopted more than 3 animals

These are the tables
Animals
(ID: int, Name: varchar(15), PrevOwner: varchar(15), DateAdmitted: date, Type: varchar(15))
Adopter
(SIN: int, Name: varchar(15), Address: varchar(15), OtherAnimals: int)
Adoption
(AnimalID: int, SIN: int, AdoptDate: date, chipNo: int)
I want to List the names of adopters who have adopted more than 3 animals
This query gives an error:
select distinct Name
from Adopter, Adoption
where Adoption.SIN = Adopter.SIN
GROUP BY Adoption.SIN
Having count(SIN) > 3;
111
You shouldn't join like that... that syntax for joins is over 20 years old. Use the new syntax
Then if you want names you have to group by names.
Like this:
select Adoption.Name
from Adopter
join Adoption on Adoption.SIN = Adopter.SIN
GROUP BY Adoption.Name
Having count(*) > 3;
You must to inform the name of table in HAVING because the column "SIN" is ambiguous
Try this:
select distinct Adoption.Name from Adopter,Adoption
where Adoption.SIN = Adopter.SIN
GROUP BY Adoption.SIN
Having count(Adoption.SIN) > 3;

SQL Query to Return Results If All the ID's Have Matches

I have two tables:
COUNTRY, with the two columns COUNTRY_ID and PERSON_REGION_ID.
PERSON, with many columns, in which PERSON_REGION_ID column is same as COUNTRY.PERSON_REGION_ID, and PERSON_ID is ID column of PERSON.
Query is as follows:
SELECT *
from COUNTRY
where PERSON_REGION_ID IN (
SELECT PERSON_REGION_ID
FROM PERSON
WHERE PERSON_ID IN (111, 888)))
AND COUNTRY_ID = 44;
The above query gives results if any one of the ID as matches (111 or 888).
I want the query to give results if both 111 and 888 has matches else return no results.
How this can be achieved?
I would prefer using joins here
EDIT: To answer your comment, it depends if it's procedure or just query. But you declare variables and go with that
By the way, just for the record ... this is T-SQL, not oracle's syntax
declare #CountryID int -- = 44? (if for some reason you keep CountryID as type other
-- then int, just change it to correct one)
declare #Person1 int -- = 111?
declare #Person2 int -- = 888?
select C.* from Country C
join Person P1 on C.Person_Region_ID = P1.PersionRegion_ID and P1.Country_ID = #CountryID
join Person P2 on C.Person_Region_ID = P2.PersionRegion_ID and P2.Country_ID = #CountryID
where P1.PersionID = #Person1 and P2.PersionID = #Person2
One option is to use group by with having in the subquery:
select *
from COUNTRY
where PERSON_REGION_ID IN (
select PERSON_REGION_ID
from PERSON
where PERSON_ID IN ( 111, 888))
group by PERSON_REGION_ID
having count(PERSON_ID) = 2)
and COUNTRY_ID= 44;
If there are duplicate person_id's per PERSON_REGION_ID , you'll need to use distinct with the count.
You can use EXISTS() like this:
SELECT * from COUNTRY t
WHERE EXISTS(SELECT 1 FROM PERSON s
WHERE t.PERSON_REGION_ID = s.PERSON_REGION_ID
AND PERSON_ID IN ( 111, 888)
GROUP BY PERSON_REGION_ID
HAVING COUNT(DISTINCT PERSON_ID) = 2)
AND COUNTRY_ID = 44

monetdb sql method to locate or match by the nearest value, without a TOP or LIMIT

i am trying to replicate this question in monetdb, which i don't believe supports the TOP command and can only use LIMIT on the outermost result.
if i could use TOP then i believe this command would give me what i want. is there a reasonable alternative that's not massively inefficient? i need to run this on a table that will quickly max out my server's ram. thanks!
CREATE TABLE nearest_matches AS
( SELECT a.* ,
(
SELECT TOP 1 svcmon
FROM person_table AS z
WHERE a.yr = z.yr AND a.person_id = z.person_id
ORDER BY abs( z.svcmon - a.svcmon )
) AS nearest_month
FROM event_table AS a ) WITH DATA
from Stefan Manegold at CWI
Hi,
making up my suggestions given my understanding of the desired semantics:
for the orignal question:
create table a (id int, sales int, date timestamp);
create table b (id int, goal int, date timestamp);
select a.*, b.* from a,b, (select a.date as a_date, max(b.date) as b_date from a,b where b.date < a.date group by a.date) as ab where a.date = ab.a_date and b.date = ab.b_date;
for the question below:
create table event_table (yr int, person_id int, svcmon int, payload string);
create table person_table (yr int, person_id int, svcmon int, payload string);
select * from (select e.yr, e.person_id, e.svcmon as e_svcmon, e.payload as e_payload, p.svcmon as p_svcmon, p.payload as p_payload, row_number() over (partition by e.yr,e.person_id order by abs(e.svcmon - p.svcmon) asc) as pos from event_table e , person_table p where e.yr = p.yr and e.person_id = p.person_id) as ep where pos = 1;
knowing actual schemas would help to understand whether "each event" is identified by yr,person_id
(as I assume above) or by, say, (yr,person_id,svcmon) (in which case e.svcmon should be added to
the partition-by clause).
knowing actual schemas might also help to pull the projection out of the inner query,
as thus shrinking the intermediate result size(s) ...
Best,
Stefan

SELECT command with a WHERE condition and an INNER JOIN

I'm sure I must be making a trivial mistake here, but I've been searching around for help with this problem and all I can find is information on conditional INNER JOINs.
< EDIT > The problem is that this stored procedure is not returning anything at all. If I type just:
SELECT TOP (6) UserID, Category, Title, SUBSTRING(Article, 0, 200) AS Summary, DatePosted
FROM ContribContent
WHERE (DateFeatured IS NOT NULL)
ORDER BY DateFeatured DESC
Into the console then I get values returned. So it must be something to do with the inner-join? < / EDIT >
The idea is to:
take the content which has been
featured (DateFeatured is NOT NULL)
and place it all into a temporary
table
get the user names and picture from the users table and match them to the values in the temporary table using the UserID value.
sort the temporary table in order of the date each post was featured.
select the top six entries from the table
Here's the code:
ALTER PROCEDURE [dbo].[admin_GetFeaturedContrib]
AS
BEGIN
DECLARE #FeaturedContrib TABLE (
UserID INT,
Category INT,
Title varchar(100),
Summary varchar(200),
DatePosted date,
FirstName varchar(50),
LastName varchar(50),
Picture varchar(100)
)
INSERT INTO #FeaturedContrib
SELECT TOP 6 ContribContent.UserID, ContribContent.Category, ContribContent.Title, SUBSTRING(ContribContent.Article, 0, 200) AS Summary, ContribContent.DatePosted, Users.FirstName, Users.LastName, Users.Picture
FROM ContribContent
INNER JOIN Users
ON ContribContent.UserID = Users.UserID
WHERE ContribContent.DateFeatured IS NOT NULL
ORDER BY ContribContent.DateFeatured DESC
SELECT * FROM #FeaturedContrib
END
There are two data tables involved:
Users - a table storing all of the users and their information.
UserID INT
FirstName varchar(50)
LastName varchar(50)
Picture varchar(50)
etc...
ContribContent
ContribContentID INT
UserID INT
Category INT
Title varchar(100)
Article varchar(MAX)
Picture varchar(50)
DatePosted date
DateFeatured date
Deleted bit
THANKS to anyone who can help out!
Run only -
SELECT TOP 6 ContribContent.UserID, ContribContent.Category, ContribContent.Title, SUBSTRING(ContribContent.Article, 0, 200) AS Summary, ContribContent.DatePosted, Users.FirstName, Users.LastName, Users.Picture
FROM ContribContent
INNER JOIN Users
ON ContribContent.UserID = Users.UserID
WHERE ContribContent.DateFeatured IS NOT NULL
ORDER BY ContribContent.DateFeatured DESC
See what you getting might be an issue with your Where or your join see carefully if you have any data in the first place being returned. My guess is join see if you have matching userids you are joining on...(Hint : Left join maybe your answer)