根据条件

时间:2018-06-05 03:39:58

标签: sql sql-server tsql sql-server-2014 gaps-and-islands

我有一个表格,我需要找到满足几个条件的值,如果满足这些条件,我需要为每个列分配相同的值

 Table 
  ID       Name   Salary  rownum
  1         Jon    500     1
  1         Jim    600     2 
  1         Jack   700     3
  1         Bob    1000    4
  2         Adam    500    1
  2         Aron    600    2 
  2         James   900    3
  2         Jay     1000   4

第一个条件是我需要具有相同的ID,然后在ID中我需要将第一行与第二行进行比较,如果差异小于或等于100.我只能将Rownum 1与2和2进行比较用3等等。在确定条件是否满足之后,我需要将工资值更新为Rownum 1存在的值,直到满足条件保持名称相同的点。

预期产出

 Table 
  ID       Name   Salary  rownum
  1         Jon    500     1
  1         Jim    500     2 
  1         Jack   500     3
  1         Bob    1000    4
  2         Adam    500    1
  2         Aron    500    2 
  2         James   900    3  
  2         Jay     900    4                 

Jim的薪水为600,而Jon的薪水差不多<= 100,而Jack与700的差距是&lt; = 100,因为Jim在Jon的薪水范围内,Jack在Jim的薪水范围内这些值是连续的,所以我们需要Jon这些行的值,而Bob是独立的,因为它不属于该范围。同样的逻辑适用于ID = 2

2 个答案:

答案 0 :(得分:3)

试试这个:

如果 “WPARAM wParamT; // irrelevant code here... case WM_KEYDOWN: case WM_SYSKEYDOWN: case WM_CHAR: case WM_SYSCHAR: wParamT = lpMsg->wParam; RtlMBMessageWParamCharToWCS(lpMsg->message, &(lpMsg->wParam)); iT = NtUserTranslateAccelerator(hwnd, hAccel, lpMsg); lpMsg->wParam = wParamT; return iT; + RtlMBMessageWParamCharToWCS 的数据不为空“然后更新薪水

var readInfinitely = function(iterator, dirName,onFileContent,onError){
    var circIter = iterator;
    fname = circIter.next().value;
    fs.readFileAsync(dirName + fname,'utf8').then(data => //THIS IS 
                                                      //WHERE IT ERRORS
        {

            onFileContent(data);
            readInfinitely(circIter, dirName,onFileContent,onError)

    })
}
left join self

SQL Fiddle DEMO LINK

答案 1 :(得分:1)

您的问题并不像看起来那么简单,因为前一行可能比当前行(比如750)具有更大的值(比如说800)。 也有可能只有一个岛屿和空隙。

以下是我的解决方案。另请参阅 working demo

; with cte as 
(
    select *
    , toUpdate= case 
                    when 
                        salary - lag( salary) over( partition by Id order by rownum asc) between 0 and 100 
                    then 
                        1 
                    else 
                        0 
                end
from t
)

update t5 
set t5.salary=t4.newsalary
from t t5 
join
(
    select 
    t1.*, 
    rn=row_number () over( partition by t1.id, t1.rownum order by t3.rownum desc), 
    newsalary=t3.salary 
    from cte t1 
        outer apply
            (
                select *  
                    from cte t2 
                where t2.id=t1.id 
                    and t2.rownum<t1.rownum 
                    and t2.toUpdate=0
              ) t3
    )t4 
on t4.rn=1 and t4.toUpdate=1 and t5.id=t4.id and t5.rownum=t4.rownum

select * from t