在包含子查询的查询中使用分组功能。

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

group by in queries that contains subqueries

问题

如何按customerId分组以下查询:

  1. select
  2. (SELECT SUM(Purchase) FROM Customers where type in (17,21)) as 'Purchase'
  3. ,(SELECT SUM(Point) FROM Customers) as 'Point'
  4. FROM CUSTOMERS
  5. GROUP BY Customers.CustomerID
英文:

How can I group by the following query by customerId

  1. select
  2. (SELECT SUM(Purchase) FROM Customers where type in (17,21)) as 'Purchase'
  3. ,(SELECT SUM(Point) FROM Customers) as 'Point'
  4. FROM CUSTOMERS
  5. GROUP BY Customers.CustomerID

答案1

得分: 4

看起来你只是想要条件聚合,你可以在SUM内部使用CASE WHEN...来实现。

  1. SELECT
  2. c.CustomerId
  3. SUM(CASE WHEN c.type IN (17, 21) THEN c.Purchase END) AS Purchase,
  4. SUM(c.Point) AS Point
  5. FROM CUSTOMERS c
  6. GROUP BY
  7. c.CustomerID;

请注意使用表别名使其更易读,并且只在必要时使用[]来引用列名,不要使用''

英文:

Looks like you just want conditional aggregation, which you can do by placing CASE WHEN... inside a SUM.

  1. SELECT
  2. c.CustomerId
  3. SUM(CASE WHEN c.type IN (17, 21) THEN c.Purchase END) AS Purchase,
  4. SUM(c.Point) AS Point
  5. FROM CUSTOMERS c
  6. GROUP BY
  7. c.CustomerID;

Note the use of a table alias to make it more readable, and do not use '' to quote column names, only [] and only where necessary.

答案2

得分: 0

更改查询为:

  1. SELECT
  2. [CustomerID] as 'CustomerID'
  3. , SUM([Purchase]) as 'Purchase'
  4. , SUM([Point]) as 'Point'
  5. FROM [CUSTOMERS]
  6. WHERE [type] in (17,21)
  7. GROUP BY [Customers].[CustomerID]

这将为您提供:

  1. CustomerID Purchase Point
  2. 1 246 3
  3. 2 23434 7
英文:

Remove the From Customers in the Selects and Move the Where to the root of the query

Change your query to

  1. SELECT
  2. [CustomerID] as 'CustomerID'
  3. , SUM([Purchase]) as 'Purchase'
  4. , SUM([Point]) as 'Point'
  5. FROM [CUSTOMERS]
  6. WHERE [type] in (17,21)
  7. GROUP BY [Customers].[CustomerID]

This will give you

  1. CustomerID Purchase Point
  2. 1 246 3
  3. 2 23434 7

答案3

得分: -1

如果您想要获取每位客户的总_Purchase_和总_Point_,那么您需要在_Purchase_子查询中以及在主查询中包括CustomerId。

关于Group By需要记住的关键一点是,所有非聚合列都必须包含在其中。因此,在您的情况下,您需要按_Purchase_分组。

您的最终查询将如下所示:

  1. select c.CustomerId,
  2. (SELECT SUM(Purchase) FROM Customers where type in (17,21) and CustomerId = c.CustomerId) as 'Purchase',
  3. SUM(Point) as 'Point'
  4. FROM CUSTOMERS c
  5. GROUP BY c.CustomerID, Purchase
英文:

If you're trying to get the total Purchase and total Points per customer, then you need to include the CustomerId in the Purchase sub-query as well as in your main query.

A key thing to remember regarding Group By is that all non-aggregate columns have to be included in it. Therefore in your case, you'd have to group by the Purchase as well.

Your final query would look like:

<pre>
select c.CustomerId,
(SELECT SUM(Purchase) FROM Customers where type in (17,21) and CustomerId = c.CustomerId) as 'Purchase'
, SUM(Point) as 'Point'
FROM CUSTOMERS c
GROUP BY c.CustomerID, Purchase
</pre>

huangapple
  • 本文由 发表于 2023年7月23日 17:58:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747627.html
匿名

发表评论

匿名网友

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

确定