0

我想在桌子上的每一天的select总和在和之间p_amountPaymentsp_date@startdate@enddate

CREATE TABLE [dbo].[tbl_Payments](
    [p_id] [bigint] IDENTITY(100000,1) NOT NULL,
    [p_amount] [int] NOT NULL,
    [p_status] [tinyint] NOT NULL,
    [p_date] [datetime] NOT NULL,
    [p_creditor] [int] NOT NULL,
    [p_debtor] [int] NOT NULL,
    [p_type] [int] NOT NULL,
 CONSTRAINT [PK_tbl_Payments] PRIMARY KEY CLUSTERED )

我想像关注

sum                     day
---------------------------------
30                 2013-07-01
40                 2013-07-02
...                 ....
10                 2013-07-19
4

2 回答 2

2

我不熟悉 sql-server 查询,但您的问题的查询是这样的

SELECT    sum(p_amount) as sum, CONVERT(date, getdate()) as day
FROM      [YourTable]
WHERE     [YourDate] BETWEEN [day1] AND [day2] group by day
于 2013-07-22T03:21:02.797 回答
1

尝试

SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, p_date)) [date],
       SUM(p_amount) [sum]
  FROM tbl_Payments
 WHERE DATEADD(dd, 0, DATEDIFF(dd, 0, p_date)) BETWEEN '20130701' AND '20130731'
 GROUP BY DATEADD(dd, 0, DATEDIFF(dd, 0, p_date))

样本输出:

| 日期 | 总和 |
-------------------------------------
| 2013 年 7 月 1 日 00:00:00+0000 | 22 |
| 2013 年 7 月 2 日 00:00:00+0000 | 30 |
| 2013 年 7 月 3 日 00:00:00+0000 | 35 |

这是SQLFiddle演示

于 2013-07-22T03:20:33.727 回答