按一天中的所有交易对列进行汇总

时间:2015-01-29 06:13:23

标签: sql sql-server-2008

我正在尝试总结我数据库中每一天的所有交易。

SELECT DISTINCT 
    SUM(Balance) OVER (partition by Date) AS account_total, 
    Date 
FROM tbl_FundData 
ORDER BY Date;

输出的问题是如果交易在不同的时间完成,它就变成了自己的唯一总和,而不是滚动到一天。我不确定如何修改查询以解决此问题。

我正在使用SQL Server 2008(我认为)

4 个答案:

答案 0 :(得分:2)

似乎将DateTime用作列数据类型,因此将其强制转换为DATE:

SELECT DISTINCT SUM(Balance) OVER (partition by CAST([Date] AS DATE)) AS account_total, CAST([Date] AS DATE)
FROM tbl_FundData 
ORDER BY CAST([Date] AS DATE);

在这种情况下,你最好将Group By用作:

SELECT SUM(Balance) AS account_total, CAST([Date] AS DATE)
FROM tbl_FundData 
GROUP BY CAST([Date] AS DATE);

答案 1 :(得分:1)

SELECT DISTINCT 
    SUM(Balance) OVER (partition by convert(varchar, Date, 103)) AS account_total,  
    convert(varchar, Date, 103) Date    
FROM tbl_FundData 
ORDER BY  convert(varchar,Date,103)

答案 2 :(得分:1)

  SELECT SUM(Balance) account_total 
  ,CAST(FLOOR(CAST(IssueDate  AS FLOAT)) AS DATETIME) IssueDate
  FROM tbl_FundData
  GROUP BY
  CAST(FLOOR(CAST(IssueDate  AS FLOAT)) AS DATETIME)
  ORDER BY 
  CAST(FLOOR(CAST(IssueDate  AS FLOAT)) AS DATETIME)

答案 3 :(得分:0)

我猜你的日期也有时间戳,这就是你总结时得到唯一价值的原因。使用此:

SELECT sum(balance)
FROM tbl_FundData 
GROUP BY convert(date ,date, 106)

106是日期的格式。但你可以使用任何东西。