i am not able to get single value from table b in subquery which is required in outer query. in table b there is group of dates related to table a and table c. and here i required purpose code of the latest date for the successfull execution of query. if i remove subquery and write where c.purpose_code = b.purpose_code it runs ok but the result is older.
please help me out.
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
The problem is a subquery –
Try this query instead –
Create tables:
Result: