这是硬编码的,几乎没有灵活性。我的系统花了2分钟才能运行。但是可能会有所帮助,如果没有,抱歉。fnGenerate_Numbers是一个表函数,它返回参数范围内的整数。
做到这一点的方法。
DECLARE @Max INT, @Pens money, @Dvds money, @Pendrives money, @Mouses money, @Tvs moneySELECt @Max = 1000, @Pens = 10, @Dvds = 29, @Pendrives = 45, @Mouses = 12.5, @Tvs = 49;WITH Results AS( SELECT p.n pens, d.n dvds, pd.n pendrives, m.n mouses, t.n tvs, tot.cost FROM fnGenerate_Numbers(0, @Max/@Pens) p -- Pens CROSS JOIN fnGenerate_Numbers(0, @Max/@Dvds) d -- DVDs CROSS JOIN fnGenerate_Numbers(0, @Max/@Pendrives) pd -- Pendrives CROSS JOIN fnGenerate_Numbers(0, @Max/@Mouses) m -- Mouses CROSS JOIN fnGenerate_Numbers(0, @Max/@Tvs) t -- Tvs CROSS APPLY (SELECt p.n * @Pens + d.n * @Dvds + pd.n * + @Pendrives + m.n * @Mouses + t.n * @Tvs cost) tot WHERe tot.cost < @Max), MaxResults AS( SELECT MAX(pens) pens, dvds, pendrives, mouses, tvs FROM Results GROUP BY dvds, pendrives, mouses, tvs)SELECt mr.*, r.cost FROM MaxResults mr INNER JOIN Results r ON mr.pens = r.pens AND mr.dvds = r.dvds AND mr.pendrives = r.pendrives AND mr.mouses = r.mouses AND mr.tvs = r.tvsORDER BY cost
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)