Code:
$ cat foo.c
#include <stdio.h>
#include <stdlib.h>
struct {
int customer;
int order;
} table[] = {
{ 102140, 3349553 },
{ 102140, 3309756 },
{ 102140, 3309756 },
{ 102160, 3330395 },
{ 102160, 3329182 },
{ 102160, 3323283 },
{ 102160, 3317115 },
{ 102160, 3305923 },
{ 102510, 3334064 },
{ 102510, 3334064 },
{ 102750, 3342812 },
{ 102750, 3330579 },
{ 102750, 3330570 },
{ 102750, 3320982 },
{ 103137, 3307385 },
};
int main()
{
const int maxOrdersPerCustomer = 3;
int currentCustomer = -1, currentOrder = -1, orderCount = 0;
for ( int i = 0 ; i < 15 ; i++ ) {
if ( table[i].customer != currentCustomer ) {
currentCustomer = table[i].customer;
currentOrder = table[i].order;
orderCount = 1;
printf("C=%d, O=%d\n", currentCustomer, currentOrder);
} else {
if ( table[i].order != currentOrder ) {
currentOrder = table[i].order;
orderCount++;
if ( orderCount <= maxOrdersPerCustomer ) {
printf("C=%d, O=%d\n", currentCustomer, currentOrder);
}
}
}
}
return 0;
}
$ ./a.out
C=102140, O=3349553
C=102140, O=3309756
C=102160, O=3330395
C=102160, O=3329182
C=102160, O=3323283
C=102510, O=3334064
C=102750, O=3342812
C=102750, O=3330579
C=102750, O=3330570
C=103137, O=3307385
The outer for loop would just be whatever your "get next record" database call is.