0

I am trying to modify MySQL output results.

Select number_purchases, COUNT(number_purchases) as user_count
FROM user_order
GROUP BY number_purchases
ORDER BY number_purchases ASC

so I get the number of purchases in the first column and the number of users that made that number of purchases in the second column.

I would like to modify the result a bit. I need a function around those lines:

If npb = 1, 2, 3... to 10 does not exist, add line with the missing npb and a corresponding user_count = 0

kmatyaszek
  • 19,016
  • 9
  • 60
  • 65
user1783504
  • 331
  • 4
  • 7
  • 14
  • Why do you want to do this in SQL? Surely your application code can handle this eventuality (i.e. if record is missing from results, it is known to have 0 users)? – eggyal Nov 05 '12 at 17:49
  • Hey. I am not trying to do it in mysql. I am actually trying to do it in vba. But, If I get mysql query to change then My VBA code would work. I am still a beginner. I asked a similar question for vba but did not get any feedback so i am trying this way – user1783504 Nov 05 '12 at 17:54

1 Answers1

0

one way to do it would be to create a view with all the numbers and left join and then use group by. but according to SQL SELECT to get the first N positive integers , you will have to create a table though.

Community
  • 1
  • 1
vignesha
  • 41
  • 2