下面是我的brand_of_items表的架构。为简单起见,此处显示了两列:id(主要和AI),符号(varchar 50,唯一)
Table - brand_of_items
id symbol
0 a
1 b
2 c
.. ..
10 j
下面是我的items_of_brand的架构。
Table - mainIndexQuantity
id brand_of_items_id vol item_type salefinalizeddate
0 1 5 0 2005-5-11
1 1 6 0 2004-5-11
2 1 7 0 2011-5-11
3 1 8 0 2011-5-12
4 1 9 0 2011-5-12
5 1 10 0 2011-5-11
6 1 5 1 2012-5-11
7 1 6 1 2012-5-11
8 1 7 1 2011-5-11
9 1 8 1 2010-5-12
10 1 9 1 2012-5-12
11 1 10 1 2005-5-12
mainIndexQuantity表brand_of_items_id列是指向brand_of_items(id)的外键。 mainIndexQuantity表item_type列不是外键,它应该是。
这两个项目类型是:0 =零售,1 =批发
我想计算每个brand_brand_of_items表条目的项目类型(零售与批发)的比率。目标是看一个品牌商品是在零售还是批发销售更多。
** 增加复杂性: 我想在mainIndexQuantity表中添加一个日期列,并希望找出RetailVolume和WholesaleVolume之和的差异,并按salefinalizeddate字段对结果进行分组。
这是为了帮助确定哪些季节中销售的商品更多以及零售价格和&的总和(delta)差异。 WholeSaleVolume将帮助选择最受关注的项目。
答案 0 :(得分:5)
试试这个:
SELECT
b.id,
b.symbol,
IFNULL(SUM(m.item_type = 1), 0) / (COUNT(*) * 1.0) AS wholesaleRatio,
IFNULL(SUM(m.item_type = 0), 0) / (COUNT(*) * 1.0) AS RetailRatio
FROM brand_of_items b
LEFT JOIN mainIndexQuantity m ON b.id = m.brand_of_items_id
GROUP BY b.id,
b.symbol;
这会给你:
| ID | SYMBOL | WHOLESALERATIO | RETAILRATIO |
----------------------------------------------
| 0 | a | 0 | 0 |
| 1 | b | 0.5 | 0.5 |
| 2 | c | 0 | 0 |
| 10 | j | 0 | 0 |
假设:
wholesaleRatio
是整个销售类型的项目计数到所有项目的计数。RetailRatio
是所有项目计数类型retail
的项目数。如果这个比例用于vol
列与总vol
的总和,则可以改为:
SELECT
b.id,
b.symbol,
SUM(CASE WHEN m.item_type = 1 THEN m.vol ELSE 0 END) / SUM(m.vol) AS wholesaleRatio,
SUM(CASE WHEN m.item_type = 0 THEN m.vol ELSE 0 END) / SUM(m.vol) AS RetailRatio
FROM brand_of_items b
LEFT JOIN mainIndexQuantity m ON b.id = m.brand_of_items_id
GROUP BY b.id,
b.symbol;
请注意:
LEFT JOIN
,因此您在结果集中获得了不匹配的行,即那些没有MainIndexQuantity
表条目的品牌商品。如果您不想包含它们,请改用INNER JOIN
。1.0
相乘得到小数位数,如@JW所述。要包含Total Volume
,Retail Volume Sum
和Wholesale Volume sum
,请尝试以下操作:
SELECT
b.id,
b.symbol,
IFNULL(SUM(m.item_type = 1), 0) * 1.0 / COUNT(*) AS wholesaleRatio,
IFNULL(SUM(m.item_type = 0), 0) * 1.0 / COUNT(*) AS RetailRatio,
IFNULL(SUM(m.vol), 0) AS 'Total Volume',
SUM(CASE WHEN m.item_type = 1 THEN m.vol ELSE 0 END) AS 'Retail Volume sum',
SUM(CASE WHEN m.item_type = 1 THEN m.vol ELSE 0 END) AS 'Wholesale Volume sum'
FROM brand_of_items b
LEFT JOIN mainIndexQuantity m ON b.id = m.brand_of_items_id
GROUP BY b.id,
b.symbol;
这会给你:
| ID | SYMBOL | WHOLESALERATIO | RETAILRATIO | TOTAL VOLUME | RETAIL VOLUME SUM | WHOLESALE VOLUME SUM |
--------------------------------------------------------------------------------------------------------
| 0 | a | 0 | 0 | 0 | 0 | 0 |
| 1 | b | 0.5 | 0.5 | 90 | 45 | 45 |
| 2 | c | 0 | 0 | 0 | 0 | 0 |
| 10 | j | 0 | 0 | 0 | 0 | 0 |
如果要按这些总和和总和对结果集进行排序,请将此查询放在子查询中,然后执行此操作:
SELECT *
FROM
(
SELECT
b.id,
b.symbol,
IFNULL(SUM(m.item_type = 1), 0) * 1.0 / COUNT(*) AS wholesaleRatio,
IFNULL(SUM(m.item_type = 0), 0) * 1.0 / COUNT(*) AS RetailRatio,
IFNULL(SUM(m.vol), 0) AS TotalVolume,
SUM(CASE WHEN m.item_type = 1 THEN m.vol ELSE 0 END) AS RetailVolumeSum,
SUM(CASE WHEN m.item_type = 1 THEN m.vol ELSE 0 END) AS WholesaleVolumeSum
FROM brand_of_items b
LEFT JOIN mainIndexQuantity m ON b.id = m.brand_of_items_id
GROUP BY b.id,
b.symbol
) AS sub
ORDER BY RetailVolumeSum DESC,
WholesaleVolumeSum DESC;
但是你的最后一个要求并不清楚,你是否正在寻找那些具有最高retio / wholesale ratis和volumns或者选择最高值的物品?
对于后者:
SELECT *
FROM
(
SELECT
b.id,
b.symbol,
IFNULL(SUM(m.item_type = 1), 0) * 1.0 / COUNT(*) AS wholesaleRatio,
IFNULL(SUM(m.item_type = 0), 0) * 1.0 / COUNT(*) AS RetailRatio,
IFNULL(SUM(m.vol), 0) AS TotalVolume,
SUM(CASE WHEN m.item_type = 1 THEN m.vol ELSE 0 END) AS RetailVolumeSum,
SUM(CASE WHEN m.item_type = 1 THEN m.vol ELSE 0 END) AS WholesaleVolumeSum
FROM brand_of_items b
LEFT JOIN mainIndexQuantity m ON b.id = m.brand_of_items_id
GROUP BY b.id,
b.symbol
) AS sub
ORDER BY RetailVolumeSum DESC,
WholesaleVolumeSum DESC,
TotalVolume DESC
LIMIT 1;
要获得总体积最高的品牌,您可以这样做:
SELECT
b.id,
b.symbol,
IFNULL(SUM(m.item_type = 1), 0) * 1.0 / COUNT(*) AS wholesaleRatio,
IFNULL(SUM(m.item_type = 0), 0) * 1.0 / COUNT(*) AS RetailRatio,
IFNULL(SUM(m.vol), 0) AS TotalVolume,
SUM(CASE WHEN m.item_type = 1 THEN m.vol ELSE 0 END) AS RetailVolumeSum,
SUM(CASE WHEN m.item_type = 1 THEN m.vol ELSE 0 END) AS WholesaleVolumeSum
FROM brand_of_items b
LEFT JOIN mainIndexQuantity m ON b.id = m.brand_of_items_id
GROUP BY b.id,
b.symbol
HAVING SUM(m.vol) = (SELECT MAX(TotalVolume)
FROM
(
SELECT brand_of_items_id, SUM(vol) AS TotalVolume
FROM mainIndexQuantity
GROUP BY brand_of_items_id
) t);
请注意:
这将为您提供总体积最高的品牌,如果您正在寻找具有最高比率的品牌,您必须更换having子句以获得比率的最大值而不是最大值总量。
这将为您提供总体积最高的项目,因此如果有多个项目的总体积最高,您可能会有超过项目的内容,例如this updated fiddle demo。在这种情况下,要只获得一个,您必须使用LIMIT
才能返回一个。