Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在sql查询表中查找减少值_Sql_Db2 - Fatal编程技术网

在sql查询表中查找减少值

在sql查询表中查找减少值,sql,db2,Sql,Db2,我有一张这样的桌子。如果次数增加,我希望增加评级列,但有时评级列会减少。我想知道这张桌子减少了多少倍。在本例中,评级列2倍减少4->2和3->1,我想在查询中输入这2个数字。而且每次增加的时间列。如何编写这种情况下的sql查询。注意:我使用的是DB2DBMS Rating times 1 20.09.2016 2 21.09.2016 3 22.09.2016 4 23.09.2016 2 24.09.2016 3 25.09.20

我有一张这样的桌子。如果次数增加,我希望增加评级列,但有时评级列会减少。我想知道这张桌子减少了多少倍。在本例中,评级列2倍减少4->2和3->1,我想在查询中输入这2个数字。而且每次增加的时间列。如何编写这种情况下的sql查询。注意:我使用的是DB2DBMS

Rating times
1      20.09.2016  
2      21.09.2016
3      22.09.2016
4      23.09.2016
2      24.09.2016
3      25.09.2016
1      26.09.2016 

谢谢,

如果没有这些函数可用,例如,对于DB2 for i,以下内容应该足够了[轻松地省略我添加的绒毛以生成一个漂亮的报告,和/或选择性地更正我包含的行数,以显示从/到,而不是显示显示显示递减转换的时间值]:

SELECT COUNT(1) AS COUNT_OF_TIMES_RATING_GOT_DECREASED
  FROM
 (
    SELECT rating, 
           times,
           rating - LEAD( rating, 1 ) OVER ( ORDER BY times ) AS diff_rating
      FROM table 
  )
WHERE diff_rating > 0;
create table ratings 
( "rating" for r dec 
, "times"  for t date
)
;
insert into  ratings values
/*    Rating times      */ 
  (   1 ,   '20.09.2016'  )
, (   2 ,   '21.09.2016'  )
, (   3 ,   '22.09.2016'  )
, (   4 ,   '23.09.2016'  )
, (   2 ,   '24.09.2016'  )
, (   3 ,   '25.09.2016'  )
, (   1 ,   '26.09.2016'  )
;
with            
  ord_ratings as
   ( select row_number() over(order by "times) as rn
          , "rating"            
     from ratings               
   )                            
select 'From'       as "From"   
     , dec(a.rn, 6) as rn_a     
     , 'to'         as "to"     
     , dec(b.rn, 6) as rn_b     
     , a."rating"   as rating_a 
     , '-->'        as "-->"    
     , b."rating"   as rating_b 
from ord_ratings a      
join ord_ratings b      
  on a.rn = ( b.rn - 1 )
 and b."rating" < a."rating"
; -- a likeness of the report from the above query:
"From"     RN_A   "to"     RN_B   RATING_A  "-->"  RATING_B
 From         4    to         5         4    -->         2 
 From         6    to         7         3    -->         1 
********  End of data  ********                            

这是样本表数据吗?给我们看一下预期的结果。你每天有一个评分吗?日期是否有差距?您好,您使用的是哪种数据库管理系统?LEAD and OVER是一个功能,我还没有见过这个功能。您能否重写此查询db2或mssql?此查询也应适用于sql server 2012及更高版本。您能否编写db2版本i Can find lead,over function?这对我有好处。@altandogan。DB2的最新版本多年来一直支持窗口函数:。