Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.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同一表上的多个联接和求和_Sql_Sql Server - Fatal编程技术网

SQL同一表上的多个联接和求和

SQL同一表上的多个联接和求和,sql,sql-server,Sql,Sql Server,我正在一个项目中学习SQL,希望能在以下方面得到一些帮助。我也是stackoverflow的新手,因此如果我的格式设置关闭,我深表歉意: 我有一个表,列有日期、组、人、金额。对于每一天,我都会为每个人提供一个条目,其中包含金额和他们所在的组,因此一行的内容如下所示: Date Group Person Amount 8/7/2012 A Steve 10 我试图写一个语句,返回两天内所有组的总和 我有: Select t1.group,sum(t1.a

我正在一个项目中学习SQL,希望能在以下方面得到一些帮助。我也是stackoverflow的新手,因此如果我的格式设置关闭,我深表歉意:

我有一个表,列有日期、组、人、金额。对于每一天,我都会为每个人提供一个条目,其中包含金额和他们所在的组,因此一行的内容如下所示:

  Date    Group   Person   Amount
8/7/2012     A     Steve     10
我试图写一个语句,返回两天内所有组的总和

我有:

Select t1.group,sum(t1.amount),sum(t2.amount)
  From table t1, table t2
 Where t1.group=t2.group AND t1.date=current_date-1 AND t2.date=current_date-2
 Group by t1.group
我没有得到任何错误,但这两个总数与我刚得到的不同

Select date,sum(amount) From table Group by date

看看这些日子。

你为什么要在两张桌子之间连接

我想你想要:

Select t.group,
       sum(case when t.date = current_date - 1 then t.amount end),
       sum(case when t.date = current_date - 2 then t.amount end)
From table t
Group by t.group 

如果您正在使用MS SQL Server,请更新您的标记,以便其他人更容易找到问题