3

親愛なる友人私には、異なる基準を持つ1つのテーブルからcount(*)を取得する必要があります。SpringでHibernateを使用しています。そのために以下のクエリを使用しています。

"select  " +
            "(select count(*)  from enquiry  where business_id="+busid+")  AS allEnqCount,"+
    "(select count(*)  from enquiry where status='"+Constants.ENQ_FALLOWUP+"' and us.business_id="+busid+")  AS followupCount," +
    "(select count(*)  from enquiry where status='"+Constants.ENQ_SITE_VISIT+"'and us.business_id="+busid+")  AS siteVisitCount ," +
    "(select count(*)  from enquiry where status='"+Constants.ENQ_CUST_VISIT+"'and us.business_id="+busid+")  AS customerVisitCount," +
    "(select count(*)  from enquiry where status='"+Constants.ENQ_OFFICE_VISIT+"'and us.business_id="+busid+")  AS officevisitCount,"+
    "(select count(*)  from enquiry where status='"+Constants.ENQ_PENDING+"'and us.business_id="+busid+")  AS pending";

しかし、かなりの時間がかかります。最小限の時間で単一のselect句でそれを実行できる方法があるかどうか教えてください。

前もって感謝します。

4

2 に答える 2

2

使用するCASE

SELECT  SUM(CASE WHEN business_id = 'busid' THEN 1 END) allEnqCount,
        SUM(CASE WHEN status = 'ENQ_FALLOWUP' AND us.business_id = 'busid' THEN 1 END) followupCount,
        SUM(CASE WHEN status = 'ENQ_SITE_VISIT' AND us.business_id = 'busid' THEN 1 END) siteVisitCount,
        SUM(CASE WHEN status = 'ENQ_CUST_VISIT' AND us.business_id = 'busid' THEN 1 END) customerVisitCount,
        SUM(CASE WHEN status = 'ENQ_OFFICE_VISIT' AND us.business_id = 'busid' THEN 1 END) officevisitCount,
        SUM(CASE WHEN status = 'ENQ_PENDING' AND us.business_id = 'busid' THEN 1 END) pending
FROM    enquiry

ニーズを満たす値を変更するだけです。

于 2013-02-18T08:37:39.240 に答える
1

使用group by:

何かのようなもの:

select 
      status,
      count(*) 
from enquiry 
where business_id = @busid 
group by status
于 2013-02-18T08:43:28.747 に答える