0

我有三张桌子

table A
------------------------------------------
filing_no | pet_name | res_name | court_no
------------------------------------------
1         | xyz      | PQR      |  1
------------------------------------------
2         | abc      | def      |  2
------------------------------------------


Table B
-------------------------------
filing_no |purpose_code|next_date
-----------------------------------
2         | 20         |3/8/2012
-----------------------------------
1         | 21         |3/9/2012
-----------------------------------
2         | 22         |3/10/2012
-----------------------------------
1         | 23         |15/11/2012
-----------------------------------

Table C.
-------------------------------------
purpose_code | purpose_name
-------------------------------------
20           | institution
-------------------------------------
21           | summon
-------------------------------------
22           | proceeding
-------------------------------------
23           | order
-------------------------------------

我想要特定法庭的结果,例如:

--------------------------------------------------------
filing_no| Pet_name | res_name |purpose_name| next_date
--------------------------------------------------------
1        | xyz      | PQR      | Order      | 15/11/2012

我执行以下查询并得到#1111 - 组函数的使用无效错误。请帮忙。

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 = (
               select MAX(next_date) 
               from Daily_proc 
               where filing_no = a.filing_no
          )
    ) 
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

2 回答 2

0

答案由@Devart 分享,我非常感谢他。

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;

于 2012-11-27T05:59:19.650 回答
0

您只能选择GROUP BY聚合函数中的列和结果。例如,a.pet_name当有不同的相同时,您希望返回什么b.filling_no

于 2012-11-08T11:14:48.960 回答