-1
私は次のフィールドを持つテーブルを持っています異なる月の基準に基づいて列を合計するMySQLクエリ
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行取得しています。
持ってどのようなあなたはこれまでに試しましたか? – lebelinoz
投稿を更新しました。私が何を試したかを見てください。 –
申し訳ありませんが、私は尋ねました。 https://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql-queryを参照してください。 – Strawberry