写“比较器”在 SQL 中

发布于 2024-09-14 00:52:46 字数 295 浏览 0 评论 0原文

我有以下问题:

根据以下规则查找表 A 中的最高行:

表 A
列:V_Date 日期、int 类型、H_Date 日期

1) 找到最高的 V_Date
2) 如果 V_Dates 相同,则查找优先级最高的行,其中优先级在表 B 中定义,列为 Type int、Priority int
3)如果V_Date和Priority相同,则找到H_Date最高的那个(那么保证唯一)

优先级不分明,所以max(prio)返回多个值。

有人可以帮助我吗?

非常感谢。

I have the following problem:

find the highest row in a table A according to the following rules:

Table A
Columns: V_Date Date, Type int, H_Date Date

1) find the highest V_Date
2) if V_Dates are the same find the row with the highest Priority, where Priority is defined in Table B with columns Type int, Priority int
3) if V_Date and Priority are the same, find the one with the highest H_Date (then it is guaranteed to be unique)

The priorities are not distinct, so max (prio) returns more than one value.

Can anybody help me?

Thank you very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

青衫负雪 2024-09-21 00:52:46

使用 ORDER BY 并将结果限制为一行:

SELECT *
FROM TableA
JOIN TableB ON TableA.Type = TableB.Type
ORDER BY V_Date DESC, Priority DESC, H_DATE DESC
LIMIT 1

具体语法可能因特定数据库而异。

  • 在 MySQL 和 PostgreSQL 中,您可以使用上面的 LIMIT 1
  • 在 SQL Server 中,您可以使用SELECT TOP(1)
  • 在 Oracle 中,您可以使用 SELECT * FROM(此处为子查询)WHERE rownum = 1。

Use ORDER BY and limit the result to one row:

SELECT *
FROM TableA
JOIN TableB ON TableA.Type = TableB.Type
ORDER BY V_Date DESC, Priority DESC, H_DATE DESC
LIMIT 1

Exact syntax may vary depending on the specific database.

  • In MySQL and PostgreSQL you can use LIMIT 1 as above.
  • In SQL Server you can use SELECT TOP(1).
  • In Oracle you can use SELECT * FROM (subquery here) WHERE rownum = 1.
故人的歌 2024-09-21 00:52:46

听起来像是家庭作业(如果不是,请告诉我)。所以你会得到一个普遍的答案。更多详细信息可能需要您指定数据库类型。

JOIN 表。将 ORDER BY 语句与 TOP 语句结合使用。

Sounds like homework (if it isn't, tell me). So you'll get a general answer. More detail might require you to specify the db type.

JOIN the tables. Use the ORDER BY statement in combination with the TOP statement.

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