SQL 带条件查询 SUM
SQL Query SUM with conditions
我在查找所需的良好查询时遇到了一些困难,正在寻求帮助。
经过多个 JOIN 和很多 WHERE,我有这些列和值:
+---------+----------+--------+-------+
| id_test | quantity | price | fee |
+---------+----------+--------+-------+
| "1-3" | 1 | 33.52 | 29.00 |
| "1-5" | 1 | 33.52 | 29.00 |
| "1-6" | 1 | 33.52 | 29.00 |
| "1-8" | 1 | 86.49 | 29.00 |
| "19-1" | 1 | 176.54 | 29.00 |
| "19-4" | 1 | 176.54 | 29.00 |
| "19-5" | 4 | 176.54 | 29.00 |
| "19-6" | 1 | 199.47 | 29.00 |
| "19-6" | 1 | 176.54 | 29.00 |
| "20-10" | 2 | 72.67 | 29.00 |
| "20-11" | 2 | 18.95 | 29.00 |
| "20-9" | 1 | 22.13 | 29.00 |
+---------+----------+--------+-------+
每个 id_test 对象作为数量、价格和费用。我想对所有内容求和以获得全球价格:(数量*价格)+费用
问题是,这就是我被困的地方,费用只能由 id_test 添加一次。然而,我这里有两个 id_test "19-6".
如果我对我拥有的所有东西求和,则为 1827.67(价格*数量)+ 348(费用)。
因为我有两个“19-6”,所以我需要1827.67(价格*数量)+ 319(费用)。
在 SQL 你会怎么做?
谢谢
Only once by id_test 意味着你必须采取不同的 id_test。如果 29.00 是一个固定的静态值,您可以取不同的 id_test 并将其乘以 29.00
您可以将上述查询的值插入临时 table (#tempTable)。然后你上面的输出值将被临时保存到一个 table
SELECT
DISTINCT(id_test) * 29.00 AS fee
FROM
#tempTable
- 将id_test、(数量*价格)&费用临时table。
WITH cte1 AS (
SELECT
id_test,
(quantity * price) AS total,
fee
FROM
table
)
- 现在为每个 id 添加总数
WITH cte2 AS (
SELECT SUM(total) + fee AS total_id
FROM cte1
GROUP BY id_test
)
- 现在求总和
SELECT SUM(total_id) AS SUM
FROM cte1
select sum(quantity * price),
(select sum(fee)
from (select distinct(id_test), fee
from sales) as fees)
from sales;
我创建了一个SQLFiddle来演示。
您可以使用通用 Table 表达式 (CTE)
假设您的数据在 "data" table:
with total_fee as (
select distinct id_test, fee from data
),
total_price as (
select distinct id_test, sum(quantity) quantity, sum(price) price
from data
group by id_test
)
select sum(b.price*quantity) quantity, sum(a.fee) fee
from total_fee a
left join total_price b
on a.id_test = b.id_test
您可以使用row_number()
SELECT SUM(price*quantity) + SUM(CASE WHEN rn = 1 THEN fee END) AS RESULT
FROM (SELECT *,ROW_NUMBER() OVER ( partition BY id_test) rn
FROM test) m
输出
Result
2146.67
只需使用聚合:
select sum(price * quantity) + fee
from t
group by id_test, fee;
如果您对同一测试有不同的 费用,您可能需要:
select sum(price * quantity) + avg(fee)
from t
group by id_test;
我在查找所需的良好查询时遇到了一些困难,正在寻求帮助。 经过多个 JOIN 和很多 WHERE,我有这些列和值:
+---------+----------+--------+-------+
| id_test | quantity | price | fee |
+---------+----------+--------+-------+
| "1-3" | 1 | 33.52 | 29.00 |
| "1-5" | 1 | 33.52 | 29.00 |
| "1-6" | 1 | 33.52 | 29.00 |
| "1-8" | 1 | 86.49 | 29.00 |
| "19-1" | 1 | 176.54 | 29.00 |
| "19-4" | 1 | 176.54 | 29.00 |
| "19-5" | 4 | 176.54 | 29.00 |
| "19-6" | 1 | 199.47 | 29.00 |
| "19-6" | 1 | 176.54 | 29.00 |
| "20-10" | 2 | 72.67 | 29.00 |
| "20-11" | 2 | 18.95 | 29.00 |
| "20-9" | 1 | 22.13 | 29.00 |
+---------+----------+--------+-------+
每个 id_test 对象作为数量、价格和费用。我想对所有内容求和以获得全球价格:(数量*价格)+费用 问题是,这就是我被困的地方,费用只能由 id_test 添加一次。然而,我这里有两个 id_test "19-6".
如果我对我拥有的所有东西求和,则为 1827.67(价格*数量)+ 348(费用)。
因为我有两个“19-6”,所以我需要1827.67(价格*数量)+ 319(费用)。
在 SQL 你会怎么做?
谢谢
Only once by id_test 意味着你必须采取不同的 id_test。如果 29.00 是一个固定的静态值,您可以取不同的 id_test 并将其乘以 29.00
您可以将上述查询的值插入临时 table (#tempTable)。然后你上面的输出值将被临时保存到一个 table
SELECT
DISTINCT(id_test) * 29.00 AS fee
FROM
#tempTable
- 将id_test、(数量*价格)&费用临时table。
WITH cte1 AS (
SELECT
id_test,
(quantity * price) AS total,
fee
FROM
table
)
- 现在为每个 id 添加总数
WITH cte2 AS (
SELECT SUM(total) + fee AS total_id
FROM cte1
GROUP BY id_test
)
- 现在求总和
SELECT SUM(total_id) AS SUM
FROM cte1
select sum(quantity * price),
(select sum(fee)
from (select distinct(id_test), fee
from sales) as fees)
from sales;
我创建了一个SQLFiddle来演示。
您可以使用通用 Table 表达式 (CTE)
假设您的数据在 "data" table:
with total_fee as (
select distinct id_test, fee from data
),
total_price as (
select distinct id_test, sum(quantity) quantity, sum(price) price
from data
group by id_test
)
select sum(b.price*quantity) quantity, sum(a.fee) fee
from total_fee a
left join total_price b
on a.id_test = b.id_test
您可以使用row_number()
SELECT SUM(price*quantity) + SUM(CASE WHEN rn = 1 THEN fee END) AS RESULT
FROM (SELECT *,ROW_NUMBER() OVER ( partition BY id_test) rn
FROM test) m
输出
Result
2146.67
只需使用聚合:
select sum(price * quantity) + fee
from t
group by id_test, fee;
如果您对同一测试有不同的 费用,您可能需要:
select sum(price * quantity) + avg(fee)
from t
group by id_test;