2017-11-25 9 views
1

毎月1から12のような選択クエリを使用したいと思います。私はインターネットのサブクエリのフォーマットから少しこれのような検索したが、私のクエリによると正しいフォーマットではありません。このクエリを1ヶ月間実行すると、正しい結果が表示されます。私はあなたが正しい道にしている。この SQL Fiddle月ごとの合計結果を表示するMysqlの複数選択クエリ

Html table Format

SELECT 
(SELECT SUM(`current_sales`) FROM `orders` WHERE YEAR(order_date) ='2017' AND MONTH(order_date) ='10' GROUP BY pro_id) AS October, 
(SELECT SUM(`current_sales`) FROM `orders` WHERE YEAR(order_date) ='2017' AND MONTH(order_date) ='11' GROUP BY pro_id) AS November, 
(SELECT SUM(`current_sales`) FROM `orders`WHERE YEAR(order_date) ='2017' AND MONTH(order_date) ='12' GROUP BY pro_id) AS December 
+1

サンプルデータを入力してください。 –

+0

このSQLFiddle-> http://sqlfiddle.com/#!9/feb9a/1 – user3030814

答えて

0

ようなHTMLテーブルに結果を表示したいです。しかし、sum()を毎月current_salesにしたいので、サブクエリではGroup byを使用しないでください。複数の行が返されるためです。代わりに、同じpro_idの行をフェッチするためにwhereという条件を入力してください。クエリは現在、Group by句を使用して実行されています。

次のクエリは動作します:

select tmp.pro_id, 
     tmp.product_name, 
     tmp.nsp,  
     tmp.Jan, 
     tmp.Feb, 
     tmp.Mar, 
     tmp.Apr, 
     (tmp.Jan+tmp.Feb+tmp.Mar+tmp.Apr) as Q1, 
     tmp.May, 
     tmp.Jun, 
     tmp.Jul, 
     tmp.Aug, 
     (tmp.May+tmp.Jun+tmp.Jul+tmp.Aug) as Q2, 
     tmp.Sep, 
     tmp.Oct, 
     tmp.Nov, 
     tmp.`Dec`, 
     (tmp.Sep+tmp.Oct+tmp.Nov+tmp.`Dec`) as Q3 
From  
( 
SELECT o.pro_id, 
     p.product_name, 
     p.nsp, 
(case when coalesce(sum(month(order_date) = 1),0) <> 0 
     then 
      (SELECT SUM(`current_sales`) FROM `orders` WHERE pro_id = o.pro_id and YEAR(order_date) ='2017' AND MONTH(order_date) ='1') 
else 0 
end 
) as Jan, 
(case when coalesce(sum(month(order_date) = 2),0) <> 0 
     then 
      (SELECT SUM(`current_sales`) FROM `orders` WHERE pro_id = o.pro_id and YEAR(order_date) ='2017' AND MONTH(order_date) ='2') 
else 0 
end 
) as Feb, 
(case when coalesce(sum(month(order_date) = 3),0) <> 0 
     then 
      (SELECT SUM(`current_sales`) FROM `orders` WHERE pro_id = o.pro_id and YEAR(order_date) ='2017' AND MONTH(order_date) ='3') 
else 0 
end 
) as Mar, 
(case when coalesce(sum(month(order_date) = 4),0) <> 0 
     then 
      (SELECT SUM(`current_sales`) FROM `orders` WHERE pro_id = o.pro_id and YEAR(order_date) ='2017' AND MONTH(order_date) ='4') 
else 0 
end 
) as Apr, 
(case when coalesce(sum(month(order_date) = 5),0) <> 0 
     then 
      (SELECT SUM(`current_sales`) FROM `orders` WHERE pro_id = o.pro_id and YEAR(order_date) ='2017' AND MONTH(order_date) ='5') 
else 0 
end 
) as May, 
(case when coalesce(sum(month(order_date) = 6),0) <> 0 
     then 
      (SELECT SUM(`current_sales`) FROM `orders` WHERE pro_id = o.pro_id and YEAR(order_date) ='2017' AND MONTH(order_date) ='6') 
else 0 
end 
) as Jun, 
(case when coalesce(sum(month(order_date) = 7),0) <> 0 
     then 
      (SELECT SUM(`current_sales`) FROM `orders` WHERE pro_id = o.pro_id and YEAR(order_date) ='2017' AND MONTH(order_date) ='7') 
else 0 
end 
) as Jul, 
(case when coalesce(sum(month(order_date) = 8),0) <> 0 
     then 
      (SELECT SUM(`current_sales`) FROM `orders` WHERE pro_id = o.pro_id and YEAR(order_date) ='2017' AND MONTH(order_date) ='8') 
else 0 
end 
) as Aug, 
(case when coalesce(sum(month(order_date) = 9),0) <> 0 
     then 
      (SELECT SUM(`current_sales`) FROM `orders` WHERE pro_id = o.pro_id and YEAR(order_date) ='2017' AND MONTH(order_date) ='9') 
else 0 
end 
) as Sep, 
(case when coalesce(sum(month(order_date) = 10),0) <> 0 
     then 
      (SELECT SUM(`current_sales`) FROM `orders` WHERE pro_id = o.pro_id and YEAR(order_date) ='2017' AND MONTH(order_date) ='10') 
else 0 
end 
) as Oct, 
(case when coalesce(sum(month(order_date) = 11),0) <> 0 
     then 
      (SELECT SUM(`current_sales`) FROM `orders` WHERE pro_id = o.pro_id and YEAR(order_date) ='2017' AND MONTH(order_date) ='11') 
else 0 
end 
) as Nov, 
(case when coalesce(sum(month(order_date) = 12),0) <> 0 
     then 
      (SELECT SUM(`current_sales`) FROM `orders` WHERE pro_id = o.pro_id and YEAR(order_date) ='2017' AND MONTH(order_date) ='12') 
else 0 
end 
) as `Dec` 
from products p 
inner join orders o 
on p.pro_id = o.pro_id 
group by o.pro_id 
)tmp 
group by tmp.pro_id 
; 

Click here for DEMO

、私はGroup_concat()のようなMySQLの多くの内蔵機能を備えた巨大なクエリを持っているあなたのタスクのための別のアプローチ、Substring_Index()などがありますが

別のアプローチをご覧ください:

select tmp2.pro_id, 
     tmp2.product_name, 
     tmp2.nsp, 
     tmp2.Jan, 
     tmp2.Feb, 
     tmp2.Mar, 
     tmp2.Apr, 
     (tmp2.Jan+tmp2.Feb+tmp2.Mar+tmp2.Apr) as Q1, 
     tmp2.May, 
     tmp2.Jun, 
     tmp2.Jul, 
     tmp2.Aug, 
     (tmp2.May+tmp2.Jun+tmp2.Jul+tmp2.Aug) as Q2, 
     tmp2.Sep, 
     tmp2.Oct, 
     tmp2.Nov, 
     tmp2.`Dec`, 
     (tmp2.Sep+tmp2.Oct+tmp2.Nov+tmp2.`Dec`) as Q3 
from 
(
select tmp.pro_id, 
     tmp.product_name, 
     tmp.nsp,    
(case when coalesce(sum(tmp.month=1),0) <> 0 
     then 
     substring_index 
     (
      substring_index 
      (
      Group_concat(tmp.total order by tmp.month separator ','), 
      ',',    
       (find_in_set 
        (1, 
        Group_concat(tmp.month order by tmp.month separator ',') 
        ) 
       ) 
      ), 
      ',', 
      -1     
     ) 
    else 0 
end 
) as Jan, 
(case when coalesce(sum(tmp.month=2),0) <> 0 
     then 
     substring_index 
     (
      substring_index 
      (
      Group_concat(tmp.total order by tmp.month separator ','), 
      ',',    
       (find_in_set 
        (2, 
        Group_concat(tmp.month order by tmp.month separator ',') 
        ) 
       ) 
      ), 
      ',', 
      -1     
     ) 
    else 0 
end 
) as Feb, 
(case when coalesce(sum(tmp.month=3),0) <> 0 
     then 
     substring_index 
     (
      substring_index 
      (
      Group_concat(tmp.total order by tmp.month separator ','), 
      ',',    
       (find_in_set 
        (3, 
        Group_concat(tmp.month order by tmp.month separator ',') 
        ) 
       ) 
      ), 
      ',', 
      -1     
     ) 
    else 0 
end 
) as Mar, 
(case when coalesce(sum(tmp.month=4),0) <> 0 
     then 
     substring_index 
     (
      substring_index 
      (
      Group_concat(tmp.total order by tmp.month separator ','), 
      ',',    
       (find_in_set 
        (4, 
        Group_concat(tmp.month order by tmp.month separator ',') 
        ) 
       ) 
      ), 
      ',', 
      -1     
     ) 
    else 0 
end 
) as Apr, 
(case when coalesce(sum(tmp.month=5),0) <> 0 
     then 
     substring_index 
     (
      substring_index 
      (
      Group_concat(tmp.total order by tmp.month separator ','), 
      ',',    
       (find_in_set 
        (5, 
        Group_concat(tmp.month order by tmp.month separator ',') 
        ) 
       ) 
      ), 
      ',', 
      -1     
     ) 
    else 0 
end 
) as May, 
(case when coalesce(sum(tmp.month=6),0) <> 0 
     then 
     substring_index 
     (
      substring_index 
      (
      Group_concat(tmp.total order by tmp.month separator ','), 
      ',',    
       (find_in_set 
        (6, 
        Group_concat(tmp.month order by tmp.month separator ',') 
        ) 
       ) 
      ), 
      ',', 
      -1     
     ) 
    else 0 
end 
) as Jun, 
(case when coalesce(sum(tmp.month=7),0) <> 0 
     then 
     substring_index 
     (
      substring_index 
      (
      Group_concat(tmp.total order by tmp.month separator ','), 
      ',',    
       (find_in_set 
        (7, 
        Group_concat(tmp.month order by tmp.month separator ',') 
        ) 
       ) 
      ), 
      ',', 
      -1     
     ) 
    else 0 
end 
) as Jul, 
(case when coalesce(sum(tmp.month=8),0) <> 0 
     then 
     substring_index 
     (
      substring_index 
      (
      Group_concat(tmp.total order by tmp.month separator ','), 
      ',',    
       (find_in_set 
        (8, 
        Group_concat(tmp.month order by tmp.month separator ',') 
        ) 
       ) 
      ), 
      ',', 
      -1     
     ) 
    else 0 
end 
) as Aug, 
(case when coalesce(sum(tmp.month=9),0) <> 0 
     then 
     substring_index 
     (
      substring_index 
      (
      Group_concat(tmp.total order by tmp.month separator ','), 
      ',',    
       (find_in_set 
        (9, 
        Group_concat(tmp.month order by tmp.month separator ',') 
       ) 
       ) 
      ), 
      ',', 
      -1     
     ) 
    else 0 
end 
) as Sep, 
(case when coalesce(sum(tmp.month=10),0) <> 0 
     then 
     substring_index 
     (
      substring_index 
      (
      Group_concat(tmp.total order by tmp.month separator ','), 
      ',',    
       (find_in_set 
        (10, 
        Group_concat(tmp.month order by tmp.month separator ',') 
        ) 
       ) 
      ), 
      ',', 
      -1     
     ) 
    else 0 
end 
) as Oct,      
(case when coalesce(sum(tmp.month=11),0) <> 0 
     then 
     substring_index 
     (
      substring_index 
      (
      Group_concat(tmp.total order by tmp.month separator ','), 
      ',',    
       (find_in_set 
        (11, 
        Group_concat(tmp.month order by tmp.month separator ',') 
        ) 
       ) 
      ), 
      ',', 
      -1     
     ) 
    else 0 
end 
) as Nov, 
(case when coalesce(sum(tmp.month=12),0) <> 0 
     then 
     substring_index 
     (
      substring_index 
      (
      Group_concat(tmp.total order by tmp.month separator ','), 
      ',',    
       (find_in_set 
        (12, 
        Group_concat(tmp.month order by tmp.month separator ',') 
        ) 
       ) 
      ), 
      ',', 
      -1     
     ) 
    else 0 
end 
) as `Dec` 
from  
(
select o.pro_id, 
     p.product_name, 
     p.nsp, 
     sum(o.current_sales) as total, 
     month(order_date) as month 
from 
products p 
inner join orders o 
on p.pro_id = o.pro_id 
group by o.pro_id,month(order_date) 
)tmp 
group by tmp.pro_id 
)tmp2 
group by tmp2.pro_id 
; 

Click here for Demo

ここで、実際のデータに対して両方のクエリを実行し、実行時間が短いものを選択できます。

希望すると助かります!

+0

両方のアプローチを親切に検証し、両方のデモも確認してください。気をつけてください。 –

+0

ありがとう@Harshil Doshi。あなたは素晴らしいです。 – user3030814

+0

このクエリを適用してもうまくいきましたが、WHERE句d_id = 4を追加したいので、この結果だけを表示する必要がありますが、適用するとすべてのd_idに対して同じ結果が表示されます。私は何をすべきか?もう一度ありがとう – user3030814

関連する問題