I have an SQL query that currently displays all the people in table HOTEL that booked a hotel between $260 and $350. I'm using ORACLE
SELECT guest.guest_name "GUEST NAME",
Count(guest.guest_no),
guest.guest_no "GUEST NUMBER",
room.r_price "ROOM PRICE"
FROM room,
guest,
booking
WHERE r_price >= 260
AND r_price <= 350
AND guest.guest_no = booking.guest_no
GROUP BY guest.guest_no,
guest.guest_name,
room.room_price
ORDER BY guest.guest_name;
I thought that I could group by just GUEST.GUEST_NAME (so it puts all of the count under the one name rather than doubling up on the guest names).
EDIT: The current results of the query look like this:
GUEST NAME COUNT GUEST_NO R_PRICE
Jenny Freeman 2 G003 260
Jenny Freeman 2 G003 295
Jenny Freeman 2 G003 310
Martin Ferguson 3 G006 260
Martin Ferguson 3 G006 295
Martin Ferguson 3 G006 310
Paul Happy 3 G002 260
Paul Happy 3 G002 295
Paul Happy 3 G002 310
Steve Kirkwood 3 G005 260
Steve Kirkwood 3 G005 295
Steve Kirkwood 3 G005 310
Tina Duncan 2 G001 260
Tina Duncan 2 G001 295
Tina Duncan 2 G001 310
Vanessa Horton 1 G007 260
Vanessa Horton 1 G007 295
Vanessa Horton 1 G007 310
But I'm expecting something like this:
GUEST NAME COUNT GUEST_NO R_PRICE
Jenny Freeman 6 G003 260
Martin Ferguson 9 G006 310
Paul Happy 9 G002 310
Steve Kirkwood 9 G005 260
Tina Duncan 6 G001 310
Vanessa Horton 3 G007 260
Well, I think you just have to change in group by statement rest looks OK to me. Just don't include Room price in Group By statement, i think that is creating problem for you if i am getting your question right...
GROUP BY GUEST.GUEST_NO,
GUEST.GUEST_NAME
and
MAX(R_PRICE)
May be you forgot to join ROOM with BOOKING by ROOM_NO (or other field)?
I've removed ROOM PRICE from group by, query now will will return unique guests and number of rooms they booked with price in given range, and max price for each guest.
SELECT
GUEST.GUEST_NAME "GUEST NAME",
GUEST.GUEST_NO "GUEST NUMBER",
COUNT(*) "CNT",
MAX(R.R_PRICE) "ROOM PRICE"
FROM BOOKING B
inner join ROOM R on R.ROOM_NO = B.ROOM_NO
inner join GUEST G on G.GUEST_NO = B.GUEST_NO
WHERE R.R_PRICE>=260 and R.R_PRICE<=350
GROUP BY G.GUEST_NO, G.GUEST_NAME
ORDER BY G.GUEST_NAME;
You can try the below sql query:
SELECT GUEST.GUEST_NAME "GUEST NAME", COUNT(GUEST.GUEST_NO), GUEST.GUEST_NO "GUEST NUMBER", ROOM.R_PRICE "ROOM PRICE"
FROM ROOM, GUEST,BOOKING
WHERE ROOM.R_PRICE>=260
AND ROOM.R_PRICE<=350
AND GUEST.GUEST_NO = BOOKING.GUEST_NO
GROUP BY GUEST.GUEST_NAME
ORDER BY GUEST.GUEST_NAME;
Related
I have the below query and results. What can I add to this query to combine the calories for people with the same name? For example, Dave's total should be 3045+3129 to show 6174.
SELECT
likes.*, beer.cal, (beer.cal * 21) AS 'Drink 3 in a Week'
FROM
likes
INNER JOIN
beer ON likes.beer = beer.beer;
**name beer cal Drink 3 in a Week**
dave bud 145 3045
dave coors 149 3129
gary miller 143 3003
linda coors 149 3129
mike bud 145 3045
mike miller 143 3003
sally coors 149 3129
sally miller 143 3003
You can use window funtion
SELECT
likes.*, beer.cal,
SUM(beer.cal * 21) OVER(PARTITION BY NAME) AS 'Drink 3 in a Week'
FROM
likes
INNER JOIN
beer ON likes.beer = beer.beer;
Another option is group by.
I would like to use microsoft access or query to change from table1
student name course_attended date quiz score
john course 1 3/1/2017 98
carl course 1 3/1/2017 90
george course 1 3/1/2017 77
john course 2 3/5/2017 99
carl course 2 3/5/2017 96
george course 2 3/5/2017 80
john course 3 4/4/2017 77
carl course 3 4/4/2017 80
george course 3 4/4/2017 85
to table2
student name course 1 course 2 course 3
john 3/1/2017 3/5/2017 4/4/2017
98 99 95
carl 3/1/2017 3/5/2017 4/4/2017
90 96 89
george 3/1/2017 3/5/2017 4/4/2017
77 80 85
Basically, with select distinct [student name] use transformation and pivot
to change from table1 to table2. Please help. I would be greatly appreciated it.
If you don't need to do any calcs on the output, try:
TRANSFORM First([attend_date] & Chr(13) & Chr(10) & [quiz_score]) AS Data
SELECT student_name
FROM Tablename
GROUP BY student_name
PIVOT course;
Date is a reserved word. Should not use reserved words as names. Also, avoid spaces and special characters/punctuation (underscore only exception) in names.
I tried this,
TRANSFORM First([date_a] & Chr(13) & Chr(10) & [quiz_score]) AS Data
SELECT student_name
FROM tbl_quiz
GROUP BY student_name
PIVOT course_attended;
and the query does not show the quiz_score. However, the form created from the query result shows it.
Thanks a lot.
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;
I've been stuck on this problem for a while
and here are there direction
Customers who have been invited to none of the “Mona Lisa” gala_night now get invited to to the 5-jan-2014 Mona Lisa gala_night. Show the insert command and the resulting Invite table.
Here are the two tables
SQL> select * from invite;
GALA_DATE PAINTING_NAME CUSTID
----------- ---------------------- ----------
10-nov-2013 Watercolors 1430
15-nov-2013 Woman 1502
15-nov-2013 Woman 1619
05-dec-2013 Watercolors 1207
22-dec-2013 Sunflowers 1806
22-dec-2013 Sunflowers 1904
31-dec-2013 Fiddler 1236
31-dec-2013 Fiddler 1280
05-jan-2014 Mona_Lisa 1111
05-jan-2014 Mona_Lisa 1502
25-jan-2014 Madonna 1806
25-jan-2014 Madonna 1822
25-jan-2014 Madonna 1904
18-feb-2014 Maya 1619
18-feb-2014 Maya 1822
18-feb-2014 Maya 1904
28-feb-2014 Mona_Lisa 1502
30-apr-2014 Lovers 1207
30-apr-2014 Lovers 1280
30-apr-2014 Lovers 1822
30-apr-2014 Lovers 1904
SQL> select * from customer;
CUSTID CUSTNAME CUSTBDATE CUST_TYPE BENEFACTOR DOCENT
1301 Disney 01-nov-1980 NM No No
1806 Garcia 31-dec-2000 VIP Yes No
1502 LaGardia 15-jan-1960 VIP Yes Yes
1207 Perry 20-jan-1960 VIP Yes Yes
1280 Beecham 31-dec-1979 VIP Yes No
1822 Becker 30-jan-1967 VIP Yes Yes
1140 Klim 05-apr-1990 NM No No
1509 Roberts 21-jul-1989 VIP Yes No
1619 Robins 20-feb-1990 VIP Yes Yes
1111 Bardot 28-feb-1970 VIP No No
1515 David 18-apr-1980 NM No No
1701 Martin 20-aug-1972 RM No No
1904 Gross 30-sep-1975 VIP Yes Yes
1236 Brooks 23-sep-1975 VIP Yes No
1430 Todd 15-jul-1982 VIP Yes Yes
I've tried doing
insert into invite(gala_date,painting_name,custid)
select invite.gala_date,invite.painting_name,invite.custid
from invite,customer
where invite.gala_date='05 Jan 2014' and invite.painting_name='Mona_Lisa'
and customer.custid=invite.custid
and customer.custid not in
(select custid from invite where gala_date in('05 Jan 2014')
and painting_name in('Mona_Lisa'));
0 rows created.
But as you can see the results yield "0 rows created"
Any thoughts?
Thanks!
You just need to use a not exists to get the results you want.
insert into invite(gala_date,painting_name,custid)
select '01/05/2014', 'Mona_Lisa', customer.custid
From customer
Where Not Exists (select '' from invite where invite.custid = customer.custid and painting_name = 'Mona_Lisa')
try this
insert into invite(gala_date,painting_name,custid)
select '01/05/2014', 'Mona_Lisa', a.custid
from customer a
left join invite b on (a.custid = b.custid and b.PaintingName = 'Mona_Lisa'
where b.custid is null
I think that will get you close, might need to tweak it as I didn't check
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)