I’m currently developing a program that will generate reports based upon lead data. My issue is that I’m running 3 queries for something that I would like to only have to run one query for. For instance, I want to gather data for leads generated in the past day
submission_date > (NOW() - INTERVAL 1 DAY)
and I would like to find out how many total leads there were, and how many sold leads there were in that timeframe. (sold=1 / sold=0). The issue comes with the fact that this query is currently being done with 2 queries, one with WHEREsold= 1 and one with WHEREsold= 0. This is all well and good, but when I want to generate this data for the past day,week,month,year,and all time I will have to run 10 queries to obtain this data. I feel like there HAS to be a more efficient way of doing this. I know I can create a mySQL function for this, but I don’t see how this could solve the problem.
Thanks!!
I’m currently developing a program that will generate reports based upon lead data. My
Share
One way to do is to exploit the aggregate functions (usually
SUMandCOUNThelp you the most in this situation) along with MySQL’sIF()function.For example, you could use a query such as:
The condition (e.g.
sold = 1) could be as complex as you want by usingANDandOR.Disclamer: code wasn’t tested, this was just provided as an example that should work with minor modifications.