sql对同一个字段不同状态分别进行统计数量

<!-- 统计供应商会员数量 -->
     <select id="sel_deal_sum_data" parameterClass="java.util.Map" resultClass="com.forlink.fkcore.data.ResultMap">
        SELECT
            count(if(w.seller_type,true,null)seller_type) as total_supplier,<!-- 统计供应商总的会员人数 -->
            count(if(w.seller_type=1,true,null)) as personage,<!-- 个人 -->
            count(if(w.seller_type=2,true,null)) as individual_business,<!-- 个体工商户 -->
            count(if(w.seller_type=3),true,null) as sellers<!-- 卖家数量 -->
        FROM (
            (SELECT
                t.deal_time                             AS                         deal_time,
                ifnull((select platform_name from tab_platform_apply where mall_id = c.n7),'全国')     AS plat_name,
                t.client_name                             AS                         client_name,
                t.other_name                             AS                         other_name,
                a.goods_name                             AS                         goods_name,
                a.name                                     AS                         name,
                a.num14                                 AS                         num14,
                a.att61                                 AS                         att61,
                a.contract_type                         AS                         contract_type,
                a.price                                    AS                         deal_price,
                t.status                                 AS                         deal_status,
                d.status                                 AS                         subs_status,
                '0'                                        AS                        data_source,
                date_format(t.pay_date, '%Y-%m-%d %H:%i:%s') AS                    pay_date_b,
                (SELECT date_format(pay_date, '%Y-%m-%d %H:%i:%s') FROM tab_goods_deal WHERE discussid = t.discussid AND buyorsell = 2) AS pay_date_s,
                date_format(d.date2, '%Y-%m-%d %H:%i:%s') AS                     date2,
                a.cat_id                                AS                        cat_id,
                a.goods_id                                AS                        goods_id,
                ifnull(oex.organic_type, 0)             AS                      organic_type,
                a.att72                                 AS                      payment_type,
                c.num4                                  AS                      seller_type
                
            FROM
                tab_goods_deal t
            LEFT JOIN tab_goods_discuss_detail a ON a.discussid = t.discussid
            LEFT JOIN tab_customer_group c ON c.cust_no = t.other_id
            LEFT JOIN tab_customer_group cb ON cb.cust_no = t.client_id
            LEFT JOIN tab_subs_goods_detail d ON d.buyorsell = 1 AND d.subs_id = t.discussid
            LEFT JOIN tab_gxs_orders_extend oex ON t.order_id = oex.order_id AND oex.auth_type = 0
            WHERE
                t.buyorsell = 1
            ) 
            UNION ALL
            (
            SELECT
                t.order_time                             AS                         deal_time,
                t.plat_name                             AS                         plat_name,
                t.buy_name                                 AS                         client_name,
                t.sell_name                             AS                         other_name,
                '外部数据'                                 AS                         goods_name,
                t.goods_name                             AS                         name,
                '0'                                     AS                         num14,
                '0'                                     AS                         att61,
                '10'                                     AS                         contract_type,
                t.price                                    AS                         deal_price,
                '99'                                     AS                         deal_status,
                '99'                                     AS                         subs_status,
                '1'                                        AS                        data_source,
                date_format(t.order_time, '%Y-%m-%d %H:%i:%s')     AS                pay_date_b,
                date_format(t.order_time, '%Y-%m-%d %H:%i:%s')    AS                 pay_date_s,
                date_format(t.order_time, '%Y-%m-%d %H:%i:%s')    AS                date2,
                '-1'                                    AS                        cat_id,
                '-1'                                    AS                        goods_id,
                '0'                                     AS                      organic_type,
                ''                                      AS                      payment_type,
                '0'                                     AS                      seller_type
            FROM
                tab_gxs_other_order t 
            WHERE t.status = 1
             ) 
        ) w
        <include refid="sel_deal_sum_query_where" />
    </select> 

上面的 count(if(w.seller_type,true,null)seller_type) as total_supplier,<!-- 统计供应商总的会员人数 -->
            count(if(w.seller_type=1,true,null)) as personage,<!-- 个人 -->
            count(if(w.seller_type=2,true,null)) as individual_business,<!-- 个体工商户 -->
            count(if(w.seller_type=3),true,null) as sellers<!-- 卖家数量 -->  类似于三目运算

猜你喜欢

转载自blog.csdn.net/lala12d/article/details/82762377