Q:

Write a query in SQL to obtain the name of the patients, their block, floor, and room number where they are admitted

0

 Write a query in SQL to obtain the name of the patients, their block, floor, and room number where they are admitted.

 

Sample table: stay

stayid |  patient  | room |     start_time      |      end_time
--------+-----------+------+---------------------+---------------------
   3215 | 100000001 |  111 | 2008-05-01 00:00:00 | 2008-05-04 00:00:00
   3216 | 100000003 |  123 | 2008-05-03 00:00:00 | 2008-05-14 00:00:00
   3217 | 100000004 |  112 | 2008-05-02 00:00:00 | 2008-05-03 00:00:00
ssn    |       name        |      address       |  phone   | insuranceid | pcp
-----------+-------------------+--------------------+----------+-------------+-----
 100000001 | John Smith        | 42 Foobar Lane     | 555-0256 |    68476213 |   1
 100000002 | Grace Ritchie     | 37 Snafu Drive     | 555-0512 |    36546321 |   2
 100000003 | Random J. Patient | 101 Omgbbq Street  | 555-1204 |    65465421 |   2
 100000004 | Dennis Doe        | 1100 Foobaz Avenue | 555-2048 |    68421879 |   3
roomnumber | roomtype | blockfloor | blockcode | unavailable
-----------+----------+------------+-----------+-------------
       101 | Single   |          1 |         1 | f
       102 | Single   |          1 |         1 | f
       103 | Single   |          1 |         1 | f
       111 | Single   |          1 |         2 | f
       112 | Single   |          1 |         2 | t
       113 | Single   |          1 |         2 | f
       121 | Single   |          1 |         3 | f
       122 | Single   |          1 |         3 | f
       123 | Single   |          1 |         3 | f
       201 | Single   |          2 |         1 | t
       202 | Single   |          2 |         1 | f
       203 | Single   |          2 |         1 | f
       211 | Single   |          2 |         2 | f
       212 | Single   |          2 |         2 | f
       213 | Single   |          2 |         2 | t
       221 | Single   |          2 |         3 | f
       222 | Single   |          2 |         3 | f
       223 | Single   |          2 |         3 | f
       301 | Single   |          3 |         1 | f
       302 | Single   |          3 |         1 | t
       303 | Single   |          3 |         1 | f
       311 | Single   |          3 |         2 | f
       312 | Single   |          3 |         2 | f
       313 | Single   |          3 |         2 | f
       321 | Single   |          3 |         3 | t
       322 | Single   |          3 |         3 | f
       323 | Single   |          3 |         3 | f
       401 | Single   |          4 |         1 | f
       402 | Single   |          4 |         1 | t
       403 | Single   |          4 |         1 | f
       411 | Single   |          4 |         2 | f
       412 | Single   |          4 |         2 | f
       413 | Single   |          4 |         2 | f
       421 | Single   |          4 |         3 | t
       422 | Single   |          4 |         3 | f
       423 | Single   |          4 |         3 | f

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

SELECT p.name AS "Patient",
       s.room AS "Room",
       r.blockfloor AS "Floor",
       r.blockcode AS "Block"
FROM stay s
JOIN patient p ON s.patient=p.ssn
JOIN room r ON s.room=r.roomnumber;

Sample Output:

      Patient      | Room | Floor | Block
-------------------+------+-------+-------
 John Smith        |  111 |     1 |     2
 Random J. Patient |  123 |     1 |     3
 Dennis Doe        |  112 |     1 |     2
(3 rows)

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now