yii2sql:获取带条件的列的计数

bjg7j2ky  于 2021-06-25  发布在  Mysql
关注(0)|答案(0)|浏览(186)

我想得到 li_units_bought 对于基于的行 li_order_id .
我正在为它连接多个表。我的问题是:

$query = (new \yii\db\Query())
    ->select('oid, ord_tracking_no as `Tracking No`, site_name as Region,adv_name as Advertiser, line_id as LineId, li_name as LineName
            , li_status as StatusCode, prd_desc as Product, li_version as Version, date_format(lrh_updated_date,"%Y-%m-%d %H:%i") as `Submit Date`
            , date_format(li_start_date,"%Y-%m-%d %H:%i") as `Start Date`
            , date_format(li_end_date,"%Y-%m-%d %H:%i") as `End Date`, li_is_rush as isRushOrder
            , li_type as Type, ord_assigned_user as `Assigned User`, status_desc as Status

            , SUM(`li_units_bought`) as Goal
            , ldp_total_delivery as `Delivered`
            , if(li_type="STANDARD",((ldp_pacing*180)/100)+1,((ldp_pacing+1)*1*180)) as Pacing
            , li_cost_type as `Cost Type`
            , ord_total_budget as `Total Budget`
            , li_is_automated
            ,li_target_server as Adserver
            ,li_del_pac_indicator as `DFP Report`
            ,li_submit_by
            ' )
            ->from('lineitems')
            ->innerJoin('orders','oid = li_order_id')
            ->leftJoin('advertisers','adv_id=ord_adv_id')
            ->leftJoin('sites','site_id=ord_site_id')
            ->leftJoin('products','prd_id=li_product')
            ->leftJoin('status_ref','status_id=li_status')
            ->leftJoin('users',"user_id='".$userid."'")
            ->leftJoin('user_role_profiles','urp_id=user_primary_role')
            ->leftJoin('lineitem_delivery_pacing','ldp_line_id = line_id')
            ->innerJoin("user_site_assoc","usc_site_id=ord_site_id and usc_userid='".$_SESSION['userId']."'")
            ->innerJoin("lineitem_revision_history", "lrh_lineitemid = line_id")
            ->where(" li_status not in ('Z','X') $cond")
            ->andWhere("li_order_id = oid")

            ->groupBy('line_id');

我只得到 li_units_bought 一排。不是具有相同属性的行的总和 li_order_id . 有人能告诉我我做错了什么吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题