2017-11-25 57 views
1

我想要像這樣使用每月1到12的select查詢。我從互聯網搜索子查詢格式是這樣的小,但它是不正確的格式根據我的查詢。當我運行這個查詢單個月時,它顯示正確的結果。我想顯示HTML表格的結果是這樣 SQL FiddleMysql多重選擇查詢按月顯示總計結果

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

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

回答

0

你是在正確的道路上。但是,因爲您想每月獲得sum()current_sales,所以您不應在子查詢中使用Group by,因爲它將返回多行。取而代之的是,只要where條件爲當前正在使用Group by子句執行的查詢獲取相同的pro_id行。

下面的查詢將工作:

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子句WHERE d_id = 4,所以它應該只顯示這個結果,但是當我應用它顯示每個d_id相同的結果。我該怎麼辦?再次感謝 – user3030814