2016-04-14 16 views
0

Bu sorguları aldım ve bu sonuçları tek bir geçici tabloya eklemek istiyorum. Bunu nasıl yapabilirim?Birden çok sorgudan oluşan yeni bir tablo sonucuna ekleyin

select date(max(created_date)) AS 'Last Shipped Date', 
sum(item_count) AS 'Last Shipped Units' 
from order 
where is_shipped = 1 
AND date(shipped_date) = (select date(max(shipped_date)) from order); 

select 
count(distinct o.key) AS 'ACTIVE ', 
count(od.ean) AS 'Active_ Units' 
from order o 
left join order_details od on o.id = od. order_details 
Where o.is_active = 1; 

select count(distinct order_key) AS 'Total_Orders_Shipped_Yesterday', 
sum(item_count) AS 'Total_units_Shipped_yesterday' 
from order 
where datediff(curdate(), modified_date)=1 
AND is_shipped =1; 


select count(distinct liquidation_order_id) AS 'orders cancelled', 
count(ean) AS 'Units cancelled' 
from order_details 
where datediff(curdate(), modified_date)=1 
AND order_details_status_ =4; 
+0

Farklı bir sütunda bu değerlerin her birini içeren bir satır içeren bir tablo mu istiyorsunuz? – Barmar

cevap

0

Tek bir sorguda yapmanın bir yolu olabilir, ancak karmaşık olacaktır. Değerleri hesaplayan diğer sorgulara katılarak, tablodaki uygun sütunları dolduran UPDATE sorgusu dizisi yapmak daha kolaydır.

CREATE TEMPORARY TABLE tempTable (
    `Last Shipped Date` DATE, 
    `Last Shipped Units` INT, 
    Active INT, 
    Active_Units INT, 
    Total_Orders_Shipped_Yesterday INT, 
    Total_units_Shipped_yesterday INT, 
    `orders cancelled` INT, 
    `Units cancelled` INT); 

INSERT INTO tempTable (`Last Shipped Date`, `Last Shipped Units`) 
select date(max(created_date)) AS 'Last Shipped Date', 
sum(item_count) AS 'Last Shipped Units' 
from order 
where is_shipped = 1 
AND date(shipped_date) = (select date(max(shipped_date)) from order); 

UPDATE tempTable AS t 
JOIN order o 
left join order_details od on o.id = od. order_details 
SET t.Active = count(distinct o.key), t.Active_Units = count(od.ean) 
Where o.is_active = 1; 

UPDATE tempTable AS t 
JOIN order 
SET t.Total_Orders_Shipped_Yesterday = count(distinct order_key), 
    t.Total_units_Shipped_yesterday = SUM(item_count) 
where datediff(curdate(), modified_date)=1 
    AND is_shipped =1; 

UPDATE tempTable AS t 
JOIN order_details 
SET t.`orders cancelled` = count(distinct liquidation_order_id), 
    t.`Units cancelled` = COUNT(ean) 
where datediff(curdate(), modified_date)=1 
    AND order_details_status_ =4; 
+0

Çok Teşekkürler Barmar –

İlgili konular