0

更新:我想做的一个例子:

现有表:

1st Table name: DistinctAcctDay;
Column name: ID int, AccountingDate datetime;
Values: (1, 2017/05/01);
     (2, 2017/08/01);
     (3, 2017/09/01);


2nd Table name: TransferOut;
Column name: AccountingDate datetime; Amount decimal;
Values: (2017/01/01, 10);
(2017/02/01, 13);
(2017/06/25, 15);
(2017/08/03, 18);
(2017/08/08, 30);

我想让 Cosmos 脚本返回低于 3(或 n,取决于第一个表中存在多少行)的输出,该输出是当天之前所有历史数据的总和:

1)第一表2017/05/01之前日期的第二表总和:数字应为23;

2)第一表2017/08/01之前日期的第二表总和:数字应为10+13+15 =38;

3)第一表2017/09/01之前日期的第二表总和:数字应为10+13+15+18+30=86;

脚本应该用 Cosmos 编写。

感谢大家的帮助。

///////////////////////

我的想法:

我正在考虑循环第一行并每次选择一行(一个日期),并使用该日期作为从第二个表中获取数据的标准。

如果用 sql 编写,它可能如下所示。

Declare @Counter int,@MaxRowLimit int; 
Set @Counter =1;
Set @MaxRowLimit = (SELECT COUNT(*) FROM DistinctAcctDay);
WHILE @Counter <= @MaxRowLimit
BEGIN
  PickAcctDay=
  SELECT AccountingDay
  FROM DistinctAcctDayId
  WHERE ID =@Counter;

  TransferOut =
     SELECT SUM(Amount) FROM TransferOut
     WHERE AccountingDate <= PickAcctDay;   
   SET @Counter =@Counter +1
END

但是它需要用 USQL 编写,我对 USQL 和 C# 都不熟悉。我像下面这样写了一些东西(这是不正确的),我被困住了......

int counter=1; int MaxRowLimit;
for (counter = 1; counter <= MaxRowLimit; counter++)
{
  DateTime PickAcctDay =
  SELECT AccountingDay
  FROM DistinctAcctDayId
  WHERE ID ==@Counter;

  TransferOut =
     SELECT SUM(Amount) FROM TransferOut
     WHERE AccountingDate <= PickAcctDay;
}
4

1 回答 1

1
@DistinctAcctDay = 
SELECT * FROM 
    ( VALUES
    (1, new DateTime(2017,05,01)),
    (2, new DateTime(2017,08,01)),
    (3, new DateTime(2017,09,01))
    ) AS T(Id, AccountingDate);


@TransferOut = 
SELECT * FROM 
    ( VALUES
    (new DateTime(2017,01,01), 10),
    (new DateTime(2017,02,01), 13),
    (new DateTime(2017,06,25), 15),
    (new DateTime(2017,08,03), 18),
    (new DateTime(2017,08,08), 30)
    ) AS T(AccountingDate, Amount);

@result = 
       SELECT d.Id, SUM(Amount) AS sumAmount
       FROM  @DistinctAcctDay AS d
       CROSS JOIN @TransferOut AS t
       WHERE d.AccountingDate >= t.AccountingDate 
      // WHERE d.AccountingDate BETWEEN t.AccountingDate AND d.AccountingDate
       GROUP BY d.Id;
于 2017-12-20T18:33:46.923 回答