SQL Server查找行之间的差异

时间:2016-08-24 22:05:12

标签: sql-server

我仍然是SQL Server的新手,我需要这个问题的帮助。

我需要产生这样的输出:

@Override
public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
    super.onInitializeAccessibilityNodeInfo(host, info);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {

        switch (host.getId()) {
            case R.id.tv_bookingID:
                if (((TextView) host).getText().length() > 1) {
                    sb.delete(0, sb.length());
                    for (char c : ((TextView) host).getText().toString().toCharArray()) {
                        sb.append(c).append(" ");
                    }
                    //change text for talkback
                    info.setText(null);
                    info.setContentDescription(sb.toString().trim());

                }
                break;
        }//switch
    }//if
}

我目前拥有的表格中包含以下数据:

PNRFNAME | PNRFNAME | Salary_Difference
---------+----------+------------------
Jason    | Vicki    |     483778.43
Jason    | Tiff     |  -34540999.72
Vicki    | Tiff     |  -35024778.15

到目前为止我的代码:

PresenterID | PnrLName         | PnrFName | age | years | Salary_Yearly
------------+------------------+----------+-----+-------+--------------
EMP_01      | NEEDELL          | Tiff     |  61 |   10  |  37451000.70
EMP_02      | BUTLER-HENDERSON | Vicki    |  41 |   10  |   2426222.55
EMP_03      | PLATO            | Jason    |  45 |    8  |   2910000.98

希望这是有道理的!我试着让我的问题清楚

2 个答案:

答案 0 :(得分:0)

试试这个:

CREATE TABLE #PRESENTERS(PRESENTERID VARCHAR(20), PNRLNAME VARCHAR(25), PNRFNAME VARCHAR(25), AGE INT, YEAR INT , SALARY_YEARLY NUMERIC(11,2))

INSERT INTO #PRESENTERS

SELECT 'EMP_01','NEEDELL','TIFF',61,10,37451000.7            UNION ALL
SELECT 'EMP_02','BUTLER-HENDERSON','VICKI',41,10,2426222.55  UNION ALL
SELECT 'EMP_03','PLATO','JASON',45,8,2910000.98

;WITH A
AS
(
SELECT ROW_NUMBER() OVER (ORDER BY PRESENTERID DESC) RN, *
FROM #PRESENTERS
)
SELECT A.PNRFNAME , A2.PNRFNAME, A.SALARY_YEARLY - A2.SALARY_YEARLY  SALARY_DIFF
FROM A
INNER JOIN A A2 ON A.RN < A2.RN

答案 1 :(得分:0)

您可以使用线索或延迟来获取此信息:使用以下查询,

;with cte as (
select pnrfname, Salary_yearly, lag(salary_yearly) over (order by presenterid) lag_sal, 
lag(pnrfname) over (order by presenterid) as lag_fname from #PRESENTERS
) select pnrfname, lag_fname, salary_yearly-lag_sal from cte

这是否解决了您的要求?