SQL两个表的Group By

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

SQL Group By for two tables

问题

我目前有两张表,一张包含客户信息,另一张包含订单信息。我试图创建一张合并表,其中包括客户信息并添加一个列来计算总订单数。

我目前正在编写我的查询,类似于这样:

SELECT 
    C.PrimaryKey,
    C.ClientNumber,
    COUNT(O.Orders) AS '订单总数'
FROM
    ClientInfo C
JOIN 
    Orders O ON O.PrimaryKey=C.PrimaryKey
GROUP BY 
    C.ClientNumber

我已经运行了以上查询的不同变体,结果要么是:

  • 一个错误,因为在选择列表中无效,因为它既不包含在聚合函数中,也不包含在GROUP BY子句中
  • 或者不正确的输出,将每个订单都计算为自己的行,而不是在一个列中计算总数:
客户编号 订单总数
1234 1
1234 1
1234 1
5678 1
5678 1
5678 1

理想情况下,我的输出应该是这样的:

客户编号 订单总数
1234 3
5678 3
英文:

I currently have two tables, one with customer information and the other with order information. I am trying to put together a merged table that gives me the customer info and adds a column that counts total orders.

I am currently writing my query similar like this:

SELECT 
    C.PrimaryKey,
    C.ClientNumber,
    COUNT(O.Orders) AS 'Number of Orders'
FROM
    ClientInfo C
JOIN 
    Orders O ON O.PrimaryKey=C.PrimaryKey
GROUP BY 
    C.ClientNumber

I have ran different variances of the above query and get 1 of 2 results;

  • An error 'is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause'
  • Or an incorrect output that counts each order as its own row instead of counting totals in one column:
ClientNumber Total Orders
1234 1
1234 1
1234 1
5678 1
5678 1
5678 1

Ideally, my output would look like this:

ClientNumber Total Orders
1234 3
5678 3

答案1

得分: 2

尝试将您的原始查询转化为简单的示例查询时,您对其进行了修改。将两个表及其各自的主键连接在一起是没有意义的。您想要根据外键进行连接。

看起来您想要统计每个客户的订单数量。我建议您统计每个客户的订单数量,然后将其与客户表连接。这样,您可以显示客户表的所有列。(通过同时连接两个表并按客户表的主键进行分组,也可以实现相同的功能,但是SQL Server不支持此标准SQL功能,因此如果您首先进行连接然后进行聚合,就需要将要选择的所有客户信息列放在GROUP BY子句中。)

SELECT 
  c.clientinfo_id,
  c.clientnumber,
  o.number_of_orders
FROM clientinfo c
JOIN 
(
  SELECT clientinfo_id, COUNT(orders) AS number_of_orders
  FROM orders
  GROUP BY clientinfo_id
) o ON o.clientinfo_id = c.clientinfo_id
ORDER BY c.clientinfo_id;
英文:

In trying to covert your original query to a simple sample query, you have mutilated it. It makes no sense to join two tables an their respective primary key. You want to join on a foreign key instead.

It seems you want to count orders per client. I suggest you count your orders per client and join this to the client table. Thus you can show all columns of the client table. (The same should be possible by joining both tables and grouping by the client table's primary key, but SQL Server doesn't support this standard SQL feature, so you would have to put all client info columns you want to select in the GROUP BY clause, if you joined first and aggregated then.)

SELECT 
  c.clientinfo_id,
  c.clientnumber,
  o.number_of_orders
FROM clientinfo c
JOIN 
(
  SELECT clientinfo_id, COUNT(orders) AS number_of_orders
  FROM orders
  GROUP BY clientinfo_id
) o ON o.clientinfo_id = c.clientinfo_id
ORDER BY c.clientinfo_id;

答案2

得分: 2

假设您的外键关系是 Order.ForeignKey = ClientInfo.PrimaryKey,这是一个有效查询的示例:

SELECT 
  C.PrimaryKey
, C.ClientNumber,
, COALESCE(O.Count,0) AS [订单数量]
FROM ClientInfo C
LEFT JOIN 
(
   SELECT 
     O.ForeignKey
   , COUNT(*) AS Count
   FROM Orders O 
   GROUP BY O.ForeignKey
) O ON O.ForeignKey = C.PrimaryKey
英文:

Assuming your FK relation is Order.ForeignKey = ClientInfo.PrimaryKey, here is an example of working query:

SELECT 
  C.PrimaryKey
, C.ClientNumber,
, COALESCE(O.Count,0) AS [Number of Orders]
FROM ClientInfo C
LEFT JOIN 
(
   SELECT 
     O.ForeignKey
   , COUNT(*) AS Count
   FROM Orders O 
   GROUP BY O.ForeignKey
) O ON O.ForeginKey = C.PrimaryKey

答案3

得分: 1

  1. O.PrimaryKey=C.PrimaryKey 更改为 O.ClientInfoPrimaryKey=C.PrimaryKey。由于您没有告诉我们订单表中与客户关联的外键的名称,因此我猜测它可能是 ClientInfoPrimaryKey - 根据需要进行更正。

  2. 更改为按 C.PrimaryKey, C.ClientNumber 进行分组 - 所有未进行聚合的列。

英文:

Your question/query is unclear, so this suggested query is based on muscle memory:

SELECT 
  C.PrimaryKey,
  C.ClientNumber,
  COUNT(O.Orders) AS 'Number of Orders'
FROM ClientInfo C
JOIN Orders O ON O.ClientInfoPrimaryKey=C.PrimaryKey
GROUP BY C.PrimaryKey, C.ClientNumber

The changes are:

  1. change O.PrimaryKey=C.PrimaryKey to O.ClientInfoPrimaryKey=C.PrimaryKey. You haven't told us what the name of the foreign key to customer in the orders table is, so I guessed it might be ClientInfoPrimaryKey - correct as necessary.

  2. changed to group by C.PrimaryKey, C.ClientNumber - all non-aggregated columns

huangapple
  • 本文由 发表于 2023年7月11日 00:09:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76655534.html
匿名

发表评论

匿名网友

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

确定