从具有特定值的行中减去
Subtract from rows with certain values
我有一个 table 从 10 个不同的 table 实体化视图中创建。
部分是这样的
group_name
值 1
值2
组1
100
20
group2
200
40
未知
300
60
总计
600
120
我必须将值 group_name = 'unknown' 的行中的所有值重新排列到其他行。最后的 table 应该是这样的
group_name
值 1
值2
组1
200
40
group2
400
80
总计
600
120
所以 'group1' 的公式为:
unknown x group1 x (TOTAL-unknown) + group1
table 是用大量代码创建的,请注意 - 我没有写它,它是给我的,我必须使用它。我不喜欢它的样子,所以请不要生气。无论如何,查询如下所示:
TABLESPACE pg_default
AS
WITH table_value1 AS (
SELECT
table1.group_name,
table1.value1,
FROM table1
), table_value2 AS (
SELECT
table2.group_name,
table2.value2,
FROM table2
), TOTAL_groups AS (
SELECT
'value1'::text AS group_name,
sum(xy_table."value1")::numeric as results
FROM xy_table
UNION ALL
SELECT
'value2'::text AS group_name,
sum(xy_table."value2")::numeric as results
FROM xy_table
UNION ALL
SELECT
'unknown'::text AS group_name,
sum(xy_table."unknown")::numeric as results
FROM xy_table
), TOTAL AS (
SELECT
TOTAL_groups.group_name,
TOTAL_groups.results
FROM TOTAL_groups
UNION ALL
'TOTAL'::text AS group_name,
round(sum(TOTAL_groups.raba), 1) as results
FROM skupaj_energenti
)
SELECT
a.group_name,
COALESCE(a.results, 0::numeric) AS value1,
COALESCE(a.results, 0::numeric) AS value2
FROM table_value1 a
LEFT JOIN table_value2 b ON b.group_name = a.group_name
LEFT JOIN TOTAL c ON f.group_name = a.group_name
WITH DATA;
我不知道在SQL中应该如何写这样的条件。请帮忙
将'unknown'行分配给其他行。假设 value1, value2 是 DECIMAL
select group_name, value1 * (1 + k1) value1, value2 * (1 + k2) value2
from tbl
cross join (
select sum(case group_name when 'unknown' then value1 end) / sum(case group_name when 'TOTAL' then value1 else -value1 end) k1,
sum(case group_name when 'unknown' then value2 end) / sum(case group_name when 'TOTAL' then value2 else -value2 end) k2
from tbl
where group_name in ('TOTAL', 'unknown')
) t
where tbl.group_name not in ('TOTAL', 'unknown')
我有一个 table 从 10 个不同的 table 实体化视图中创建。
部分是这样的
group_name | 值 1 | 值2 |
---|---|---|
组1 | 100 | 20 |
group2 | 200 | 40 |
未知 | 300 | 60 |
总计 | 600 | 120 |
我必须将值 group_name = 'unknown' 的行中的所有值重新排列到其他行。最后的 table 应该是这样的
group_name | 值 1 | 值2 |
---|---|---|
组1 | 200 | 40 |
group2 | 400 | 80 |
总计 | 600 | 120 |
所以 'group1' 的公式为:
unknown x group1 x (TOTAL-unknown) + group1
table 是用大量代码创建的,请注意 - 我没有写它,它是给我的,我必须使用它。我不喜欢它的样子,所以请不要生气。无论如何,查询如下所示:
TABLESPACE pg_default
AS
WITH table_value1 AS (
SELECT
table1.group_name,
table1.value1,
FROM table1
), table_value2 AS (
SELECT
table2.group_name,
table2.value2,
FROM table2
), TOTAL_groups AS (
SELECT
'value1'::text AS group_name,
sum(xy_table."value1")::numeric as results
FROM xy_table
UNION ALL
SELECT
'value2'::text AS group_name,
sum(xy_table."value2")::numeric as results
FROM xy_table
UNION ALL
SELECT
'unknown'::text AS group_name,
sum(xy_table."unknown")::numeric as results
FROM xy_table
), TOTAL AS (
SELECT
TOTAL_groups.group_name,
TOTAL_groups.results
FROM TOTAL_groups
UNION ALL
'TOTAL'::text AS group_name,
round(sum(TOTAL_groups.raba), 1) as results
FROM skupaj_energenti
)
SELECT
a.group_name,
COALESCE(a.results, 0::numeric) AS value1,
COALESCE(a.results, 0::numeric) AS value2
FROM table_value1 a
LEFT JOIN table_value2 b ON b.group_name = a.group_name
LEFT JOIN TOTAL c ON f.group_name = a.group_name
WITH DATA;
我不知道在SQL中应该如何写这样的条件。请帮忙
将'unknown'行分配给其他行。假设 value1, value2 是 DECIMAL
select group_name, value1 * (1 + k1) value1, value2 * (1 + k2) value2
from tbl
cross join (
select sum(case group_name when 'unknown' then value1 end) / sum(case group_name when 'TOTAL' then value1 else -value1 end) k1,
sum(case group_name when 'unknown' then value2 end) / sum(case group_name when 'TOTAL' then value2 else -value2 end) k2
from tbl
where group_name in ('TOTAL', 'unknown')
) t
where tbl.group_name not in ('TOTAL', 'unknown')