在SQL中按组求和

huangapple go评论59阅读模式
英文:

SQL Sum by Group

问题

我有一个像这样的发票表:

组别 客户 发票金额
组别 1 约翰 250
组别 1 约翰 250
组别 2 黛布拉 250

我想要将报告显示为:

组别 客户 发票金额
组别 1 约翰 500
组别 2 黛布拉 250

基本上我想要按组别求和。

为了实现这一点,我尝试了以下代码:

select dbo.Clients.Groups,
       SUM(dbo.ClientInvoices.Amount)
from   dbo.Clients
       INNER JOIN dbo.ClientInvoices ON dbo.Clients.Groups = dbo.Clients.Groups

有人能否指导如何实现这个目标?

英文:

I have a invoices table like this:

Group Client Invoice Amount
Group 1 John 250
Group 1 John 250
Group 2 Debra 250

I would like to display the report as:

Group Client Invoice Amount
Group 1 John 500
Group 2 Debra 250

Essentially I'd like to sum by group.

To achieve this, the code that I've tried is:

select dbo.Clients.Groups,
       dbo.ClientInvoices.Amount
from   dbo.Clients
       INNER JOIN dbo.ClientInvoices ON dbo.Clients.Groups = dbo.Clients.Groups

Is anyone able to advise how to achieve this?

答案1

得分: 1

你可以使用 [SUM][1]  [GROUP BY][2]

这是一个示例(你可能需要根据你的确切用例更改列名)

```sql
SELECT Clients.Group,
       Clients.Client,
       SUM(ClientInvoices.Amount) AS Amount
FROM   Clients
       LEFT JOIN ClientInvoices ON Clients.Group = ClientInvoices.Group
GROUP BY Clients.Group, Clients.Client

<details>
<summary>英文:</summary>

You can use [SUM][1] and [GROUP BY][2]

Here&#39;s an example (you might need to change the column names for your exact use case)

```sql
SELECT Clients.Group,
       Clients.Client,
       SUM(ClientInvoices.Amount) AS Amount
FROM   Clients
       LEFT JOIN ClientInvoices ON Clients.Group = ClientInvoices.Group
GROUP BY Clients.Group, Clients.Client

答案2

得分: 0

以下是翻译好的内容:

您可以尝试使用分组或窗口函数来解决您的问题

select 
		  a.[Group]
		 ,a.Client
		 ,a.Amount as [Invoice Amount]
from (
			SELECT c.[Group],
				   c.Client,
				   sum(ci.Amount) over(
				                         partition by 
										                c.[Group]
													  , c.Client
									   ) as Amount,
				   row_number() over(  partition by c.[Group], c.Client order by c.[Group], c.Client) as rw

			FROM   Clients c
				   LEFT JOIN ClientInvoices ci ON c.[Group] = ci.[Group]
	   )a
	   where a.rw=1

您可以使用以下代码插入基本数据

drop table if exists Clients
drop table if exists ClientInvoices
create table  Clients([Group] varchar(100),Client varchar(100))
create table  ClientInvoices([Group] varchar(100),Amount int)

insert into Clients([Group],Client)
          select 'Group 1','John' 
union all select 'Group 1','John' 
union all select 'Group 2','Debra'

insert into ClientInvoices([Group],Amount)
          select 'Group 1',250 
union all select 'Group 2', 250
英文:

You can try using a group by or Window function for solve your problem

select 
		  a.[Group]
		 ,a.Client
		 ,a.Amount as [Invoice Amount]
from (
			SELECT c.[Group],
				   c.Client,
				   sum(ci.Amount) over(
				                         partition by 
										                c.[Group]
													  , c.Client
									   ) as Amount,
				   row_number() over(  partition by c.[Group], c.Client order by c.[Group], c.Client) as rw

			FROM   Clients c
				   LEFT JOIN ClientInvoices ci ON c.[Group] = ci.[Group]
	   )a
	   where a.rw=1

You can to insert the basic data with the following codes


drop table if exists Clients
drop table if exists ClientInvoices
create table  Clients([Group] varchar(100),Client varchar(100))
create table  ClientInvoices([Group] varchar(100),Amount int)

insert into Clients([Group],Client)
          select &#39;Group 1&#39;,&#39;John&#39; 
union all select &#39;Group 1&#39;,&#39;John&#39; 
union all select &#39;Group 2&#39;,&#39;Debra&#39;

insert into ClientInvoices([Group],Amount)
          select &#39;Group 1&#39;,250 
union all select &#39;Group 2&#39;, 250

huangapple
  • 本文由 发表于 2023年4月17日 12:08:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76031659.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定