如何在查询中添加辅助列?
How to add auxiliary column inside query?
我的 table 可能是这样的:
| Email | ID | Order_date | Total |
| ----------------- | --------- | ------------ | ------------ |
|customerA@gmail.com| 1 | 01-01-2019 | 500 |
|customerB@gmail.com| 2 | 02-01-2019 | 1000 |
|customerC@gmail.com| 3 | 03-01-2019 | 1000 |
|customerD@gmail.com| 4 | 04-01-2019 | 2000 |
|customerA@gmail.com| 5 | 05-01-2019 | 3000 |
|customerB@gmail.com| 6 | 06-01-2019 | 500 |
|customerC@gmail.com| 7 | 03-01-2019 | 1000 |
|customerA@gmail.com| 8 | 05-01-2019 | 3000 |
|customerB@gmail.com| 9 | 09-01-2019 | 2000 |
|customerA@gmail.com| 10 | 10-01-2019 | 4000 |
|customerB@gmail.com| 11 | 02-01-2019 | 1000 |
|customerA@gmail.com| 12 | 12-01-2019 | 2000 |
我想添加一个包含订单数量的辅助列,但不进行任何额外的分组。
我的梦想 table 是这样的:
| Email | ID | Order_date | Total | Number of orders |
| ----------------- | --------- | ------------ | ------------ | ---------------- |
|customerA@gmail.com| 1 | 01-01-2019 | 500 | 5 |
|customerB@gmail.com| 2 | 02-01-2019 | 1000 | 4 |
|customerC@gmail.com| 3 | 03-01-2019 | 1000 | 2 |
|customerD@gmail.com| 4 | 04-01-2019 | 2000 | 1 |
|customerA@gmail.com| 5 | 05-01-2019 | 3000 | 5 |
|customerB@gmail.com| 6 | 06-01-2019 | 500 | 4 |
|customerC@gmail.com| 7 | 03-01-2019 | 1000 | 2 |
|customerA@gmail.com| 8 | 05-01-2019 | 3000 | 5 |
|customerB@gmail.com| 9 | 09-01-2019 | 2000 | 4 |
|customerA@gmail.com| 10 | 10-01-2019 | 4000 | 5 |
|customerB@gmail.com| 11 | 02-01-2019 | 1000 | 4 |
|customerA@gmail.com| 12 | 12-01-2019 | 2000 | 5 |
正如您可能猜到的那样,我想根据购物订单的数量执行进一步的计算(如总和或平均值)。我的 SQL 查询可能如下所示:
SELECT AVG(Total)
FROM
(SELECT ...
...
...
WHERE ...
...
...
GROUP BY ...
HAVING COUNT(Number_of_orders > 1) AND COUNT(Number_of_orders < 5) x
或者类似的东西。只是为了让您了解为什么我认为我需要辅助列。
我原以为这很容易,但我已经花了很多时间在这上面,所以任何类型的建议或帮助都将不胜感激!
假设 'Email' 是您要按订单数分组的列,这应该会得到您想要的 table。
SELECT T.Email, T.ID, T.Date, T.Total, TT.Count
FROM Table T
INNER JOIN (
SELECT Email, Count(Email) as Count
FROM Table
GROUP BY Email) TT
ON T.Email = TT.Email
如果你的数据库引擎支持partitioning
:
select * , count(*) over (partition by Email) as OrderCount
from tablename
我的 table 可能是这样的:
| Email | ID | Order_date | Total |
| ----------------- | --------- | ------------ | ------------ |
|customerA@gmail.com| 1 | 01-01-2019 | 500 |
|customerB@gmail.com| 2 | 02-01-2019 | 1000 |
|customerC@gmail.com| 3 | 03-01-2019 | 1000 |
|customerD@gmail.com| 4 | 04-01-2019 | 2000 |
|customerA@gmail.com| 5 | 05-01-2019 | 3000 |
|customerB@gmail.com| 6 | 06-01-2019 | 500 |
|customerC@gmail.com| 7 | 03-01-2019 | 1000 |
|customerA@gmail.com| 8 | 05-01-2019 | 3000 |
|customerB@gmail.com| 9 | 09-01-2019 | 2000 |
|customerA@gmail.com| 10 | 10-01-2019 | 4000 |
|customerB@gmail.com| 11 | 02-01-2019 | 1000 |
|customerA@gmail.com| 12 | 12-01-2019 | 2000 |
我想添加一个包含订单数量的辅助列,但不进行任何额外的分组。 我的梦想 table 是这样的:
| Email | ID | Order_date | Total | Number of orders |
| ----------------- | --------- | ------------ | ------------ | ---------------- |
|customerA@gmail.com| 1 | 01-01-2019 | 500 | 5 |
|customerB@gmail.com| 2 | 02-01-2019 | 1000 | 4 |
|customerC@gmail.com| 3 | 03-01-2019 | 1000 | 2 |
|customerD@gmail.com| 4 | 04-01-2019 | 2000 | 1 |
|customerA@gmail.com| 5 | 05-01-2019 | 3000 | 5 |
|customerB@gmail.com| 6 | 06-01-2019 | 500 | 4 |
|customerC@gmail.com| 7 | 03-01-2019 | 1000 | 2 |
|customerA@gmail.com| 8 | 05-01-2019 | 3000 | 5 |
|customerB@gmail.com| 9 | 09-01-2019 | 2000 | 4 |
|customerA@gmail.com| 10 | 10-01-2019 | 4000 | 5 |
|customerB@gmail.com| 11 | 02-01-2019 | 1000 | 4 |
|customerA@gmail.com| 12 | 12-01-2019 | 2000 | 5 |
正如您可能猜到的那样,我想根据购物订单的数量执行进一步的计算(如总和或平均值)。我的 SQL 查询可能如下所示:
SELECT AVG(Total)
FROM
(SELECT ...
...
...
WHERE ...
...
...
GROUP BY ...
HAVING COUNT(Number_of_orders > 1) AND COUNT(Number_of_orders < 5) x
或者类似的东西。只是为了让您了解为什么我认为我需要辅助列。
我原以为这很容易,但我已经花了很多时间在这上面,所以任何类型的建议或帮助都将不胜感激!
假设 'Email' 是您要按订单数分组的列,这应该会得到您想要的 table。
SELECT T.Email, T.ID, T.Date, T.Total, TT.Count
FROM Table T
INNER JOIN (
SELECT Email, Count(Email) as Count
FROM Table
GROUP BY Email) TT
ON T.Email = TT.Email
如果你的数据库引擎支持partitioning
:
select * , count(*) over (partition by Email) as OrderCount
from tablename