如何按日期按顺序分别添加值。假设有几个值与特定字段具有相同的日期。我想总结这些数据,并希望有一个日期的特定行。按日期提出。
答案 0 :(得分:0)
所以,让我们说你有这样的数据:
DATE VALUE
------------------------------
2017-01-01 100
2017-02-01 50
2017-01-01 75
2017-02-01 25
你想要做的是将这4行合并为2行,一个用于2017-01-01,一个用于2017-02-01,这是正确的吗?
如果是这样,你只需要在原始数据上使用SELECT和GROUP BY进行INSERT。
因此,如果我尝试INSERT到Test_Table_One并且我的数据在Origin_Table_One中,我会这样做:
INSERT INTO Test_Table_One
SELECT DATE, SUM(VALUE)
FROM Origin_Table_One
GROUP BY DATE
有意义吗?
答案 1 :(得分:0)
试试这个:
create table #temp
(
Date date,
Sum bigint
)
create table #tempVarious
(
Date date,
value bigint
)
insert into #tempVarious
values
(cast(getdate() as date), 1),
(cast(getdate() as date), 1),
(cast(getdate() as date), 3),
(cast(getdate() as date), 5),
(cast(getdate() as date), 6),
(cast(getdate() as date), 8),
(cast(getdate() as date), 3),
(cast(dateadd(day,-1,getdate()) as date), 1),
(cast(dateadd(day,-1,getdate()) as date), 3),
(cast(dateadd(day,-1,getdate()) as date), 6),
(cast(dateadd(day,-1,getdate()) as date), 2),
(cast(dateadd(day,-1,getdate()) as date), 1)
insert
into #temp
select date
, sum(value)
from #tempVarious
group by date
select *
from #temp
答案 2 :(得分:0)
看来你是SQL的新手,请阅读这两个关于 GROUP BY https://www.w3schools.com/sql/sql_groupby.asp和 ORDER BY https://www.w3schools.com/sql/sql_orderby.asp
SELECT DATE_COLUMN, SUM(VALUE_COLUM)
FROM TABLE
GROUP BY DATE_COLUMN
ORDER BY DATE_COLUMN
如果您想通过降序排序
,则为变体ORDER BY DATE_COLUMN DESC
如果您的列的类型为 DateTime ,并且您在一个日期中有不同的时间
SELECT CAST(DATE_COLUMN AS DATE) AS DC, SUM(VALUE_COLUM)
FROM TABLE
GROUP BY CAST(DATE_COLUMN AS DATE)
ORDER BY DC