takn25 Posted April 27, 2011 Share Posted April 27, 2011 Hi, I want to know is there a simpler of achieving this. I have a table called part it has a row called HD. The HD stores a number which is not unique. I will provide a visual example below to explain further. Table HD 42 42 42 What I am after is a query something like Pile HD in stacks of 3. For example there are 3 HD with the number 42. That is 1 stack if there were 6 HD with the number 42 that would equal 2 stacks and so on. I can do some sort of maths to get the results but would like to know is there a query I can come up with to show how many stacks of 3 are there? Quote Link to comment https://forums.phpfreaks.com/topic/234851-stacking-in-3s/ Share on other sites More sharing options...
fugix Posted April 27, 2011 Share Posted April 27, 2011 you could probably make multiple queries using limits...but im not sure if that would be the most effective way..I'l try to come up with some more Quote Link to comment https://forums.phpfreaks.com/topic/234851-stacking-in-3s/#findComment-1206879 Share on other sites More sharing options...
cssfreakie Posted April 27, 2011 Share Posted April 27, 2011 I just puzzled a bit and this is what i came up with. Not sure if it is the cleanest way, but it works. Love to hear a better way though Note though this assumes you first fetch data, and let php do the puzzling <?php $myarray = array( 42,42,42,60,60,60,60,60,60,60,50,50,50,40,40,50 ); sort($myarray); // added this to sort unsorted arrays $i = 0; foreach($myarray as $key => $value){ if ($key !== 0){$num = $key - 1;}else{$num=0;} //mainly for the first array element if($i <= 2 && $myarray[$num] == $value){ // if previous value is the same as the current echo $value.'<br />'; $i++; }else{ echo '<hr>'.$value.'<br />'; $i = 1; } } ?> p.s. what wine and some cheese can come up with edit: remove some redundant code edit2: added a sorting thingy Quote Link to comment https://forums.phpfreaks.com/topic/234851-stacking-in-3s/#findComment-1207280 Share on other sites More sharing options...
Pikachu2000 Posted April 28, 2011 Share Posted April 28, 2011 SELECT ROUND((COUNT(HD) / 3), 2) AS n, HD FROM tbl_a GROUP BY HD ORDER BY HD DESC Quote Link to comment https://forums.phpfreaks.com/topic/234851-stacking-in-3s/#findComment-1207317 Share on other sites More sharing options...
cssfreakie Posted April 28, 2011 Share Posted April 28, 2011 SELECT ROUND((COUNT(HD) / 3), 2) AS n, HD FROM tbl_a GROUP BY HD ORDER BY HD DESC woO nice cheers for sharing! Quote Link to comment https://forums.phpfreaks.com/topic/234851-stacking-in-3s/#findComment-1207325 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.