1

我有两个数据库(MS SQL Server);一个带有当前的一组报告表,另一个带有上周的一组报告表。每周我都想将每个指标中的所有指标(尽管每周的行数可能会有所不同)与前一周的指标进行比较,并确保我的 QTD 和 YTD 数字都在增加。每周的行顺序可能不同,所以无论我做什么都必须考虑到这一点。

另一个考虑因素是我有大约 50 个这样的表要比较,因此手动执行每个表都不是一个选项。拥有某种错误日志也很棒,例如当本周年初至今的值不高于上周时。

本周

+-------+------------+--------+
| Item  | TimePeriod | Number |
+-------+------------+--------+
| Pigs  | QTD        |     28 |
| Pigs  | YTD        |     86 |
| Cows  | QTD        |      7 |
| Cows  | YTD        |      9 |
| Bears | QTD        |     67 |
| Bears | YTD        |    132 |
+-------+------------+--------+

上个礼拜

+-------+------------+--------+
| Item  | TimePeriod | Number |
+-------+------------+--------+
| Bears | QTD        |     55 |
| Bears | YTD        |    120 |
| Pigs  | QTD        |     22 |
| Pigs  | YTD        |     80 |
| Cows  | QTD        |      5 |
| Cows  | YTD        |     10 |
+-------+------------+--------+

错误

错误:奶牛 YTD 当前 Week-9 Prev Week-10

4

2 回答 2

1

也许这样的事情会做:

SELECT 'Error ' + CW.TimePeriod + ' Current week:' + CAST(CW.Number AS VARCHAR) + ' Prev week:' + CAST(PW.Number AS VARCHAR)
FROM [Current Week DB].Table CW 
INNER JOIN [Previous DB].Table PW ON 
  CW.ITEM=PW.ITEM 
  AND CW.TIMEPERIOD=PW.TIMEPERIOD 
  AND CW.Number=PW.Number

加入所有列将给出表之间未更改的行。表的标识符显然需要更改。

于 2013-06-26T15:15:18.750 回答
0

您的问题中没有任何内容可以证明使用多个数据库多个表是合理的。

相反,请考虑使用这样的表格。

ITEM     TIMEPERIOD  WEEKNUM     NUMBER
Bears    QTD         2013-26     67
Bears    QTD         2013-25     55
Bears    YTD         2013-26     132
Bears    YTD         2013-25     120
Cows     QTD         2013-26     7
Cows     QTD         2013-25     5
Cows     YTD         2013-26     9
Cows     YTD         2013-25     10
Pigs     QTD         2013-26     28
Pigs     QTD         2013-25     22
Pigs     YTD         2013-26     86
Pigs     YTD         2013-25     80

如果您有 SQL Server 2012,那么基本查询相对简单明了。

select 
    item, timeperiod, weeknum, number,
    number - lead (number) 
             over (partition by item, timeperiod 
                       order by item, timeperiod, weeknum desc) as change
from table1;

ITEM    TIMEPERIOD   WEEKNUM   NUMBER   CHANGE
--
Bears   QTD          2013-26   67       12
Bears   QTD          2013-25   55       (null)
Bears   YTD          2013-26   132      12
Bears   YTD          2013-25   120      (null)
Cows    QTD          2013-26   7        2
Cows    QTD          2013-25   5        (null)
Cows    YTD          2013-26   9       -1
Cows    YTD          2013-25   10       (null)
Pigs    QTD          2013-26   28       6
Pigs    QTD          2013-25   22       (null)
Pigs    YTD          2013-26   86       6
Pigs    YTD          2013-25   80       (null)

如果您只想要那些有负面变化的行,请将上一个查询用作派生表,并添加 WHERE 子句

select *
from 
  (select 
      item, timeperiod, weeknum, number,
      number - lead (number) 
               over (partition by item, timeperiod 
                         order by item, timeperiod, weeknum desc) as change
  from table1) t
where change < 0
  and timeperiod = 'YTD';

ITEM    TIMEPERIOD   WEEKNUM   NUMBER   CHANGE
--
Cows    YTD          2013-26   9       -1
于 2013-06-26T15:31:45.000 回答