0

女士们,先生们,

我在 MySQL 5.1 中有以下问题。这是我的桌子。

Sale | Store | Product | Discount
---------------------------------
 1   |   1   |   B     | Yes
 2   |   1   |   B     | Yes
 3   |   1   |   B     | No
 4   |   1   |   D     | Yes
 5   |   1   |   A     | No
 6   |   2   |   A     | No
 7   |   3   |   B     | No
 8   |   3   |   B     | No
 9   |   1   |   D     | Yes
10   |   2   |   A     | No

现在,这当然是一种非常不方便的数据组织方式,但数据就是这样进来的(并将继续进来)。

我需要能够提供一个完全任意的产品类型列表(如果你愿意,可以用逗号分隔);比方说A,B,C。最后,我需要一份哪些商店卖什么的清单。这是一个中间结果,以便于理解。

Store | Product | Sales | Discounts
-----------------------------------
  1   |   A     |  1    | 0
  1   |   B     |  3    | 2
  1   |   C     |  0    | 0
  2   |   A     |  2    | 0
  2   |   B     |  0    | 0
  2   |   C     |  0    | 0
  3   |   A     |  0    | 0
  3   |   B     |  2    | 0
  3   |   C     |  0    | 0

最后,我需要的是:

Store | Product types sold | Discounts given on product types:
-------------------------------------------------------------
  1   |         2          |        1
  2   |         1          |        0
  3   |         1          |        0

第三列表示有多少已售出(和查询)的产品类型获得了至少一个折扣。

我已经尝试过各种动态交叉表生成查询(是的,我都看过了),但是我没有足够的脑力内存来一次完成所有事情。您将如何以最有效的方式处理此问题?临时表/存储过程等都可以,非常欢迎粗略的想法大纲。谢谢!

4

2 回答 2

3

您可以按商店和产品对数据进行分组以生成中间结果:

mysql> SELECT Store, Product,
COUNT(1) AS Sales,
SUM(IF(Discount='Yes', 1, 0)) AS Discounts
FROM input
WHERE Product IN ('A', 'B', 'C', 'D')
GROUP BY Store, Product
+-------+---------+-------+-----------+
| Store | Product | Sales | Discounts |
+-------+---------+-------+-----------+
|     1 | A       |     1 |         0 |
|     1 | B       |     3 |         2 |
|     1 | D       |     2 |         2 |
|     2 | A       |     2 |         0 |
|     3 | B       |     2 |         0 |
+-------+---------+-------+-----------+
5 rows in set (0.00 sec)

然后,您可以仅按商店将此结果分组,以将其转换为您的最终结果:

mysql> SELECT Store,
COUNT(1) AS `Product types sold`,
SUM(Discounts) AS `Discounts given on product types`
FROM (
SELECT Store, Product,
COUNT(1) AS Sales,
SUM(IF(Discount='Yes', 1, 0)) AS Discounts
FROM input
WHERE Product IN ('A', 'B', 'C', 'D')
GROUP BY Store, Product
) AS intermediate
GROUP BY Store
+-------+--------------------+----------------------------------+
| Store | Product types sold | Discounts given on product types |
+-------+--------------------+----------------------------------+
|     1 |                  3 |                                4 |
|     2 |                  1 |                                0 |
|     3 |                  1 |                                0 |
+-------+--------------------+----------------------------------+
3 rows in set (0.00 sec)

请注意,第一个查询是第二个查询的子查询。

于 2011-08-02T17:47:13.363 回答
1

我认为只是分离查询是最简单的:

select Store, 
   (Select count( distinct Product) from products as p2 where p2.Store = p1.Store),
   (Select count(distinct Product) from products as p3 where p3.Store = p1.Store and p3.Discount = "Yes")
from products p1
group by Store

(如果是大型表,请务必测试您的性能。)

于 2011-08-02T17:54:02.480 回答