我正在研究一个项目而且我创建了这个sql查询但是如果我选择一个更大的日期范围似乎需要太长时间,任何想法如何让它更快,我尝试使用union all
并且它更快,但数据不一样,需要帮助代码:
SELECT SUM(ROUND(total, 2)) as Enero from (
SELECT
FORMAT (
COALESCE(
CONCAT(ROUND(pro.precio / 1.14 , 2) + ROUND(p.recargo / 1.14 , 2) + (SELECT SUM(ROUND(precio / 1.14 , 2)) AS precio FROM acompanante_orden WHERE id_orden=p.id_orden) - p.descuento),
CONCAT(ROUND(pro.precio / 1.14 , 2) + ROUND(p.recargo / 1.14 , 2) - p.descuento)
)+ COALESCE(
ROUND(CONCAT(ROUND(pro.precio / 1.14 , 2) + (SELECT SUM(ROUND(precio / 1.14 , 2)) AS precio FROM acompanante_orden WHERE id_orden=p.id_orden) + ROUND(p.recargo / 1.14 , 2) - p.descuento) * 0.14, 2),
ROUND(CONCAT(ROUND(pro.precio / 1.14 , 2) + ROUND(p.recargo / 1.14 , 2) - p.descuento) * 0.14, 2)
), 2) as total
FROM orden p
LEFT JOIN producto_orden AS pro ON p.id_orden = pro.id_orden
LEFT JOIN acompanante_orden AS pa ON p.id_orden = pa.id_orden
WHERE p.actual_date BETWEEN '2017-04-01' AND '2017-04-01' and p.status=4
) sum_1
UNION ALL IDEA
select ROUND(( sum(precio)) * 1.14,2) as total from (
SELECT a.id_orden, ROUND(b.precio / 1.14 , 2) + ROUND(a.recargo / 1.14 , 2) - a.descuento as precio FROM orden a, producto_orden b WHERE a.id_orden=b.id_orden and a.actual_date BETWEEN '2017-04-01' AND '2017-04-01' and a.status=4 union all
SELECT c.id_orden, ROUND(d.precio / 1.14 , 2) as precio FROM orden c, acompanante_orden d WHERE c.id_orden=d.id_orden and c.actual_date BETWEEN '2017-04-01' AND '2017-04-01' and c.status=4
) sum_1
更新
这是我的表架构:http://sqlfiddle.com/#!9/4fe12/2
我需要什么:
我需要获得每月卖出的总数,但是数据库上的值与TAXES一样,所以在我得到总数之前我需要做一些数学运算:
数据库信息
PRODUCT= $50.00
ADDONS= $8.00
SHIPPING= $6.00
DISCOUNT= $2.00
结果我需要:
PRODUCT= $50.00 / 1.14 = $43.86
ADDONS= $8.00 / 1.14 = $7.02
SHIPPING= $6.00 / 1.14 =$5.26
SUBTOTAL= $56.14 - DISCOUNT($2)= $54.14
SUBTOTAL= $54.14 * 1.14 = $61.72
答案 0 :(得分:1)
第一个子查询基本上是:
SELECT . . .
FROM orden p LEFT JOIN
producto_orden pro
ON p.id_orden = pro.id_orden LEFT JOIN
acompanante_orden pa
ON p.id_orden = pa.id_orden
WHERE p.actual_date BETWEEN '2017-04-01' AND '2017-04-01' and
p.status = 4
对于此查询,您需要orden(status, actual_date, id_id_orden)
上的索引,producto_orden(id_orden), and
acompanante_orden(id_orden)`。
我不确定查询的其余部分是什么。