这与计算符合特定条件的记录数有关,例如invoice amount > $100。
我倾向于更喜欢
COUNT(CASE WHEN invoice_amount > 100 THEN 1 END)
Run Code Online (Sandbox Code Playgroud)
然而,这同样有效
SUM(CASE WHEN invoice_amount > 100 THEN 1 ELSE 0 END)
Run Code Online (Sandbox Code Playgroud)
我认为 COUNT 更可取有两个原因:
COUNTCOUNT 可能在i += 1某处涉及一个简单的操作,而 SUM 不能指望它的表达式是一个简单的整数值。有没有人有关于特定 RDBMS 差异的具体事实?
我有一个main包含以下行的表:
id | name | rank
----+--------+------
1 | Ali | a
2 | Sami | b
3 | Khan | c
4 | Kamran | d
5 | Imran | e
6 | Asad | a
7 | Nawid | v
8 | Jamil | c
9 | Usman | j
Run Code Online (Sandbox Code Playgroud)
我想计算 column 中具有某些值的行rank。例如,我想按如下方式对值进行分组:
(a,b,v)应按名称归入一组myvalues。(c,d,j)应该按名称出现在另一个组中yourvalues。(e)应该通过 name 到达另一个组extravalues。我想要的结果:
myvalues | yourvalues …Run Code Online (Sandbox Code Playgroud) 在 PostgreSQL 9.5 中,我有一个名为的表reports:
CREATE TABLE public.reports (
id BIGSERIAL PRIMARY KEY,
id_station character(11) NOT NULL,
date date NOT NULL,
element character(4) NOT NULL,
value smallint NOT NULL
);
Run Code Online (Sandbox Code Playgroud)
对于每个站(id_station列)和每一天(date列),我可能有多个值类型(元素列) : TMIN, TMAX, TAVG(有时这些值不存在:对于给定的一天,我可能只有 aTMIN和 a TMAX)。
这是一个(假)样本:
22;"FR069029001";"1925-01-01";"TMAX";130
23;"FR069029001";"1925-01-01";"TMIN";-25
24;"FR069029001";"1925-01-01";"TAVG";0
Run Code Online (Sandbox Code Playgroud)
我想使用此表将每个站点和每天的这些值合并在一行中:
CREATE TABLE public.reports_con (
id SERIAL PRIMARY KEY,
id_station character(11) NOT NULL,
date date NOT NULL,
tmin smallint,
tmax smallint,
tavg smallint
);
Run Code Online (Sandbox Code Playgroud)
我想达到这个结果: …
根据对这个问题的回答,我设法产生了以下输出以获得运行的值总和:
id creation operation value running sum
SyJw-c 2016-09-01 00:11:08.307419 positive_op_1 1.33 28.82
SyJw-c 2016-08-21 08:32:54.431662 negative_op_1 -1 27.49
SyJw-c 2016-08-18 07:38:33.878365 positive_op_2 1 28.49
SyJw-c 2016-08-14 18:12:03.599797 negative_op_1 -1 27.49
SyJw-c 2016-08-02 15:44:29.693303 positive_op_1 1.33 28.49
SyJw-c 2016-07-31 12:08:50.659905 override_op_1 4.66 27.16
SyJw-c 2016-06-26 06:53:54.537603 negative_op_1 -3.5 22.5
SyJw-c 2016-05-31 13:34:08.005687 negative_op_1 -1 26
SyJw-c 2016-05-31 13:34:04.776970 negative_op_1 -1 27
SyJw-c 2016-05-31 11:27:09.502983 override_op_2 28 28
Run Code Online (Sandbox Code Playgroud)
但我的情况更复杂。我不仅需要对这些值求和,还需要能够首先根据其下方行的运行总和对某些行执行转换。
我先解释一下动机:
目前我有一个带有增量、减量和覆盖操作的表。我想将数据移植到一个只有增量和减量操作的表中,这样我就可以直接总结这些值。我不希望维护旧表,只是一种将数据迁移到更简单模型的方法,因此只将数据附加到新表。
采用上面的“原始”表,我想编写一个查询(我在 postgresql 9.5 上运行)并获得一个与下面非常相似的表。(相反,我想知道我正在尝试的是 …
如何计数,postgresql 用户表中公共字段的真假如何我试过这个查询
select
sum(case when false then 1 else 0 end) as false,
sum(case when true then 1 else 0 end) as true
from public.user;
Run Code Online (Sandbox Code Playgroud)
但我没有得到任何价值,如果我从查询中删除 public 那么我将得到正确的计数,只有我的值为 true
table : name| DOB | public
values : bb | 20/2/1991/| true
op : true = 1 and false = 0
Run Code Online (Sandbox Code Playgroud)
但是当我公开为假时,我得到了相同的答案
table : name| DOB | public
values : bb | 20/2/1991/| false
op : true = 1 and false = 0
Run Code Online (Sandbox Code Playgroud)
所以有人请帮我解决这个问题
我试图在 Postgres 9.5 中从这个查询中挤出更多的性能。我正在运行超过 400,000 行。
在玩弄它时,我注意到这些CASE语句增加了相当多的查询成本 - 如果我用简单地对一些现有列求和来替换它们,它会将执行时间减半。有没有更有效的方法来计算这些总和?
SELECT sum("tag1"), sum("tag2"), sum("total_tags")
FROM (
SELECT people.data->'recruiter_id' AS recruiter_id,
(CASE WHEN people.data->'tags' ? 'tag1' THEN 1 END) AS "tag1",
(CASE WHEN people.data->'tags' ? 'tag2' THEN 1 END) AS "tag2",
((CASE WHEN people.data->'tags' ? 'tag1' THEN 1 ELSE 0 END) +
(CASE WHEN people.data->'tags' ? 'tag2' THEN 1 ELSE 0 END)) AS total_tags
FROM people WHERE people.data->'tags' ?| ARRAY['tag1','tag2'] ) AS target
GROUP BY recruiter_id
Run Code Online (Sandbox Code Playgroud)
的输出EXPLAIN ANALYSE: …
postgresql performance statistics execution-plan json postgresql-performance
设想:
Table1 的字段名称命名为 testtable
ID、名称、大小、宽度、高度
Table2 的字段名称命名为 errortable
id,desc,field1,field2,operator
errortable 的值
+----+-------------------------------------+--------+--------+----------+
| id | desc | field1 | field2 | operator |
+----+-------------------------------------+--------+--------+----------+
| 1 | size should not greater than width | size | width | > |
| 2 | size should not greater than height | size | height | > |
| 3 | with should be equal to height | width | height | <> |
+----+-------------------------------------+--------+--------+----------+
Run Code Online (Sandbox Code Playgroud)
现在我想检查testtable:
我在视图中有一个非常大的查询(让我们称之为a_sql),这真的很快,除非我ORDER BY在SELECT带有小的外部使用LIMIT:
SELECT
customs.id AS custom_id, customs.custom_name AS custom_name, customs.slug AS slug, customs.use_case AS custom_use_case,
SUM(CASE WHEN designers.id = orders.user_id AND orders.bulk = 't' THEN order_rows.quantity ELSE 0 END) AS sale_bulk,
SUM(CASE WHEN designers.id = orders.user_id AND orders.bulk = 'f' THEN order_rows.quantity ELSE 0 END) AS sale_not_bulk,
SUM(CASE WHEN designers.id = orders.user_id THEN order_rows.quantity ELSE 0 END) AS sale_total,
SUM(CASE WHEN designers.id <> orders.user_id AND orders.bulk = 't' THEN order_rows.quantity ELSE 0 …Run Code Online (Sandbox Code Playgroud) postgresql performance optimization view postgresql-9.4 postgresql-performance
在这个答案中,Erwin Brandstetter 说:
count(step OR NULL) OVER (ORDER BY date)是最短的语法,也适用于 Postgres 9.3 或更早版本。count()只计算非空值。在现代 Postgres 中,更简洁、等效的语法是:Run Code Online (Sandbox Code Playgroud)count(step) FILTER (WHERE step) OVER (ORDER BY date)
我不确定为什么count(step OR NULL)是首选。在我的查询中,我执行以下操作。我重命名了我的变量以匹配他的同时保持语法。
CASE WHEN lag(id_type) OVER (ORDER BY date) <> id_type THEN 1 END AS step
Run Code Online (Sandbox Code Playgroud)
我们正在计算它返回的值。请注意,case 只能返回 1 或 null。
欧文的回答是:
这假设涉及的列是
NOT NULL. 否则你需要做更多。
所以我更迷茫了。添加count(step OR NULL)什么来保护我们的查询有什么意义?
任何人都可以分解这一点,也许可以展示两个带有数据的示例,其中只有一个 - 一个 -count(x OR NULL)有效?
具体来说,我有一个事件表,用于记录用户加入或离开团队的时间。它看起来像下面这样:
-------------------------------------
| user | event | team | timestamp |
-------------------------------------
| A | joined | 1 | 2016-1-1 |
| B | joined | 1 | 2016-1-1 |
| C | left | 1 | 2016-1-1 |
| C | joined | 2 | 2016-1-1 |
| A | left | 1 | 2016-1-2 |
| A | joined | 2 | 2016-1-2 |
| B | left | 1 | 2016-1-3 |
| A | left | …Run Code Online (Sandbox Code Playgroud) postgresql ×10
aggregate ×2
mysql ×2
performance ×2
pivot ×2
count ×1
dynamic-sql ×1
json ×1
migration ×1
null ×1
optimization ×1
oracle ×1
plpgsql ×1
redshift ×1
sql-server ×1
sqlite ×1
statistics ×1
view ×1