Join with the highest value in Oracle - sql

I have two tables, students and school_year.
students
---------
ID Name
---------
1 ABC
2 XYZ
school_year
-----------
ID student_id grade year
--------------------------
1 1 5 2011
2 1 6 2012
3 2 1 2010
4 2 2 2011
5 2 3 2012
I join them and get this result
select s.*, sy.grade, sy.year
from students s
left join school_year sy
on s.id=sy.student_id
order by s.name
and I get this result
id name grade year
---------------------------
1 ABC 5 2011
1 ABC 6 2012
2 XYZ 1 2010
2 XYZ 2 2011
2 XYZ 3 2012
I would like to join school year table where grade is maximum/highest for the student so the table would look like this.
id name grade year
-------------------------
1 ABC 6 2012
2 XYZ 3 2012
Please help. Thanks.

Try this out:
SELECT s.id, s.name, sy1.grade, sy1.year FROM school_year sy1
LEFT JOIN school_year sy2
ON sy1.student_id = sy2.student_id AND sy1.grade < sy2.grade
JOIN students s ON sy1.student_id = s.id
WHERE sy2.grade IS NULL
Fiddle here.

Related

How to make a view with 2 tables linked by another table?

I have 3 table
driver
id
first name
last_name
age
1
Joe
doe
26
2
John
smith
31
...
...
...
...
Location
id
name
zipcode
country
1
London
250 329
UK
2
NY
00501
USA
...
...
...
...
Travel
id
Person_id
location_id
nb_passenger
1
1
1
2
2
1
1
4
3
2
2
3
4
1
2
2
5
2
1
3
...
...
...
...
I'm looking to create a view to get the total number of passengers by destination and by driver, but it's taking the same row multiple times
I tried something like that but it didn't work
SELECT location.id AS location_id,
location.name AS location_name,
d.id AS driver_id,
d.name AS driver_name,
sum(tr.passenger) as passenger_count
FROM location
LEFT JOIN travel tr_0 ON location.id = tr_0.location_id
LEFT JOIN driver d ON tr_0.driver_id = d.id
LEFT JOIN travel tr ON location.id = tr.location_id AND tr.driver_id = d.id
GROUP BY location.id, location.name,...;
I'm sure it's simple but I'm not on the right way

SQL Output with multi join on different rows

I'm trying to join 2 different data sets with different columns and when I make the join I get repeated results.
My input dataset1 with actual data:
Cust_id Year sales
----------------------
1 2016 679862
1 2017 705365
1 2018 195662
1 2019 201234
2 2016 51074
2 2017 50611
2 2018 19070
2 2019 20123
My input dataset2 with estimated data:
Cust_id Year salesest
-------------------------
1 2018 779862
1 2019 125662
2 2017 23456
2 2018 32856
2 2019 26602
Desired output:
Cust_id Year sales salesest
-------------------------------
1 2016 679862 null
1 2017 705365 null
1 2018 195662 779862
1 2019 201234 125662
2 2016 51074 null
2 2017 50611 23456
2 2018 19070 32856
2 2019 20123 26602
This is what I have tried:
select
a.*, b.salesest
from
tab1 a, tab2 b
where
a.Cust_id = b.Cust_id
You want a LEFT JOIN. The correct syntax is:
select a.*, e.salesest
from actuals a left join
estimates e
on a.Cust_id = e.Cust_id and
a.year = e.year;
you also need to specify the year - and make an outer join for the times when there is no corresponding year in the other table.
select a.*, b.salesest
frpm tab1 a, tab2 b
where
a.Cust_id=b.Cust_id
AND a.YEAR = b.YEAR (+)

i want to get the values from two tables

i got two tables
office accnt id
------------------------------
HR poop 1
HR fart 2
EXEC poop 3
and
id number
-----------------------
1 2
1 2
1 1
2 5
2 1
3 6
and what i wanted to be the output is like this
id office accnt number
--------------------------------------------
1 HR poop 5
2 HR fart 6
3 EXEC poop 6
and here's what I've tried so far
SELECT AccntTbl.office, AccntTbl.accnt, SUM(NumberTbl.Number)
FROM AccntTbl INNER JOIN
NumberTbl ON AccntTbl.Id = NumberTbl.Id
and sadly i can't get what i want..glad for any help.. :)
select a.id, a.office, a.accnt, SUM(n.Number)
from AccntTbl a INNER JOIN
NumberTbl n ON a.AccntTbl.Id = NumberTbl.Id
SELECT Accnt.id,Accnt.office, Accnt.accnt, SUM(Num.Number)
FROM AccntTbl Accnt INNER JOIN
NumberTbl Num ON Accnt.Id = Num.Id
It just needs a GROUP BY with the id, the office and the accnt.
SELECT
acc.id, acc.office, acc.accnt,
SUM(num.Number) AS number
FROM AccntTbl acc
JOIN NumberTbl num
ON acc.Id = num.Id
GROUP BY
acc.id, acc.office, acc.accnt

SQL Server Adding Count up

I'm working on a query and I'm getting the result as intended but they're not adding up
My Query
SELECT SUBSTRING(h.HotelName, 1, CHARINDEX(' ', h.HotelName)) AS 'HotelName', LEFT(r.RoomNumber, 1) AS Floor, COUNT(*) AS 'Rooms'
FROM HOTEL AS h
JOIN HOTELROOMTYPE AS hr ON h.HotelID = hr.HotelID
JOIN ROOM AS r ON hr.HotelRoomTypeID = r.HotelRoomTypeID
GROUP BY r.RoomNumber, h.HotelName
My Results
HotelName Floor Rooms
------------------------------ ----- -----------
John's 2 1
John's 2 1
John's 3 1
University 1 1
University 1 1
University 2 1
University 2 1
University 2 1
Utah 2 1
Utah 2 1
Utah 2 1
Intended results
HotelName Floor Rooms
------------------------------ ----- -----------
John's 2 2
John's 3 1
University 1 2
University 2 3
Utah 2 3
Basically I want the Rooms for each floor to be added up, any help would be great.
If you want the floor in the output, then group by it:
SELECT SUBSTRING(h.HotelName, 1, CHARINDEX(' ', h.HotelName)) AS HotelName,
LEFT(r.RoomNumber, 1) AS Floor, COUNT(*) AS 'Rooms'
FROM HOTEL h JOIN
HOTELROOMTYPE hr
ON h.HotelID = hr.HotelID JOIN
ROOM r
ON hr.HotelRoomTypeID = r.HotelRoomTypeID
GROUP BY h.HotelName, LEFT(r.RoomNumber, 1);
I presume none of your hotels have more than 9 floors.

id getting duplicate PV.RFMID [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how can i use distinct INNER JOIN RFM ON RFM.RFMID=PV.RFMID
Table MMASTER
MID col1
1 abc
2 xyx
3 pqr
Table AMM
AMMID MID col1 col2
1 1 bnb mfk
2 1 def rwr
3 2 re wrwr
Table PS
UID VTID AMMID SMID col1 col2
1 1 1 1 rkk jdj
2 2 3 3 kdf lfl
6 2 2 4 rgr rtr
Table PV
VTID PMID RFMID
1 2 1
2 2 3
7 2 2
Table RFM
RFMID title name
1 mr john
2 mr jack
3 mr jim
Table PM
PMID col1 col2
1 df ere
2 rwe rwer
3 rwr fwr
Table SM
SMID MMID col1 col2
1 1 fdf efe
2 1 ddf dfdf
3 2 df ef
I get result like this after fire above query
PMID title name PV.RFMID
2 mr. jim 3
2 mr. jim 3
2 mr. jim 3
***BUT RESULT SHOULD BE LIKE THIS***
PMID title name PV.RFMID
2 mr. john 1
2 mr. jim 3
2 mr. jack 2
In my query PV.RFMID getting duplicate value when join with RFM and due to that title and name getting duplicate
This will give you your desired Output:
select pm.pmid, rfm.title, rfm.name, pv.rfmid from PM as PM
inner join PV on pm.pmid = pv.pmid
inner join RFM as rfm on pv.rfmid = rfm.rfmid
WHERE PM.PMID='2'
EDIT
Considering that you need to Join all tables, it's something tricky.
I mean, checking RFMtable, you can see the RFMId, related to PV table, and see that the VTID has a 7 value.. which is not related to anything ! So, if you need to join all tables, you won't get that result
I Am assuming that table "PM" is your transaction table where one "PMID" can have multiple Entries, thus Adding a Distinct Clause Before your Query will Get you the Desired Out Put.
Try This:
SELECT DISTINCT PM.PMID,RFM.TITLE,RFM.NAME ,PV.RFMID FROM MMASTER
INNER JOIN AMM ON MMASTER.MID=AMM.MID
INNER JOIN PS ON AMM.AMMID=PS.AMMID
INNER JOIN PV ON PV.VTID=PS.VTID
INNER JOIN RFM ON RFM.RFMID=PV.RFMID
INNER JOIN PM ON PV.PMID=PM.PMID
INNER JOIN SM ON PS.SMID=SM.SMID
WHERE PM.PMID='2'