统计商品各项数据sql&统计历史数据sql

1.统计商品各项数据

需求就是将统计商品的 成交总数、订单数、商品价格、访问数量等。写了个sql记录一下,以便以后遇到类似的需求方便查看。

SELECT
	 productName,orderIdCount,actualPrice,accountCount,askNum
FROM
	(
SELECT
	count( order_id ) orderIdCount,
	product_id,
	p.NAME productName
FROM
	(
SELECT
	d.order_id,
	d.product_id,
	o.create_time,
	o.actual_price 
FROM
	yun_job_shop_order_details d
	LEFT JOIN yun_job_shop_order o ON d.order_id = o.order_id 
WHERE
	product_id IN ( 180, 170 ) 
	AND date( d.create_time ) IN ( '2022-10-26', '2022-10-25' ) 
	) b
	LEFT JOIN yun_job_shop_product p ON p.id = product_id 
GROUP BY
	product_id 
	) a left join (
	SELECT
product_id,
sum(actual_price) actualPrice
FROM
	(
SELECT
	d.order_id,
	d.product_id,
	o.create_time,
	o.actual_price 
FROM
	yun_job_shop_order_details d
	LEFT JOIN yun_job_shop_order o ON d.order_id = o.order_id 
WHERE
	product_id IN ( 180,170 ) 
	AND date( d.create_time ) IN ( '2022-10-26', '2022-10-25' ) 
	) b
	left join yun_job_shop_product p on
	p.id=product_id
	GROUP BY product_id) b on a.product_id=b.product_id
	
	left join (	SELECT
	product_id,
	count(b.account) accountCount
	FROM
		(
	SELECT
		d.order_id,
		d.product_id,
		o.create_time,
		o.account 
	FROM
		yun_job_shop_order_details d
		LEFT JOIN yun_job_shop_order o ON d.order_id = o.order_id 
	WHERE
		product_id IN ( 180,170 ) 
		AND date( d.create_time ) IN ( '2022-10-26', '2022-10-25' ) 
		GROUP BY 	o.account 
		) b
		left join yun_job_shop_product p on
		p.id=product_id
		GROUP BY product_id)c on c.product_id=a.product_id
		left join (SELECT
            count(b.account) askNum,
            product_id
				
            FROM
            (
            SELECT
            account,
            product_id,
            date(create_time)
            FROM
            yun_job_shop_footprint
            WHERE
            product_id 
          IN ( 180,170 ) 
            and date(create_time) 
           IN ( '2022-10-26', '2022-10-25' ) 
            ) b left join yun_job_shop_product p
            on b.product_id = p.id
            GROUP BY b.product_id
						)d on d.product_id=a.product_id
mybatis中的写法
 SELECT
        product_id productId,productName,orderIdCount,actualPrice,accountCount,askNum,payCount
        FROM
            (
                SELECT
                    count( order_id ) orderIdCount,
                    product_id,
                    p.NAME productName
                FROM
                    (
                        SELECT
                            d.order_id,
                            d.product_id,
                            o.create_time,
                            o.actual_price
                        FROM
                            yun_job_shop_order_details d
                                LEFT JOIN yun_job_shop_order o ON d.order_id = o.order_id
                        WHERE
                            product_id IN 
                                               #{id}
                                          
                          AND date( d.create_time ) IN
                                                   
                                                    #{day}
                                                   
            ) b
                LEFT JOIN yun_job_shop_product p ON p.id = product_id
        GROUP BY
            product_id
            ) a left join (
	SELECT
product_id,
sum(actual_price) actualPrice
FROM
	(
SELECT
	d.order_id,
	d.product_id,
	o.create_time,
	o.actual_price
FROM
	yun_job_shop_order_details d
	LEFT JOIN yun_job_shop_order o ON d.order_id = o.order_id
WHERE
	product_id IN  
        #{id}
    
	AND date( d.create_time ) IN 
        #{day}
    
	) b
	left join yun_job_shop_product p on
	p.id=product_id
	GROUP BY product_id) b on a.product_id=b.product_id

            left join (	SELECT
            product_id,
            count(b.account) accountCount
            FROM
            (
            SELECT
            d.order_id,
            d.product_id,
            o.create_time,
            o.account
            FROM
            yun_job_shop_order_details d
            LEFT JOIN yun_job_shop_order o ON d.order_id = o.order_id
            WHERE
            product_id IN 
        #{id}
    
            AND date( d.create_time ) IN 
        #{day}
    
            GROUP BY 	o.account
            ) b
            left join yun_job_shop_product p on
            p.id=product_id
            GROUP BY product_id)c on c.product_id=a.product_id
            left join (SELECT
            count(b.account) askNum,
            product_id

            FROM
            (
            SELECT
            account,
            product_id,
            date(create_time)
            FROM
            yun_job_shop_footprint
            WHERE
            product_id
            IN 
        #{id}
    
            and date(create_time)
            IN 
        #{day}
    
            ) b left join yun_job_shop_product p
            on b.product_id = p.id
            GROUP BY b.product_id
            )d on d.product_id=a.product_id
            left join (SELECT
            count(order_id) payCount,
            product_id
            FROM
            (
            SELECT
            d.order_id,
            d.product_id,
            o.create_time,
            o.actual_price
            FROM
            yun_job_shop_order_details d
            LEFT JOIN yun_job_shop_order o ON d.order_id = o.order_id
            WHERE
            product_id IN 
        #{id}
    
            AND date( d.create_time ) IN  
        #{day}
    
            and o.order_state in (1,2,4)
            ) b
            left join yun_job_shop_product p on
            p.id=product_id
            GROUP BY product_id)e on e.product_id=a.product_id
        limit #{pagination.startRow},#{pagination.pageSize}

2.统计历史数据

 需求:获取每天的访问的商品的数量。使用天作为条

SELECT a.startTime,IFNULL(b.number,0) AS actualPrice
            FROM (
            
                SELECT date(#{day}) startTime
            
            ) a LEFT JOIN (
            SELECT DATE(create_time) AS timee,  sum(actual_price) AS number
            FROM yun_job_shop_order
            where
            DATE_FORMAT(create_time, '%Y-%m-%d' ) IN
            
                #{day}
            
            and
            order_id in
            
                #{day.orderId}
            
            AND order_state in (1,2,3)
            GROUP BY DATE(create_time)
            ) b ON a.startTime = b.timee;

你可能感兴趣的:(sql,数据库)