我有一个包含以下字段的表
Suf_id |类别|子类别|收藏|供应商| quantity_sold |销售|月|年
我想根据单个mysql查询中选定的年持续时间,将过去1个月,过去3个月和过去12个月的quantity_sold和sales列相加。
我尝试了以下查询。
SELECT `category`, `subcategory`, `collection`, `vendor_id`,
(SELECT SUM(quantity_sold)
FROM `sales_upload_file` as s2
where file_month = 5
and file_year = 2017
and s2.`category` = s1.category
and s2.`subcategory` = s1.subcategory
and s2.`collection` = s1.collection
and s2. `vendor_id` = s1.vendor_id) as month_1_sold,
(SELECT SUM(sales)
FROM `sales_upload_file` as s2
where file_month = 5
and file_year = 2017
and s2.`category` = s1.category
and s2.`subcategory` = s1.subcategory
and s2.`collection` = s1.collection
and s2. `vendor_id` = s1.vendor_id) as month_1_sale,
(SELECT SUM(quantity_sold)
FROM `sales_upload_file` as s2
where file_month <= 5
and file_month >= 3
and file_year = 2017
and s2.`category` = s1.category
and s2.`subcategory` = s1.subcategory
and s2.`collection` = s1.collection
and s2. `vendor_id` = s1.vendor_id) as month_3_sold,
(SELECT SUM(sales)
FROM `sales_upload_file` as s2
where file_month <= 5
and file_month >= 3
and file_year = 2017
and s2.`category` = s1.category
and s2.`subcategory` = s1.subcategory
and s2.`collection` = s1.collection
and s2. `vendor_id` = s1.vendor_id) as month_3_sale,
(SELECT SUM(quantity_sold)
FROM `sales_upload_file` as s2
where file_month <= 5
and file_month >= 1
and file_year = 2017
and s2.`category` = s1.category
and s2.`subcategory` = s1.subcategory
and s2.`collection` = s1.collection
and s2. `vendor_id` = s1.vendor_id
UNION
SELECT SUM(quantity_sold)
FROM `sales_upload_file` as s2
where file_month >= 6
and file_month <= 12
and file_year = 2016
and s2.`category` = s1.category
and s2.`subcategory` = s1.subcategory
and s2.`collection` = s1.collection
and s2. `vendor_id` = s1.vendor_id) as month_12_sold,
(SELECT SUM(sales)
FROM `sales_upload_file` as s2
where file_month <= 5
and file_month >= 1
and file_year = 2017
and s2.`category` = s1.category
and s2.`subcategory` = s1.subcategory
and s2.`collection` = s1.collection
and s2. `vendor_id` = s1.vendor_id
UNION
SELECT SUM(sales)
FROM `sales_upload_file` as s2
where file_month >= 6
and file_month <= 12
and file_year = 2016
and s2.`category` = s1.category
and s2.`subcategory` = s1.subcategory
and s2.`collection` = s1.collection
and s2. `vendor_id` = s1.vendor_id) as month_12_sale
FROM `sales_upload_file` as `s1`
WHERE `processed` = '1'
AND `pc` = '1' GROUP BY `category`, `subcategory`, `collection`, `vendor_id` ORDER BY `category`, `subcategory`, `collection`, `vendor_id`
我收到错误子查询返回超过1行
我们将不胜感激。
答案 0 :(得分:0)
我认为您可以通过这种方式更改查询(我无法进行任何测试,因为您没有发布创建脚本和示例数据:请发布以获得更多帮助):
SELECT s1.category
,s1.subcategory
,s1.COLLECTION
,s1.vendor_id
, SUM(s2.quantity_sold) AS month_1_sold
, SUM(s2.sales) AS month_1_sale
, SUM(s3.quantity_sold) AS month_3_sold
, SUM(s3.sales) AS month_3_sale
, SUM(s4.quantity_sold) AS month_12_sold
, SUM(s4.sales) AS month_12_sale
FROM sales_upload_file s1
LEFT JOIN sales_upload_file s2 ON s2.category = s1.category
AND s2.subcategory = s1.subcategory
AND s2.COLLECTION = s1.COLLECTION
AND s2.vendor_id = s1.vendor_id
AND s2.file_year = 2017 AND s2.file_month = 5
LEFT JOIN sales_upload_file s3 ON s3.category = s1.category
AND s3.subcategory = s1.subcategory
AND s3.COLLECTION = s1.COLLECTION
AND s3.vendor_id = s1.vendor_id
AND s3.file_year = 2017 AND s3.file_month >= 3 AND s3.file_month<=5
LEFT JOIN sales_upload_file s4 ON s4.category = s1.category
AND s4.subcategory = s1.subcategory
AND s4.COLLECTION = s1.COLLECTION
AND s4.vendor_id = s1.vendor_id
AND ((s4.file_year = 2017 AND s4.file_month >= 1 AND s4.file_month<=5) OR (s4.file_year = 2016 AND s4.file_month >= 6 AND s4.file_month<=12))
WHERE s1.processed = '1'
AND s1.pc = '1'
GROUP BY s1.category
,s1.subcategory
,s1.COLLECTION
,s1.vendor_id
ORDER BY s1.category
,s1.subcategory
,s1.COLLECTION
,s1.vendor_id