select FFP_CODE,
OPT_AIRLINE_CODE,
ACCOUNT_MONTH,
ORIGINATION,
RETROL_TYPE,
count(MEMBER_ID),--总数1
(select count(MEMBER_ID) from tbl_report_billing_data a where a.ORIGINATION=t.ORIGINATION and FFP_CODE='CA' and OPT_AIRLINE_CODE='UA' and ACCOUNT_MONTH='200810') --总数2
from tbl_report_billing_data t 
where FFP_CODE='CA' 
and OPT_AIRLINE_CODE='UA' 
and ACCOUNT_MONTH='200810' 
group by FFP_CODE,OPT_AIRLINE_CODE,ACCOUNT_MONTH,ORIGINATION,RETROL_TYPE在这个sql语句中查到的两个总数为什么总数2比总数1大?这个语句没有问题,我就是不理解

解决方案 »

  1.   

    where a.ORIGINATION=t.ORIGINATION 是不是产生了笛卡尔积?
      

  2.   

    select 
    FFP_CODE, 
    OPT_AIRLINE_CODE, 
    ACCOUNT_MONTH, 
    ORIGINATION, 
    RETROL_TYPE, 
    count(MEMBER_ID),--总数1 

      select count(MEMBER_ID) 
      from tbl_report_billing_data a 
      where a.ORIGINATION=t.ORIGINATION and FFP_CODE='CA' and OPT_AIRLINE_CODE='UA' and ACCOUNT_MONTH='200810'
    ) --总数2 
    from tbl_report_billing_data t 
    where FFP_CODE='CA' 
    and OPT_AIRLINE_CODE='UA' 
    and ACCOUNT_MONTH='200810' 
    group by FFP_CODE,OPT_AIRLINE_CODE,ACCOUNT_MONTH,ORIGINATION,RETROL_TYPE --你还缺少条件。
    select 
    FFP_CODE, 
    OPT_AIRLINE_CODE, 
    ACCOUNT_MONTH, 
    ORIGINATION, 
    RETROL_TYPE, 
    count(MEMBER_ID),--总数1 

      select count(MEMBER_ID) 
      from tbl_report_billing_data a 
      where a.ORIGINATION=t.ORIGINATION and 
            (FFP_CODE, OPT_AIRLINE_CODE, ACCOUNT_MONTH, RETROL_TYPE) --这里还少了这些条件,但是如果加上,和你本身的count(MEMBER_ID)是一回事,有什么意思?所以,这个数2肯定>=数1
            FFP_CODE='CA' and 
            OPT_AIRLINE_CODE='UA' and 
            ACCOUNT_MONTH='200810'
    ) --总数2 
    from tbl_report_billing_data t 
    where FFP_CODE='CA' 
    and OPT_AIRLINE_CODE='UA' 
    and ACCOUNT_MONTH='200810' 
    group by FFP_CODE,OPT_AIRLINE_CODE,ACCOUNT_MONTH,ORIGINATION,RETROL_TYPE 
      

  3.   

    总数2里的子查询缺少了 RETROL_TYPE  这个条件