MYSQL两个查询之间的分区顶部

时间:2017-01-30 16:52:27

标签: mysql sql

我有两个问题,第一个是按小时获得前10名销售,另一个是按小时获得最佳访问者

SELECT HOUR(fechaRegistreoDatetime) AS laHora, 
ROUND( IFNULL(SUM(tickets.precioTotal),0) , 2 ) AS CantidadVenta 
FROM tickets 
WHERE fechaRegistreoDatetime BETWEEN '2017-01-20 00:00:00' and '2017-01-30 23:59:59' 
group by HOUR(fechaRegistreoDatetime) 
ORDER BY CantidadVenta DESC LIMIT 10



SELECT HOUR(horaLocal) AS laHora, horaLocal, 
COUNT(*) AS Cantidad FROM poepleCounter  
where horaLocal BETWEEN '2017-01-20 00:00:00' and '2017-01-30 23:59:59' 
group by HOUR(horaLocal) 
ORDER BY Cantidad DESC LIMIT 10

我需要在这两个查询(CantidadVenta / Cantidad)之间产生一个新的前十名,有人可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

像这样的东西

select * from 
(
SELECT HOUR(fechaRegistreoDatetime) AS laHora, 
ROUND( IFNULL(SUM(tickets.precioTotal),0) , 2 ) AS CantidadVenta 
FROM tickets 
WHERE fechaRegistreoDatetime BETWEEN '2017-01-20 00:00:00' and '2017-01-30 23:59:59' 
group by HOUR(fechaRegistreoDatetime) 
) A
Join
(
SELECT HOUR(fechaRegistreoDatetime) AS laHora, 
ROUND( IFNULL(SUM(tickets.precioTotal),0) , 2 ) AS CantidadVenta 
FROM tickets 
WHERE fechaRegistreoDatetime BETWEEN '2017-01-20 00:00:00' and '2017-01-30 23:59:59' 
group by HOUR(fechaRegistreoDatetime) 
) B 
ON A.laHora = B.laHora
ORDER BY CantidadVenta / Cantidad DESC LIMIT 10

答案 1 :(得分:0)

这是我的最终代码,感谢Prdp!

select *, (CantidadVenta / Cantidad) as Conversion from 
(
SELECT HOUR(fechaRegistreoDatetime) AS laHora, 
ROUND( IFNULL(SUM(tickets.precioTotal),0) , 2 ) AS CantidadVenta 
FROM tickets 
WHERE fechaRegistreoDatetime BETWEEN '2017-01-20 00:00:00' and '2017-01-30 23:59:59' 
group by HOUR(fechaRegistreoDatetime) 
) A
Join
(
SELECT HOUR(horaLocal) AS laHora, horaLocal, 
COUNT(*) AS Cantidad FROM poepleCounter  
where horaLocal BETWEEN '2017-01-20 00:00:00' and '2017-01-30 23:59:59' 
group by HOUR(horaLocal) 
) B 
ON A.laHora = B.laHora
ORDER BY (CantidadVenta / Cantidad) DESC LIMIT 10