sql server - Priority queue in SQL -
i implementing queueing system multiple priorities. query can return x rows at least y rows of each priority. for example: lets queue has 3 priorities (high, medium , low), , 3, 2 , 1 rows each priority respectively. if table looked this: ----------------- | id | priority | ----------------- | 1 | high | | 2 | high | | 3 | high | | 4 | high | | 5 | medium | | 6 | medium | | 7 | low | ----------------- three simple queries unioned return (1, 2, 3, 5, 6, 7). select top 3 id tbl priority = 'high' union select top 2 id tbl priority = 'medium' union select top 1 id tbl priority = 'low' however problem occurs when table doesn't contain enough of particular priority: ----------------- | id | priority | ----------------- | 1 | high | | 2 | high | | 3 | high | | 4 | high | | 5 | medium | | 6 | low | | 7 | low | ----------------- i have return (1, 2, 3, 4, 5, 6). using highest ...