Modificar consulta mysql

Hi I have written this query

SELECT cl_brands.name AS Brand,DATE_FORMAT(cl_doctor_call.date_entered,'%b%y')AS MonthYear,

SUM(FIND_IN_SET(CONCAT('^',cl_brands.id,'^'),cl_doctor_call_cstm.brand_discussed_c))AS No_Times_Brand_Discussed
FROM cl_doctor_call LEFT JOIN cl_doctor_call_cstm ON
cl_doctor_call.id=cl_doctor_call_cstm.id_c 
LEFT JOIN cl_brands ON
CONCAT('^',cl_brands.id,'^') LIKE CONCAT(cl_doctor_call_cstm.brand_discussed_c, '%' )
WHERE (cl_doctor_call.date_entered)
BETWEEN CURDATE()-INTERVAL 3 MONTH AND CURDATE()
GROUP BY cl_brands.name,MonthYear
ORDER BY No_Times_Brand_Discussed DESC limit 1

This query gives us highest brand discussed with brand name and monthyear.

P.ej:

Brand   Monthyear    No_Times_Brand_Discussed
   x       Nov11              5

Now i want show the brand discussed details of ''same brand'' for last 2 months (that means my query should return me results for same brand details for more last 2 months) so my result would be like that.

 Brand   Monthyear    No_Times_Brand_Discussed

  x       Nov11              5
  x       Oct11              0
  x       Sep11              1

Tell me how can I modify my above query so that it will give me this result.

preguntado el 31 de enero de 12 a las 08:01

1 Respuestas

If I'm reading this right, essentially, you just need to get the top 3 results ordered by date for the brand that is the most discussed. So, you just have to query your original tables the same way, but include an "in" clause that limits you to the brand that is most discussed. I'd use an ID instead of a Name field so you know it's unique. I'm assuming that field name is "brand_id" but it might be something different in your table.

select  cl_brands.name AS Brand,DATE_FORMAT(cl_doctor_call.date_entered,'%b%y')AS MonthYear,
SUM(FIND_IN_SET(CONCAT('^',cl_brands.id,'^'),cl_doctor_call_cstm.brand_discussed_c))AS No_Times_Brand_Discussed
from cl_doctor_call LEFT JOIN cl_doctor_call_cstm ON
cl_doctor_call.id=cl_doctor_call_cstm.id_c 
LEFT JOIN cl_brands ON
CONCAT('^',cl_brands.id,'^') LIKE CONCAT(cl_doctor_call_cstm.brand_discussed_c, '%' )
where brand_id in 
(
   SELECT c1.brand_id
   FROM cl_doctor_call LEFT JOIN cl_doctor_call_cstm ON
   cl_doctor_call.id=cl_doctor_call_cstm.id_c 
   LEFT JOIN cl_brands ON
   CONCAT('^',cl_brands.id,'^') LIKE CONCAT(cl_doctor_call_cstm.brand_discussed_c, '%' )
   WHERE (cl_doctor_call.date_entered)
   BETWEEN CURDATE()-INTERVAL 3 MONTH AND CURDATE()
   GROUP BY cl_brands.name,MonthY`enter code here`ear
) 
order by date_entered
limit 3

Respondido el 31 de enero de 12 a las 17:01

yes your query logic is correct but I am getting wrong result now. If we execute only where clause we can get details that which brand is discussed highest no.of times. But final result conflicts with this result....why is it so? - Java

Could you give an example of exactly how the two results are different? - Jody

where clause will return us all brands_id that are discussed, which is right but might be becauz of wrong group by clause we are getting not exact result. (as i know that which brand is discussed highest no.of times by looking through database records) - Java

I understand they're different.... but specifically how. can you provide the 2 record sets. - Jody

so ...can we achieve our result by creating any stored procedure? - Java

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