1

外部クエリで必要なサブクエリで、テーブル b から単一の値を取得できません。テーブル b には、テーブル a とテーブル c に関連する日付のグループがあります。ここでは、クエリを正常に実行するために最新の日付の目的コードが必要でした。サブクエリを削除して where c.purpose_code = b.purpose_code と書くと、正常に実行されますが、結果は古いです。私を助けてください。

SELECT
  a.filing_no AS "File No",
  a.case_no AS "Registration No",
  a.pet_name AS Petitioner,
  a.res_name AS Accused,
  a.dt_of_filing AS "Date of Inst",
  max(b.Next_date) AS "Next Date",
  c.purpose_name AS "Case Stage"
FROM
  filing_t a, Daily_proc b, Purpose_t c
WHERE
  a.filing_no = b.filing_no
  AND c.purpose_code = (
    SELECT purpose_code FROM Daily_proc
    WHERE filing_no = a.filing_no AND next_date = max(next_date)
    )
  AND a.court_no = 1
  AND a.ci_cri = 2
  AND a.status = 'P'
GROUP BY
  b.filing_no
ORDER BY
  a.dt_of_filing DESC
LIMIT
  0, 2000
4

1 に答える 1

0

問題はサブクエリです -

SELECT purpose_code FROM Daily_proc
WHERE filing_no = a.filing_no AND next_date = max(next_date)

代わりにこのクエリを試してください -

SELECT
  purpose_code
FROM
  Daily_proc
WHERE
  filing_no = 20 AND next_date = (SELECT MAX(next_date) FROM Daily_proc);

テーブルを作成します。

CREATE TABLE A(
  filing_no INT,
  pet_name VARCHAR(20),
  res_name VARCHAR(20),
  court_no INT);
INSERT INTO A VALUES
  (1, 'xyz', 'PQR', 1),
  (2, 'abc', 'def', 2);

CREATE TABLE B(
  filing_no INT,
  purpose_code INT,
  next_date DATE);
INSERT INTO B VALUES
  (2, 20, '2012-8-3'),
  (1, 21, '2012-9-3'),
  (2, 22, '2012-10-3'),
  (1, 23, '2012-11-15');

CREATE TABLE C(
  purpose_code INT,
  purpose_name VARCHAR(20));
INSERT INTO C VALUES
  (20, 'institution'),
  (21, 'summon'),
  (22, 'proceeding'),
  (23, 'order');

SELECT a.filing_no, a.pet_name, a.res_name, c.purpose_name, b.next_date FROM a
  JOIN (SELECT * FROM (
          SELECT * FROM b ORDER BY filing_no, next_date DESC
       ) t GROUP BY filing_no) b
    ON a.filing_no = b.filing_no
  JOIN c
    ON b.purpose_code = c.purpose_code
WHERE a.court_no = 1;

結果:

+-----------+----------+----------+--------------+------------+
| filing_no | pet_name | res_name | purpose_name | next_date  |
+-----------+----------+----------+--------------+------------+
|         1 | xyz      | PQR      | order        | 2012-11-15 |
+-----------+----------+----------+--------------+------------+
于 2012-11-07T07:38:03.143 に答える