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.
Related
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
I need to query a name(s) from the Officials table, but exclude that name if the person has the day blocked.
For example, if Sam has blocked 8/21/2021 and 9/11/2021, he should not be selected if the corresponding dates are selected from the Games table. Sam should show up if 9/18/2021 is selected, however. I have 3 tables:
Officials tbl
RefId Name
---------------------
1 Jack
2 Sam
3 Jane
Games tbl Blocks tbl
GameId GameDate BlockId RefId BlockDate
------------------------- ----------------------
1 8/21/2021 1 2 8/21/2021
2 9/11/2021 2 2 9/11/2021
3 9/18/2021 3 3 8/21/2021
Desired Output
----------------------------------
If Game 1 is selected: Jack
If Game 2 is selected: Jack and Jane
If Game 3 is selected: Jack, Sam and Jane
The only 2 tables that are related are the Officials table and Blocks table, with the RefId. I need to compare the BlockDate of Blocks table to GameDate of Games table. I have tried some sql language and this below is obviously not correct, but I'm looking for a way to accomplish what I want to do:
#GameDate datetime,
Select c.Id, c.Name
From Officials c
Where In c.Id And Blocks.BlockDate <> Games.GameDate)
You can do it with NOT EXISTS:
SELECT o.*
FROM Officials o
WHERE NOT EXISTS (
SELECT 1
FROM Blocks b INNER JOIN Games g
ON g.GameDate = b.BlockDate
WHERE b.RefId = o.RefId AND g.GameId = ?
);
See the demo.
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
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.
I am trying to do a query but I donĀ“t know how to do it.
These are the tables:
Table Hospital Table Doctor Table Work
Hid Country ic Hid ic
1 England 1 1 1
2 Spain 2 1 2
3 France 3 1 3
4 England 4 2 4
5 China 5 4 5
Result that I want:
Country Average of Doctors Working on that Hospitals of that Country
England 2 (the doctor with ic 1, 2, 3, and 4/number of hid)
Spain 1
France 0
China 0
I tried:
SELECT DISTINCT H.country, AVG(D.ic)
FROM Hospital H, Doctor D
WHERE H.hid IN
( SELECT W.hid
FROM Work W
WHERE W.ic IN
( SELECT COUNT(D.ic)
FROM D Doctor ....
)
)
GROUP BY(H.country);
Try this
select H.Country, count(W.ic) / count(distinct H.hid) as [Average]
from Hospital as H
left outer join Work as W on W.Hid = H.Hid
group by H.Country
SQL FIDDLE
First get the number of doctors for each hospital, then get the average over that:
select country, avg(docs) as avg_doctors_working
from (
select country, h.hid, count(*) as docs,
from hospital h
left join work w on w.hid = h.hid
group by 1, 2) x
group by 1;