- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想创建一个窗口函数来计算当前行中字段的值出现在当前行之前的有序分区部分的次数。为了使这个更具体,假设我们有一个这样的表:
| id| fruit | date |
+---+--------+------+
| 1 | apple | 1 |
| 1 | cherry | 2 |
| 1 | apple | 3 |
| 1 | cherry | 4 |
| 2 | orange | 1 |
| 2 | grape | 2 |
| 2 | grape | 3 |
我们想创建一个这样的表(为清楚起见省略日期列):
| id| fruit | prior |
+---+--------+-------+
| 1 | apple | 0 |
| 1 | cherry | 0 |
| 1 | apple | 1 |
| 1 | cherry | 1 |
| 2 | orange | 0 |
| 2 | grape | 0 |
| 2 | grape | 1 |
请注意,对于 id = 1
,沿着有序分区移动,第一个条目“apple”不匹配任何内容(因为隐含集为空),下一个水果“cherry”也不匹配。然后我们再次得到'apple',这是一场比赛等等。我想象 SQL 看起来像这样:
SELECT
id, fruit,
<some kind of INTERSECT?> OVER (PARTITION BY id ORDER by date) AS prior
FROM fruit_table;
但我找不到任何看起来合适的东西。 FWIW,我正在使用 PostgreSQL 8.4。
最佳答案
您可以在没有窗口函数的情况下通过self-left join 和count()
优雅地解决这个问题:
SELECT t.id, t.fruit, t.day, count(t0.*) AS prior
FROM tbl t
LEFT JOIN tbl t0 ON (t0.id, t0.fruit) = (t.id, t.fruit) AND t0.day < t.day
GROUP BY t.id, t.day, t.fruit
ORDER BY t.id, t.day
我重命名了日期列 day
因为 date
是 reserved word in every SQL standard and in PostgreSQL .
我更正了您的示例数据中的一个错误。他们按照您的方式拥有它,但没有结帐。可能会使人感到困惑。
如果您的目的是使用窗口函数,那么这个应该可行:
SELECT id, fruit, day
,count(*) OVER (PARTITION BY id, fruit ORDER BY day) - 1 AS prior
FROM tbl
ORDER BY id, day
If frame_end is omitted it defaults to CURRENT ROW.
(id, fruit)
- 包括当前行。这就是 - 1
的用途。关于sql - 在 SQL 中使用窗口函数运行总计 "matches",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9368691/
我需要一些帮助。 我希望“总计”由“数量*价格=总计”计算(到目前为止没问题)。问题是我还需要通过“总/价格=数量”来计算“数量”,即如果一个字段发生更改,另一个字段也会自动更改。 我做了一个非常简单
我的项目即将结束,但在解决它时遇到了一些问题。我是一名厨师,想要自己的应用程序与他自己的业务相关 我的表单称为“菜谱”,并包含数量、单价、使用百分比。 假设您有 1 公斤苹果,价格为 5 欧元。清洁结
我的数据库表是: 产品展示 - ID - 名称 ... 订单 - ID - 状态 ... OrderItems - ID -order_id -product_id -数量 -line_total 我
我的项目即将结束,但在解决它时遇到了一些问题。我是一名厨师,想要自己的应用程序与他自己的业务相关 我的表单称为“菜谱”,并包含数量、单价、使用百分比。 假设您有 1 公斤苹果,价格为 5 欧元。清洁结
exchanges表结构: id exchange created_at updated_at deleted_at start_time
我正在开发 Telerik RadGrid,网格内有多个 radtextbox,如 A、B、C 和 D。我使用带有正则表达式验证的 radtextbox 来验证带有数字的 A、B 和 C 文本框。然而
我有一个非常简单的$lookup聚合查询,如下所示: {'$lookup': {'from': 'edge', 'localField': 'gid', 'foreignField': 't
我正在尝试做一些与我之前问过的问题非常相似的事情,但我似乎无法让它正常工作。这是我之前的问题:How to get totals per day 表格如下: Table N
[更新] 谢谢大家,最终代码: var EUR_share_cost = 0; var USD_share_cost = 0; var GBP_shar
由于某种原因,下面的这部分代码没有将每个数组项添加到一起。我单步执行调试器,正在创建并递增数组项,但 total += ScoreArray[i]; 似乎没有将已输入的数字相加。相反,我只是将第一个输
我在这里有点迷路了。我有一个 for 循环,它增加一个值,如下所示: int nu01 = 10000; int level = 0; int increase = 35000; for (int i
请引用之前的问题:Sum total for column in jQuery 我使用了 Aymen 的解决方案,但我对其进行了编辑以满足我的需要。它停止工作,我的代码如下所示,在 jsfiddle
Date Flight ID Member ID Seat Type Seat Price 2013-07-28 F71498 M692
我希望程序忽略零总数。我正试图阻止向非参与者提供零总数的汽车奖励。我在 && 运算符后面添加了 >= 表达式。这对于击杀数和净值来说正确吗? $fetch_nor_killers = mysq
我有下表: CREATE TABLE zoom (`id` int, `fb_id` int, `date` datetime); INSERT INTO zoom (`id`, `f
我想知道是否有一种方法可以将已计算的派生列的总和添加到新列中。 Employee_KT_State --------------------------------------------------
在 SQL 中,我想获得来自每个用户的网站点击率的百分比。为此,我需要获取 site hits 列的总和,但我的查询在另一列上使用了 GROUP By。 除了 GROUP BY 中的每个 user_i
我有以下查询: SELECT SUM(case when s1 = 'fel' then 1 else 0 end) as s1_count, SUM(case when s2 = '
我已经使用 HTML 创建了一个表格。该表由一个标题和许多称为 Family 的行组成。每个家庭行可以有许多行代表家庭的各个成员(请参见下面的代码)。 Family N
如何使用 javascript 给出 float 总计。请 friend 们帮助我。 function addNumbers() {
我是一名优秀的程序员,十分优秀!