0

I am trying to do up a query for a table hotel, roomtype, roomprice, I am trying to get sum of roomprice as "total Single Rooms" where roomtype = single grouped by hotel So I end up with hotel and "Total Single rooms"

I have been trying different ways but I can not seem to get that to work. Yes I am new to SQL lol

Thanks

2
  • 5
    Show us your current query attempt! Commented Aug 5, 2016 at 8:10
  • SELECT hotelNo,roomPrice, SUM(roomPrice) AS "Total Room Price", WHERE roomType = "Single", FROM room, GROUP BY hotelNo); Commented Aug 5, 2016 at 8:14

2 Answers 2

1

You should accept MrVimes solution if it meets your requirement. If you want to include a hotel chain total you could use a with rollup to the group by for example:-

/*drop table t;
create table t (hotel varchar(3),roomtype varchar(1),price decimal(10,2));
truncate table t;
insert into t values 
('abc','s',10.00),
('abc','s',10.00),
('abc','s',30.00),
('abc','d',10.00),
('def','s',10.00),
('def','s',10.00),
('abc','d',10.00),
('abc','d',10.00),
('abc','d',10.00);
*/

select  case
            when s.hotel is not null then s.hotel
            else 'Total'
            end as hotel,
            s.totalsinglerooms
from
(
select  hotel, sum(price) as totalsinglerooms
from      t
where     roomtype = 's'
group   by hotel with rollup
) s

result

+-------+------------------+
| hotel | totalsinglerooms |
+-------+------------------+
| abc   |            50.00 |
| def   |            20.00 |
| Total |            70.00 |
+-------+------------------+
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks to all of you :) MrVimes was right on with the answer :0 thanks
Upvote for showing me 'rollup' which I might find useful in my own query jobs :) I didn't know about it.
0
select hotelno,sum(roomprice) as `Total Single Rooms` from room
where roomtype = 'single'
group by hotelno
having sum(roomprice) > 100

9 Comments

the table name is "room"
I do thank you :) I just changed the names where needed. I is so easy when you look at it and I was going all over the place lol.
No it is doing what it is suppose to do and now I have to add in there another part that only shows total over 100 so does that need another line or after the total single rooms?
@Wombat You can add "HAVING [Total Single Rooms] > 100" after the group by to only show lines with total over 100.
I've edited my answer to include the over 100 requirement
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.