0

there is a couple of question I have,

first, as I read some of the articles and I know I shall implement the SQL code with using inner Join in this question. I have below Tables :

Hospital Table :

    id  |  hospital_name | adress  | city_id
   -------------------------------------------
    1   | pars           |   55,6LA|   2
    2   | ghaem          |   12.9GI|   2
    3   | Mehr           |   632.Sp|   2
    4   | Erfan          |   21,6UJ|   3
    5   | Razavi         |   31.jjI|   3
    6   | Mohab          |   65.Spi|   1

Cities Table:

    id  |  cityname 
   --------------------
    1   | LosAngels
    2   | NewYork
    3   | Colifornia

Treatments Table:

    id  |  TreatmentName 
   ----------------------
    1   | nose surgery   
    2   | Orthopedic  
    3   | Knee joint replacement

treatment_prices Table :

    id  |  Treatments_id | Hospital_id  | Price
   ------------------------------------------
    1   |       1        |       1      |   700
    2   |       1        |       6      |   800
    3   |       1        |       4      |   900
    4   |       2        |       1      |   500
    5   |       2        |       2      |   700
    6   |       2        |       3      |   300
    7   |       3        |       1      |   600
    7   |       3        |       2      |   450

my question is how can I found the minimum treatment price each hospital by city code.

I need something like below Table View If city ID is 2 :

    price  |  hospital_name | treatment_name| 
   -------------------------------------------
    700    | pars           |   nose surgery   
    300    | Mehr           |   Orthopedic     
    450    | ghaem          |   Knee joint replacement     

this is my SQL Code :

SELECT Min(treatment_prices.dollar) AS price, 
       hospitals.name_en            AS hname, 
       treatments.title_en          AS title 
FROM   treatment_prices 
       INNER JOIN hospitals 
               ON hospitals.id = treatment_prices.hospital_id 
       INNER JOIN treatments 
               ON treatments.id = treatment_prices.treatment_id 
WHERE  hospitals.city_id = 2 
GROUP  BY treatment_prices.treatment_id 

*But It does not work Well. Like Below photo : Result of my SQL code

any help Will appreciated.*

6
  • You say it doenst work, what doesnt work?
    – Brad
    Apr 24, 2019 at 19:44
  • It's not going to work because you aren't grouping by all of the fields. If you use MIN() on one field the rest need to either also have some form of aggregation or need to be grouped in the Group By.
    – haag1
    Apr 24, 2019 at 19:49
  • @haag1 okay what SQL look like is ? Apr 24, 2019 at 19:51
  • Looking at your query, you're looking at all of the hospitals in a city and which one offers the cheapest price for each procedure, correct?
    – Shawn
    Apr 24, 2019 at 20:06
  • @Shawn yes its correct Apr 24, 2019 at 20:09

3 Answers 3

2

You're looking to find out which procedure is cheapest in which hospital in a city.

In MySQL 5.x, you'll have to use some inner sub-queries to find the cheapest procedure by hospital, and then join that back to your tables again to get the data you need.

See the Fiddle for the setup.

SQL Fiddle

MySQL 5.x:

First, you want to find the cheapest price for a procedure, filtering by your required City and the Hospitals in that city.

Query 1:

SELECT tp.Treatments_ID, min(tp.Price) AS price
FROM treatment_prices tp
INNER JOIN Hospital h ON tp.Hospital_ID = h.ID
  INNER JOIN Cities c ON h.city_id = c.id
    AND c.ID = 2
GROUP BY tp.Treatments_ID 

This gives you

Results:

| Treatments_ID | price |
|---------------|-------|
|             1 |   700 |
|             2 |   300 |
|             3 |   450 |

Now, you can use this as your sub-query to link back to your main tables again to retrieve the data you're looking for.

Query 2:

SELECT t2.TreatmentName, h2.Hospital_Name, s1.Price
FROM (
    SELECT tp.Treatments_ID, min(tp.Price) AS price
    FROM treatment_prices tp
    INNER JOIN Hospital h ON tp.Hospital_ID = h.ID
      INNER JOIN Cities c ON h.city_id = c.id
        AND c.ID = 2
    GROUP BY tp.Treatments_ID 
) s1
INNER JOIN treatment_prices tp2 ON s1.Treatments_ID = tp2.Treatments_ID
    AND s1.price = tp2.Price
INNER JOIN Hospital h2 ON tp2.Hospital_ID = h2.ID
INNER JOIN Treatments t2 ON tp2.Treatments_ID = t2.ID

Results:

|          TreatmentName | Hospital_Name | Price |
|------------------------|---------------|-------|
|           nose surgery |          pars |   700 |
|             Orthopedic |          Mehr |   300 |
| Knee joint replacement |         ghaem |   450 |

It's MUCH easier in later versions of MySQL or any other SQL language that allows window functions.

MySQL 8+:

SELECT s1.TreatmentName, s1.Hospital_Name, s1.Price
FROM (
    SELECT t.TreatmentName, h.Hospital_Name, tp.Price
        , ROW_NUMBER() OVER (PARTITION BY tp.Treatments_ID ORDER BY tp.Price) AS rn
    FROM treatment_prices tp 
    INNER JOIN Hospital h ON tp.Hospital_ID = h.ID
    INNER JOIN Cities c ON h.city_id = c.id
            AND c.ID = 2
    INNER JOIN Treatments t ON tp.Treatments_ID = t.ID
) s1
WHERE rn = 1
;

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=9b28f7543c3b127990654ad9e401c11a

1
0

It doest show the hospital it should because you there is only a GROUP BY the treatment_id, when you select a column with an aggregation function and dont include it in the group by the DBMS will spread the result for that column of the first row to all the rows it fetchs. So group first by treatment_id and then by hospital_name (or id) and that should do it. Leaving the query like this:

   SELECT Min(tp.dollar) AS price, 
          h.name_en      AS hname, 
          t.title_en     AS title 
    FROM   treatment_prices AS tp
           INNER JOIN hospitals AS h
                   ON h.id = tp.hospital_id 
           INNER JOIN treatments AS t 
                   ON t.id = tp.treatment_id 
    WHERE  h.city_id = 2 
    GROUP  BY tp.treatment_id, hname

Note that im using the alias for h.name_en in the GROUP BY, MySQL 8+ has no problem with this and it puts to use the aliases for more than for the visuals.

Also one last thing, and since I see other questions suggesting the use of subqueries, only use them if it's 100% a necessity and there's no other way. That's because subqueries create a query over a table that is not indexed and not normalized in the DB so if that table the subquery returns has too much data it will take a much longer time to complete the query than if it's done without the subquery. Maybe this doesnt have great effect while studying or learning but in the work enviroment time is crucial and the queries that give data to the many reports a department may do need to be fast and optimal.

0

You should use GROUP BY hospitals.name_en , treatments.title_en

SELECT Min(treatment_prices.dollar) AS price, 
       hospitals.name_en            AS hname, 
       treatments.title_en          AS title
FROM   treatment_prices 
       INNER JOIN hospitals 
               ON hospitals.id = treatment_prices.hospital_id 
       INNER JOIN treatments 
               ON treatments.id = treatment_prices.treatment_id 
WHERE  hospitals.city_id = 2 
GROUP  BY hospitals.name_en  ,  treatments.title_en     

and for obtain the hospital with min(price) should be

select  t.price, t.title, hospitals.name_en   

from (
  SELECT Min(treatment_prices.dollar) AS price, 
         treatments.title_en          AS title,
         treatment_prices.treatment_id 
  FROM   treatment_prices 
         INNER JOIN hospitals 
                 ON hospitals.id = treatment_prices.hospital_id 
         INNER JOIN treatments 
                 ON treatments.id = treatment_prices.treatment_id 
  WHERE  hospitals.city_id = 2 
  GROUP  BY  treatments.title_en, treatment_prices.treatment_id
) t 
INNER JOIN treatments ON treatments.id = treatment_prices.treatment_id  
    AND treatments.title_en   = t.title 
INNER JOIN hospitals  ON hospitals.id = treatment_prices.hospital_id   
    AND treatment_prices.dollar = t.price     
3
  • Excuse me bro but phpmyadmin gave the nonsense error : #1146 - Table 'bloga.title' doesn't exist Apr 24, 2019 at 20:17
  • Typo on AND treatments.title_en = t,title ... it's a comma instead of a dot.
    – FanoFN
    Apr 25, 2019 at 1:15
  • 1
    comma .. changed in dot
    – ScaisEdge
    Apr 25, 2019 at 5:49

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.