Solarpitch Posted January 7, 2009 Share Posted January 7, 2009 Hey, I have a table that has a list of items that were sold on a POS system. Each item has an associated receipt number so the table would look like.. ID PRODUCT PRICE RECEIPT NUMBER --------------------------------------------- 1 Budwiser 4.80 001 2 Carlsberg 4.80 001 3 Coke 2.20 001 4 Heineken 4.80 002 5 Coke 2.20 002 You can see that there are 3 items in the first sale (receipt 001) and two items in the second sale (receipt 002) I'm basically looking to count the number sales in the table, not the rows. There are 5 rows but only 2 sales. Wouls I use a group by clause to achieve this? I think the query would need to group all the reciept numbers that are the same and count them as 1 sale? Quote Link to comment https://forums.phpfreaks.com/topic/139845-solved-question-regarding-couting-results-in-a-query/ Share on other sites More sharing options...
RussellReal Posted January 7, 2009 Share Posted January 7, 2009 SELECT COUNT(`receipt_number`) FROM `POS_system` GROUP BY `receipt_number` Quote Link to comment https://forums.phpfreaks.com/topic/139845-solved-question-regarding-couting-results-in-a-query/#findComment-731585 Share on other sites More sharing options...
Solarpitch Posted January 7, 2009 Author Share Posted January 7, 2009 I tried that already but that just counts the all the rows. In my example it would return 5 and not 2 EDIT: Even when I remove the group by clause I still get the same result. Quote Link to comment https://forums.phpfreaks.com/topic/139845-solved-question-regarding-couting-results-in-a-query/#findComment-731589 Share on other sites More sharing options...
gevans Posted January 7, 2009 Share Posted January 7, 2009 That select statement should work, can you post your code? Quote Link to comment https://forums.phpfreaks.com/topic/139845-solved-question-regarding-couting-results-in-a-query/#findComment-731593 Share on other sites More sharing options...
Solarpitch Posted January 7, 2009 Author Share Posted January 7, 2009 This is the function here. (I'm using the Codeigniter Framework so the rest of the code just prints to screen) It's like it doesnt even take the group by into consideration and just returns the total rows. <?php function total_sales($date) { $query = $this->client->query('SELECT COUNT(ticketnumber) as total FROM sales WHERE date = '.$date.' GROUP BY "ticketnumber"'); $row = $query->row(); return $row->total; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/139845-solved-question-regarding-couting-results-in-a-query/#findComment-731600 Share on other sites More sharing options...
RussellReal Posted January 7, 2009 Share Posted January 7, 2009 GROUP BY "ticketnumber" IS WRONG below is correct GROUP BY `ticketnumber` Quote Link to comment https://forums.phpfreaks.com/topic/139845-solved-question-regarding-couting-results-in-a-query/#findComment-731604 Share on other sites More sharing options...
Solarpitch Posted January 7, 2009 Author Share Posted January 7, 2009 Cool.. but not sure what happening now. There are 52 sales in the table but its now only returning a count of 1. I'll see if I can figure it out. Quote Link to comment https://forums.phpfreaks.com/topic/139845-solved-question-regarding-couting-results-in-a-query/#findComment-731613 Share on other sites More sharing options...
Solarpitch Posted January 7, 2009 Author Share Posted January 7, 2009 Right I see what was happening... it's returning it like 3 2 1 4 etc.. Is there something I can add into that query to get the sum of them then? Quote Link to comment https://forums.phpfreaks.com/topic/139845-solved-question-regarding-couting-results-in-a-query/#findComment-731630 Share on other sites More sharing options...
RussellReal Posted January 7, 2009 Share Posted January 7, 2009 "SELECT COUNT(ticketnumber) as total, SUM(price) FROM sales WHERE date = '.$date.' GROUP BY `ticketnumber` Quote Link to comment https://forums.phpfreaks.com/topic/139845-solved-question-regarding-couting-results-in-a-query/#findComment-731633 Share on other sites More sharing options...
Solarpitch Posted January 7, 2009 Author Share Posted January 7, 2009 duh.. should have know that. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/139845-solved-question-regarding-couting-results-in-a-query/#findComment-731639 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.