PROGRESS 数据库的 SQL 命令

发布于 2024-08-07 12:37:24 字数 558 浏览 5 评论 0原文

请耐心等待 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤檠 2024-08-14 12:37:24

要完成您正在完成的任务,您需要使用聚合函数和 GROUP BY。

这是正确的查询:

SELECT esthead_0."k-est-code", estdie_0."estd-size2", MIN(estdie_0."k-cmp-no") AS k-cmp-no-minimum, 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"
 GROUP BY esthead_0."k-est-code", estdie_0."estd-size2", estdie_0."estd-cal"

添加 GROUP BY / 聚合查询的一般语法是:

  1. 使用 MIN()、MAX()、AVG()、SUM() 等聚合函数来选择所需的列...(根据需要选择函数关于您是否想要最小值、最大值等)。我列出了一些标准的,然后您的数据库通常还会为您提供一些附加的。

  2. 将您选择的所有其他列(函数中的列除外)添加到查询末尾的 GROUP BY 中。

  3. GROUP BY 必须出现在 WHERE 之后、ORDER BY 之前。

  4. 如果你想对函数进行类似 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:

SELECT esthead_0."k-est-code", estdie_0."estd-size2", MIN(estdie_0."k-cmp-no") AS k-cmp-no-minimum, 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"
 GROUP BY esthead_0."k-est-code", estdie_0."estd-size2", estdie_0."estd-cal"

The general syntax for adding a GROUP BY / Aggregate query is:

  1. 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.

  2. Add every other column you're selecting EXCEPT the ones in the function to a GROUP BY at the end of your query.

  3. Your GROUP BY has to occur after your WHERE, but before your ORDER BY.

  4. 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

不弃不离 2024-08-14 12:37:24

Progress (OE 11.2) 支持 OFFSET FETCH,与 mysql 中的 LIMIT OFFSET 相同。

示例:

SQLExplorer>select FirstName , LastName , EmpNum from pub.employee order by empnum offset 10 rows fetch next 10 rows only;
FirstName                      LastName                                                EmpNum
------------------------------ -------------------------------------------------- -----------
Frank                          Garsen                                                      11
Jenny                          Morris                                                      12
Luke                           Sanders                                                     13
Marcy                          Adams                                                       14
Alex                           Simons                                                      15
Holly                          Atkins                                                      16
Larry                          Barry                                                       17
Jean                           Brady                                                       18
Larry                          Dawsen                                                      19
Dan                            Flanagan                                                    20

希望这有帮助

Progress (OE 11.2) supports OFFSET FETCH which is same as LIMIT OFFSET in mysql.

Example:

SQLExplorer>select FirstName , LastName , EmpNum from pub.employee order by empnum offset 10 rows fetch next 10 rows only;
FirstName                      LastName                                                EmpNum
------------------------------ -------------------------------------------------- -----------
Frank                          Garsen                                                      11
Jenny                          Morris                                                      12
Luke                           Sanders                                                     13
Marcy                          Adams                                                       14
Alex                           Simons                                                      15
Holly                          Atkins                                                      16
Larry                          Barry                                                       17
Jean                           Brady                                                       18
Larry                          Dawsen                                                      19
Dan                            Flanagan                                                    20

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文