PROGRESS 数据库的 SQL 命令
请耐心等待 SQL 新手 - 我正在尝试编写一个带有 PROGRESS 数据库连接的 SQL 命令。然后我想仅从连接中选择第一个匹配的记录。我想使用 LIMIT 但 PROGRESS 不支持。我认为 MIN 或 TOP 也可以工作,但语法有问题。这是当前语法:
SELECT esthead_0."k-est-code", estdie_0."estd-size2", estdie_0."k-cmp-no", estdie_0."estd-cal"
FROM VISION.PUB.estdie estdie_0
INNER JOIN VISION.PUB.esthead esthead_0 ON estdie_0."k-est-code" = esthead_0."k-est-code"
WHERE estdie_0."k-cmp-no" = (SELECT MIN("k-cmp-no")
FROM VISION.PUB.estdie estdie_0 )
这将从整个表中选择 MIN,但我想要连接为每个“k-est-code”返回的记录的 MIN。
Please bear with me new to SQL- I am trying to write an SQL command with a join in a PROGRESS db. I would like to then select only the first matching record from the join. I thought to use LIMIT but PROGRESS does not support that. MIN or TOP would also work I think but having trouble with the syntax. Here is current syntax:
SELECT esthead_0."k-est-code", estdie_0."estd-size2", estdie_0."k-cmp-no", estdie_0."estd-cal"
FROM VISION.PUB.estdie estdie_0
INNER JOIN VISION.PUB.esthead esthead_0 ON estdie_0."k-est-code" = esthead_0."k-est-code"
WHERE estdie_0."k-cmp-no" = (SELECT MIN("k-cmp-no")
FROM VISION.PUB.estdie estdie_0 )
This will select the MIN from the whole table but I would like the MIN of the records the join returns for each "k-est-code".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要完成您正在完成的任务,您需要使用聚合函数和 GROUP BY。
这是正确的查询:
添加 GROUP BY / 聚合查询的一般语法是:
使用 MIN()、MAX()、AVG()、SUM() 等聚合函数来选择所需的列...(根据需要选择函数关于您是否想要最小值、最大值等)。我列出了一些标准的,然后您的数据库通常还会为您提供一些附加的。
将您选择的所有其他列(函数中的列除外)添加到查询末尾的 GROUP BY 中。
将
GROUP BY 必须出现在 WHERE 之后、ORDER BY 之前。
如果你想对函数进行类似 WHERE 的过滤(假设你只需要 k-cmp-no 超过 100),你可以在 group by 之后使用 HAVING,例如:
有最小值(estdie_0."k-cmp-no") > 100
Google for Group By 和 Aggregate 函数以获取有关此 SQL 概念的更多信息。它在所有数据库中的工作方式都相同,因为它是标准 ANSI SQL。请参阅此页面以获取更全面的介绍和示例:http://www.w3schools.com/sql/ sql_groupby.asp
To do what you're accomplishing, you need to use Aggregate functions and GROUP BY.
Here is the correct query:
The general syntax for adding a GROUP BY / Aggregate query is:
use an aggregate function like MIN(), MAX(), AVG(), SUM() to select which column you want ... (choose the function depending on whether you want minimum, maximum etc). There are those I listed which are standard, and then often your database will give you some additional ones as well.
Add every other column you're selecting EXCEPT the ones in the function to a GROUP BY at the end of your query.
Your GROUP BY has to occur after your WHERE, but before your ORDER BY.
If you want to do WHERE-like filtering on the function (say you wanted only k-cmp-no over 100), you use HAVING after the group by, e.g.:
HAVING MIN(estdie_0."k-cmp-no") > 100
Google for Group By and Aggregate functions for more info on this SQL concept. It works the same in all databases as it's standard ANSI SQL. See this page for a more thorough introduction with examples: http://www.w3schools.com/sql/sql_groupby.asp
Progress (OE 11.2) 支持 OFFSET FETCH,与 mysql 中的 LIMIT OFFSET 相同。
示例:
希望这有帮助
Progress (OE 11.2) supports OFFSET FETCH which is same as LIMIT OFFSET in mysql.
Example:
Hope this helps