我有一个数据库,以及相同日期和相关金额的多个记录

时间:2012-07-28 02:35:13

标签: sql sql-server

我有一个数据库,并且有多个相同日期和相关金额的记录,但我只想要一条记录,而且与日期相关的总金额

No.       Date                   name   amt
----    -----------------------  -----  ----
4   2012-07-27 10:47:50.000  zerox  15  
5   2012-07-27 12:22:16.000  bag    30  
6   2012-07-28 10:47:50.000  zerox  25  
7   2012-07-28 12:22:16.000  bag    30  

我想要这个:

-----------------
date         totamt
----------   -------
2012-07-27   45
2012-07-28   55

4 个答案:

答案 0 :(得分:1)

select cast([date] as Date) as [Date], 
       sum(amt) TotAmt
from YourTable
group by cast([date] as Date) 

答案 1 :(得分:0)

假设您的“日期”列没有妨碍,以下情况应该有效。

SELECT
     CAST(date AS DATE),
     SUM(amt) AS totamt
FROM TableName
GROUP BY CAST(date AS DATE)

答案 2 :(得分:0)

create table orders(Num int,Date datetime,name varchar(100),amt float)
INSERT INTO orders
VALUES(4,'2012-07-27 10:47:50.000','zerox',15),
(5,'2012-07-27 12:22:16.000','bag',    30),  
(6,'2012-07-28 10:47:50.000','zerox',  25),    
(7,'2012-07-28 12:22:16.000','bag',    30)  

select cast(date as date) date,SUM(amt) sumAmt from orders
group by cast(date as date)

答案 3 :(得分:0)

您好试试这个新解决方案:

select convert(varchar,date,101) date,SUM(amt) sumAmt from orders
group by convert(varchar,date,101)