sql查询双全连接和嵌套连接

nom7f22z  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(218)

我一直在努力创建一个sql查询,它将在一个查询中给出所有3个查询的结果。我想我需要使用完全连接。这3个查询都按小时\键和存储\ id分组,并按小时\键排序。



这3个查询如下:

select SUM(qty_invoiced) as diag, DATE_FORMAT(a.created_at, '%Y-%m-%d-%h') as hour_key, b.store_id
from sales_flat_order_item a inner join
    sales_flat_order b
    on (a.order_id = b.entity_id)
where a.created_at BETWEEN (NOW() - INTERVAL 7 DAY) AND NOW() AND
     a.parent_item_id IS NULL AND
     a.sku LIKE 'D-%' AND b.status in ('processing', 'complete', 'printed', 'ready_to_print', 'ready_to_ship')
GROUP BY DATE_FORMAT(a.created_at, '%Y-%m-%d-%h'), b.store_id 
ORDER BY hour_key DESC
select SUM(grand_total) as grand_total, store_id , DATE_FORMAT(created_at, '%Y-%m-%d-%h') as hour_key, SUM(shipping_amount) as shipping
from sales_flat_order
where created_at BETWEEN (NOW() - INTERVAL 7 DAY) AND NOW() AND status in ('processing', 'complete', 'printed', 'ready_to_print', 'ready_to_ship')
GROUP BY DATE_FORMAT(created_at, '%Y-%m-%d-%h'), store_id
ORDER BY hour_key DESC
SELECT COUNT(a.entity_id) as totalPaniers, a.store_id, DATE_FORMAT(a.created_at, '%Y-%m-%d-%h') as hour_key
FROM sales_flat_quote a
WHERE a.created_at BETWEEN (NOW() - INTERVAL 7 DAY) AND NOW() 
GROUP BY DATE_FORMAT(a.created_at, '%Y-%m-%d-%h'), a.store_id 
ORDER BY hour_key DESC
hgqdbh6s

hgqdbh6s1#

我们不知道你的table结构是什么,所以我最好的猜测是

SELECT 
  DATE_FORMAT(a.created_at, '%Y-%m-%d-%h') as hour_key, 
  COUNT(entity_id) as totalPaniers, 
  store_id, 
  SUM(grand_total) as grand_total, 
  SUM(shipping_amount) as shipping,
  SUM(qty_invoiced) as diag
FROM
  sales_flat_quote a
  [LEFT] JOIN sales_flat_order b ON a.store_id=b.store_id
  INNER JOIN sales_flat_order_item c ON c.order_id=b.entity_id
WHERE
  a.created_at BETWEEN (NOW() - INTERVAL 7 DAY) AND NOW() 
  AND b.created_at BETWEEN (NOW() - INTERVAL 7 DAY) AND NOW()
  AND c.created_at BETWEEN (NOW() - INTERVAL 7 DAY) AND NOW()
  AND b.`status` IN ('processing', 'complete', 'printed', 'ready_to_print', 'ready_to_ship')
  AND c.parent_item_id IS NULL 
  AND c.sku LIKE 'D-%' 
GROUP BY
  b.created_at, store_id
ORDER BY hour_key DESC

相关问题