Mysql: seleccione el recuento inferior, igual o superior en la tabla de precios

I have a table that stores the price of various products and would like to know how to do a query to return if my price is below, above or equal to the other records on the same day. All records containing idmonitor zero simply means that this is my price and I would compare with the other records the same day.

id | idmonitor | idproduct | price | date
1  | 0         | 5         | 42.00 | 2012-06-01
2  | 1         | 5         | 45.00 | 2012-06-01
3  | 2         | 5         | 50.00 | 2012-06-01
4  | 0         | 6         | 22.00 | 2012-06-01
5  | 1         | 6         | 24.00 | 2012-06-01
6  | 2         | 6         | 26.00 | 2012-06-01
7  | 0         | 5         | 40.00 | 2012-06-03
8  | 1         | 5         | 40.00 | 2012-06-03
9  | 2         | 5         | 40.00 | 2012-06-03
10 | 0         | 6         | 30.00 | 2012-06-03
11 | 1         | 6         | 20.00 | 2012-06-03
12 | 2         | 6         | 10.00 | 2012-06-03

I would like to query return me some like:

date       | below | equal | above
2012-06-01 | 2     | 0     | 0
2012-06-02 | 2     | 0     | 0
2012-06-03 | 0     | 1     | 1

I'm trying to make this query are already days.

preguntado el 12 de junio de 12 a las 17:06

And what is your query so far? -

Are you looking to compare the price against the average price for a specific day? -

@BrianHoover no, he wants to compare the price from idmonitor=0 to the others. -

@aF - Thanks, I missed that part of the requirements -

2 Respuestas

Something like this, although not the best query in terms of performance, might do the trick:

select aux.date,
       SUM(aux.below) as 'below',
       SUM(aux.equal) as 'equal',
       SUM(aux.above) as 'above'
from
(
   select t1.date,
          t1.idproduct,
          (select count(1) from tablename t2 where t2.date = t1.date and t2.idproduct = t2.idproduct and t2.price > t1.price and t2.idmonitor <> 0) as 'below',
          (select count(1) from tablename t3 where t3.date = t1.date and t3.idproduct = t3.idproduct and t3.price = t1.price and t3.idmonitor <> 0) as 'equal',
          (select count(1) from tablename t4 where t4.date = t1.date and t4.idproduct = t4.idproduct and t4.price < t1.price and t4.idmonitor <> 0) as 'above',
   from tablename t1
   where t1.idmonitor = 0
) aux
group by aux.date

Note that it's from memory, I might be missing something.

respondido 13 nov., 14:17

It's working. How to modify your query to display the result of day two as the result of day one since the day 02 is not inserted? Join with a calendar table? - adrianogf

I've edited this to get the base price based on idmonitor=0. I've got a version working against my local DB, but your field names are different, so I might have a typo or two.

select date, 
  sum(equal_price) as equals,
  sum(above_price) as above,
  sum(below_price) as below from (
select date,
  if (price = base_price, 1, 0) as equal_price,
  if (price > base_price, 1, 0) as above_price,
  if (price < base_price, 1, 0) as below_price
  from orders
  join (select 
    date as base_date, 
      price as base_price
     from orders where idmonitor = 0) as base_price on base_price.base_date = date) as counts
     group by date

Respondido el 12 de junio de 12 a las 17:06

@adrianogf - I don't understand the question. - Brian Hoover

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.