Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL Server按组求和_Sql_Sql Server - Fatal编程技术网

SQL Server按组求和

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

例如,在这个表中,我需要使用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/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