SQL Server按组求和
例如,在这个表中,我需要使用distinct对按id和日期分组的数据进行求和SQL Server按组求和,sql,sql-server,Sql,Sql Server,例如,在这个表中,我需要使用distinct对按id和日期分组的数据进行求和 id amt date 1 100 2018/06/01 1 120 2018/06/02 1 100 2018/06/03 1 100 2018/06/03 1 100 2018/06/03 2 100 2018
id amt date
1 100 2018/06/01
1 120 2018/06/02
1 100 2018/06/03
1 100 2018/06/03
1 100 2018/06/03
2 100 2018/06/01
2 100 2018/06/01
2 100 2018/06/01
2 130 2018/06/02
2 130 2018/06/02
2 130 2018/06/02
2 130 2018/06/02
2 100 2018/06/03
首先我试过
SELECT SUM(DISTINCT amt) GROUP BY id
但是结果是错误的,它正在删除重复的,例如id 1,而不是320,它只会导致220,因为它删除了重复的amt 100
所以我试过了
SELECT SUM(DISTINCT amt) GROUP BY id, date
但我不能总结
编辑:对不起,我忘了说结果应该是
id amt
1 320
2 330
试过下面的
SELECT id,SUM(DISTINCT amt) as amt,date #tmp from [yourTableName] GROUP BY id, date
select id,amt from #tmp
试过下面的
SELECT id,SUM(DISTINCT amt) as amt,date #tmp from [yourTableName] GROUP BY id, date
select id,amt from #tmp
具有长版本,但易于理解的查询。下面使用CTE的查询应该对您有所帮助
with records as
(select distinct id, date, amt from table_name)
select sum (amt), id
from records
group by
id;
with cte
as
(
select 1 id, 100 amt,cast('2018/06/01' as date) date
union all
select 1 id, 120 amt,cast('2018/06/02' as date) date
union all
select 1 id, 100 amt,cast('2018/06/03' as date) date
union all
select 1 id, 100 amt,cast('2018/06/03' as date) date
union all
select 1 id, 100 amt,cast('2018/06/03' as date) date
union all
select 2 id, 100 amt,cast('2018/06/01' as date) date
union all
select 2 id, 100 amt,cast('2018/06/01' as date) date
union all
select 2 id, 100 amt,cast('2018/06/01' as date) date
union all
select 2 id, 130 amt,cast('2018/06/02' as date) date
union all
select 2 id, 130 amt,cast('2018/06/02' as date) date
union all
select 2 id, 130 amt,cast('2018/06/02' as date) date
union all
select 2 id, 130 amt,cast('2018/06/02' as date) date
union all
select 2 id, 100 amt,cast('2018/06/03' as date) date
)
select id,sum(amt) as sum1 from
(
select *,ROW_NUMBER() over(partition by id,date order by id,date) s1 from cte
) b
where s1=1
group by id
具有长版本,但易于理解的查询。下面使用CTE的查询应该对您有所帮助
with records as
(select distinct id, date, amt from table_name)
select sum (amt), id
from records
group by
id;
with cte
as
(
select 1 id, 100 amt,cast('2018/06/01' as date) date
union all
select 1 id, 120 amt,cast('2018/06/02' as date) date
union all
select 1 id, 100 amt,cast('2018/06/03' as date) date
union all
select 1 id, 100 amt,cast('2018/06/03' as date) date
union all
select 1 id, 100 amt,cast('2018/06/03' as date) date
union all
select 2 id, 100 amt,cast('2018/06/01' as date) date
union all
select 2 id, 100 amt,cast('2018/06/01' as date) date
union all
select 2 id, 100 amt,cast('2018/06/01' as date) date
union all
select 2 id, 130 amt,cast('2018/06/02' as date) date
union all
select 2 id, 130 amt,cast('2018/06/02' as date) date
union all
select 2 id, 130 amt,cast('2018/06/02' as date) date
union all
select 2 id, 130 amt,cast('2018/06/02' as date) date
union all
select 2 id, 100 amt,cast('2018/06/03' as date) date
)
select id,sum(amt) as sum1 from
(
select *,ROW_NUMBER() over(partition by id,date order by id,date) s1 from cte
) b
where s1=1
group by id
试试这个:
select id, SUM(amt) from (
select id, SUM(distinct amt) amt, [date] from @tbl
group by id, [date]
) a group by id
首先,您需要按id
和date
对不同的amt
进行分组。接下来,您必须按id
对结果进行分组,对部分求和的amt
列求和(在第一步中,我们只对特定日期的不同值求和)。尝试以下操作:
select id, SUM(amt) from (
select id, SUM(distinct amt) amt, [date] from @tbl
group by id, [date]
) a group by id
首先,您需要按id
和date
对不同的amt
进行分组。接下来,您必须按id
对结果进行分组,对部分求和的amt
列求和(在第一步中,我们只对特定日期的不同值求和)。尝试以下操作
SELECT id,
Sum(amt) AS amt
FROM (SELECT DISTINCT *
FROM mytable) tbl
GROUP BY id
输出
+----+-----+
| id | amt |
+----+-----+
| 1 | 320 |
| 2 | 330 |
+----+-----+
SQL FIDDLE:试试这个
SELECT id,
Sum(amt) AS amt
FROM (SELECT DISTINCT *
FROM mytable) tbl
GROUP BY id
输出
+----+-----+
| id | amt |
+----+-----+
| 1 | 320 |
| 2 | 330 |
+----+-----+
SQL FIDLE:下面使用CTE和行号的查询应该可以帮助您
with records as
(select distinct id, date, amt from table_name)
select sum (amt), id
from records
group by
id;
with cte
as
(
select 1 id, 100 amt,cast('2018/06/01' as date) date
union all
select 1 id, 120 amt,cast('2018/06/02' as date) date
union all
select 1 id, 100 amt,cast('2018/06/03' as date) date
union all
select 1 id, 100 amt,cast('2018/06/03' as date) date
union all
select 1 id, 100 amt,cast('2018/06/03' as date) date
union all
select 2 id, 100 amt,cast('2018/06/01' as date) date
union all
select 2 id, 100 amt,cast('2018/06/01' as date) date
union all
select 2 id, 100 amt,cast('2018/06/01' as date) date
union all
select 2 id, 130 amt,cast('2018/06/02' as date) date
union all
select 2 id, 130 amt,cast('2018/06/02' as date) date
union all
select 2 id, 130 amt,cast('2018/06/02' as date) date
union all
select 2 id, 130 amt,cast('2018/06/02' as date) date
union all
select 2 id, 100 amt,cast('2018/06/03' as date) date
)
select id,sum(amt) as sum1 from
(
select *,ROW_NUMBER() over(partition by id,date order by id,date) s1 from cte
) b
where s1=1
group by id
下面使用CTE和Row_编号的查询应该对您有所帮助
with records as
(select distinct id, date, amt from table_name)
select sum (amt), id
from records
group by
id;
with cte
as
(
select 1 id, 100 amt,cast('2018/06/01' as date) date
union all
select 1 id, 120 amt,cast('2018/06/02' as date) date
union all
select 1 id, 100 amt,cast('2018/06/03' as date) date
union all
select 1 id, 100 amt,cast('2018/06/03' as date) date
union all
select 1 id, 100 amt,cast('2018/06/03' as date) date
union all
select 2 id, 100 amt,cast('2018/06/01' as date) date
union all
select 2 id, 100 amt,cast('2018/06/01' as date) date
union all
select 2 id, 100 amt,cast('2018/06/01' as date) date
union all
select 2 id, 130 amt,cast('2018/06/02' as date) date
union all
select 2 id, 130 amt,cast('2018/06/02' as date) date
union all
select 2 id, 130 amt,cast('2018/06/02' as date) date
union all
select 2 id, 130 amt,cast('2018/06/02' as date) date
union all
select 2 id, 100 amt,cast('2018/06/03' as date) date
)
select id,sum(amt) as sum1 from
(
select *,ROW_NUMBER() over(partition by id,date order by id,date) s1 from cte
) b
where s1=1
group by id