YII2 SQL:获取有条件的列数

时间:2018-05-07 07:04:35

标签: mysql sql yii2

我希望根据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的行的总和。 有人能告诉我我做错了吗?

0 个答案:

没有答案