I have a table (table 1) where the first column is the key and the second column contains elements of different types. In table 1, there's three types (type A, B, C) but the actual database have many more types. 

```
Table.1. A minimal example. 
_________________  
|    |           |
|_KEY| attribute |
|____|___________|
|k1  |    A      |
|k2  |    A      |
|k3  |    B      |
|k4  |    C      | 
|k5  |    C      |
|____|___________|

```

From table 1; I am interested in retrieving only a limited amount of elements from each type. The limited amount of elements of a given type is provided by table 2, in which the elements type is the key of the table (\_element). 

To clarify; The limited amount of elements of type A to obtain from table 1. in this minimal example is 1. Likewise, for type B it is 2 and for type C it is 1. 

```
Table 2. Limits of item to obtain for each type in table 1.
____________________
| _Element | Limit |
|----------|-------|
| A        |   1   | 
| B        |   2   |
| C        |   1   | 
|__________|_______|

```

Finally, the elements should be retrieved from table 1 from top to bottom. 

Thanks for any help and/or pointers / gus.

**P.S.**

For the above minimal example, the expected output would be

```
___________________ 
| Key| Attribute  |
|____|____________|
| k1 | A          |
| k3 | B          |
| K4 | C          |
|____|____________|

```

Since there only exists 1 C attribute for this particular minimal example. Note that if there would have existed, say 5 elements of type C then the follow table would have been obtained instead (since the limited amount of C elements is 2)

```
___________________ 
| Key| Attribute  |
|____|____________|
| k1 | A          |
| k3 | B          |
| K4 | C          |
|_k5 | C          |
|____|____________|

```