我会定义一个表包含序列号,说
1要
1000和加入该表(除非你的数据库支持生成甲骨文使用类似查询这些号码
ConNECT BY):
表号
n123...
我使用Oracle尝试了以下查询(也应与TSQL一起使用):
With summed_colors As ( Select name, Sum(quantity) quantity From colors Group By name)Select name, Case When n*2-1 = quantity Then 1 Else 2 End quantityInPackageFrom summed_colorsJoin nums On ( n*2-1 <= quantity )Order By name, quantityInPackage Desc
它返回
Blue 1Red 2Red 2Red 1Yellow 2Yellow 1



