0

私はこのクエリを試してみました: 医師の数が少ない各国の病院はどこですか. (1 列目: 国名、2 列目: 病院の名前。医師の数が少ない病院が複数ある場合は、結果に表示する必要があります)。しかし、結果は期待したものではなく、構文エラーがあります。

私はこれらのテーブルを持っています:

CREATE TABLE Hospital (
    hid INT PRIMARY KEY,
    name VARCHAR(127) UNIQUE,
    country VARCHAR(127),
    area INT
);
CREATE TABLE Doctor (
    ic INT PRIMARY KEY,
    name VARCHAR(127),
    date_of_birth INT,
);
CREATE TABLE Work (
    hid INT,
    ic INT,
    since INT,
    FOREIGN KEY (hid) REFERENCES Hospital (hid),
    FOREIGN KEY (ic) REFERENCES Doctor (ic),
    PRIMARY KEY (hid,ic)
);

私はこれで試しました:

SELECT DISTINCT H.country, H.name, MIN(*) 
FROM Hospital H
WHERE H.hid IN (
               SELECT COUNT(*)
               FROM Work W, Doctor D
               WHERE W.hid = H.hid AND W.ic = D.ic
               GROUP BY H.country
               )
GROUP BY H.country
;    

ありがとう。

4

2 に答える 2

0
SELECT country, name
FROM
    (
        SELECT  hid, country, name, MIN(doctorCount)
        FROM
            (
                SELECT  a.hid, a.country, a.name, COUNT(b.hid) doctorCount
                FROM    Hospital a
                        LEFT JOIN Work b
                            ON a.hid = b.hid
                GROUP BY    a.hid, a.country, a.name
            ) x
        GROUP BY hid, country, name
    ) y
于 2012-11-25T14:02:19.527 に答える
0

これを試して:

WITH doctorCount AS
  (SELECT H.country country, H.hid hid, COUNT(*) dCount
   FROM Work W, Doctor D, Hospital H
   WHERE W.ic = D.ic
   AND   H.hid = W.hid
   GROUP BY H.country, H.hid),
   minCount AS
  (SELECT D.country, MIN (D.dCount) lowCount
   FROM doctorCount D
   GROUP BY D.country)
SELECT D.country, H.name
FROM doctorCount D, Hospital H, minCount M
WHERE D.hid = H.hid
AND   M.country = D.country
AND   D.dCount = M.lowCount;
于 2012-11-25T17:21:48.493 に答える