在mysql的2列上使用派生表和分组方式

tsm1rwdh  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(314)

我使用的mysql服务器示例有一定的限制(没有cte,不能创建临时表,甚至不能创建新表),所以唯一可以解决的方法就是使用派生表。
我有下表 t ,包含每个月(1-12)的每次销售事件:

+------+-----------+-----------------+
|  id  |  product  |  sold_on_month  |
+------+-----------+-----------------+
|   1  | Product 1 |        1        |
|   2  | Product 1 |        2        |
|   3  | Product 1 |        2        |
|   4  | Product 1 |        3        |
|   5  | Product 1 |        3        |
|   6  | Product 1 |        4        |
|   7  | Product 1 |        4        |
|   8  | Product 1 |        4        |
|   9  | Product 1 |        4        |
|  10  | Product 1 |        4        |
|  11  | Product 2 |        1        |
|  12  | Product 2 |        1        |
|  13  | Product 2 |        2        |
|  14  | Product 2 |        2        |
|  15  | Product 2 |        3        |
|  16  | Product 2 |        3        |
+------+-----------+-----------------+

我想创建一个查询,回答我按产品和月份销售了多少产品,如下所示:

+-----------+---------+------------+
|  product  |  month  |  how_many  |
+-----------+---------+------------+
| Product 1 |    1    |     1      |
| Product 1 |    2    |     2      |
| Product 1 |    3    |     2      |
| Product 1 |    4    |     5      |
| Product 1 |    5    |     0      |
| Product 1 |    6    |     0      |
| Product 1 |    7    |     0      |
| Product 1 |    8    |     0      |
| Product 1 |    9    |     0      |
| Product 1 |   10    |     0      |
| Product 1 |   11    |     0      |
| Product 1 |   12    |     0      |
| Product 2 |    1    |     2      |
| Product 2 |    2    |     2      |
| Product 2 |    3    |     2      |
| Product 2 |    4    |     0      |
| Product 2 |    5    |     0      |
| Product 2 |    6    |     0      |
| Product 2 |    7    |     0      |
| Product 2 |    8    |     0      |
| Product 2 |    9    |     0      |
| Product 2 |   10    |     0      |
| Product 2 |   11    |     0      |
| Product 2 |   12    |     0      |
+-----------+---------+------------+

到目前为止,我取得的成就是:如果我有一个没有列的类似表 product 如果我想按月份分组,问题是:

SELECT
  a.mn, COUNT(t.sold_on_month)
FROM
  (SELECT 1 AS mn UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8
   UNION SELECT 9 UNION SELECT 10 UNION SELECT 11 UNION SELECT 12) AS a
  LEFT JOIN t
    ON t.sold_on_month = a.mn
GROUP BY a.mn;

所以,我真正需要的是一个查询,它使用相同的派生表来计算月份,并按产品和销售月份分组,显示 0how_many 当当月没有销售产品时。记住我不能使用cte或临时表。
感谢您的帮助!:)

bxpogfeg

bxpogfeg1#

使用查询生成不同的产品列表(如果您有一个products表,您可以引用它。)
只要给出问题中的表,我们就可以得到这样一个不同的列表

SELECT q.product
    FROM t q
   GROUP
      BY q.product

我们可以把它 Package 成一个内联视图(mysql术语中是一个派生表),然后交叉连接到生成的月份列表,

SELECT p.product
     , a.mn
  FROM ( -- distinct list of products 
         SELECT q.product
           FROM t q
          GROUP
             BY q.product
       ) p
 CROSS
  JOIN ( -- list of months
         SELECT 1 AS mn UNION ALL ... 
       ) a
 ORDER
    BY p.product
     , a.mn

然后我们可以加入到 t ,并在选择列表中进行分组和聚合。。。

SELECT p.product
     , a.mn
  -- , IFNULL(SUM(t.qty),0)    AS tot_qty
     , COUNT(t.sold_on_month)  AS cnt_rows
  FROM ( -- distinct list of products
         SELECT q.product
           FROM t q
          GROUP
             BY q.product
       ) p
 CROSS
  JOIN ( -- list of months
         SELECT 1 AS mn UNION ALL ... 
       ) a
  LEFT
  JOIN t t
    ON t.product       = p.product
   AND t.sold_on_month = a.mn 
 GROUP
    BY p.product
     , a.mn
 ORDER
    BY p.product
     , a.mn

如果内联视图有其他源 p ,而不是问题中的表(例如,具有不同产品列表的产品表),我们可以引用该表。
诀窍在于 CROSS JOIN 在“月”和“产品”之间。
然后使用外部联接从中获取匹配行 t .

相关问题