0

我的 SQL Server 查询有问题,希望您能帮助我

主选必须输出日期和重量总和

如何将一个选择与另一个选择相加?数据取自一张表Tbreport,条件是 - 将具体日期和前一个日期的权重相加(具体日期减去 1 天)

例如:

CONCRETE DATE  WEIGHT 
  Jan 1         100                 
  Jan 2         150         
  Jan 3         210         

PREVIOUS DATE   WEIGHT 
  Jan 1          100 
  Jan 2          250 (Jan 1 + Jan 2)
  Jan 3          460 (Jan 1 + Jan 2 + Jan 3) 

在真实表格中,我有几秒钟的格式。例如:1358892000 秒是 2013 年 1 月 23 日 0:00:00,1358978400 是 2013 年 1 月 23 日 23:59:00。而且每次都是自己的重量

询问:

SELECT CONVERT(varchar, DATEADD(s, TBreport.date, 25568), 102)  AS DATE,
    SUM(TBreport.weight) 
+ 
(
SELECT SUM(TBreport.weight) AS WEIGHT 
FROM         TBreport INNER JOIN TBway ON TBreport.id_way = TBway.id 
WHERE     (SUBSTRING(TBway.name, 5, 8) LIKE 'to warehouse')  
AND ... ???
GROUP BY CONVERT(varchar, DATEADD(s, TBreport.date, 25568), 102)  
)  
FROM         TBreport INNER JOIN TBway ON TBreport.id_way = TBway.id 
WHERE     (SUBSTRING(TBway.name, 5, 8) LIKE 'to warehouse')  
GROUP BY CONVERT(varchar, DATEADD(s, TBreport.date, 25568), 102) 
4

1 回答 1

1

我假设date是独一无二的TBreport

SELECT this.date, SUM(upToThis.weight)
FROM TBreport this
INNER JOIN TBreport upToThis ON upToThis.date <= this.date
GROUP BY this.date
于 2013-02-25T09:27:14.260 回答