使用row_number

时间:2015-09-02 08:18:17

标签: sql db2 counter row-number

我有以下SQL Query部分:

SELECT car, 
       grouper, 
       yearout, 
       rn 

FROM (SELECT Row_number() OVER(partition BY grouper ORDER BY yearout) AS rn, 
             car, 
             grouper, 
             yearout 
      FROM (SELECT DISTINCT res.sn AS car, 
                            res.groupin AS groupin,
                            Year(res.yearin) AS yearin
            FROM (inner stuff)) res
      WHERE Year(res.yearin) BETWEEN '2015' AND '2030'
      ORDER BY res.groupin) temp) ALL 

WHERE ALL.rn <= (SELECT taken
                 FROM (SELECT p1.nos - p2.gone AS taken,
                              yearout,
                              group_key
                       FROM (SELECT count(sn) AS nos,
                                    group_key
                             FROM d_fm_cars
                             GROUP BY group_key) p1
                       JOIN (SELECT taken AS gone,
                                    group_key_all,
                                    yearout
                             FROM settings_keys) p2
                         ON p1.group_key = p2.group_key_all) counterin
                 WHERE counterin.yearout = ALL.yearout 
                   AND counterin.group_key_all = ALL.grouper)

采取的值类似于

5 
15

对于2015年和2016年的石斑鱼1,现在的问题是我从查询中获得的结果只运行1-15而不是它应该从1-5开始而不是从1-15开始 每年可用的汽车是

2015 3
2016 10
2017 25 

因此产量必须超过年限,并将2015年的2辆汽车投入到2015年的产量中。 当用石斑鱼和年轻人进行分区时,情况就不再如此了。但是离开它只是通过并且不会在5点重置计数器。

有关如何解决此问题的任何想法?

感谢您提供的任何帮助。

0 个答案:

没有答案