从结果中选择最大日期行

发布于 2024-09-18 23:58:33 字数 1153 浏览 3 评论 0原文

使用 Pervasive SQL,我有一个结果集:

Tp_No     Name        State    Eff_Date     Actual      Billed   
 1006       ABC        TN      2006-07-01     .1          .5
 1006       ABC        TN      2008-02-15     .27         .6
 1006       ABC        TN      2010-09-01     .37         .7
 1022       Widget     TN      2006-07-01     .1          .5
 1022       Widget     TN      2007-02-22     .27         .6
 1022       Widget     TN      2009-01-01     .37         .7
 1022       Widget     TN      2010-11-11     .38         .71

我想要的是日期为 MAX 的每个客户、公司和州的行:

Tp_No     Name        State    Eff_Date     Actual      Billed 
 1006       ABC        TN      2010-09-01     .37         .7
 1022       Widget     TN      2010-11-11     .38         .71

让事情变得更困难的是,原始结果集是查询,而不仅仅是直接从表中查询。

    select a.tp_no, c.name, a.state, b.eff_date, a.er_rate as 'Actual', b.er_rate as 'Billed' 
from "PR_TSUTA" as a 
left join CL_SUTA as b on(a.tp_no=b.loc_no)
left join CL_MAST as c on(b.loc_no=c.loc_no) 
where c.yn_17 = 'A' and a.er_rate != b.er_rate 
order by a.tp_no

提前致谢

Using Pervasive SQL, I have a result set:

Tp_No     Name        State    Eff_Date     Actual      Billed   
 1006       ABC        TN      2006-07-01     .1          .5
 1006       ABC        TN      2008-02-15     .27         .6
 1006       ABC        TN      2010-09-01     .37         .7
 1022       Widget     TN      2006-07-01     .1          .5
 1022       Widget     TN      2007-02-22     .27         .6
 1022       Widget     TN      2009-01-01     .37         .7
 1022       Widget     TN      2010-11-11     .38         .71

What I want is the row for each Client, Company, and State where the date is MAX:

Tp_No     Name        State    Eff_Date     Actual      Billed 
 1006       ABC        TN      2010-09-01     .37         .7
 1022       Widget     TN      2010-11-11     .38         .71

What makes it a little more difficult is the fact that the original result set is the results of a query, not just straight from a table.

    select a.tp_no, c.name, a.state, b.eff_date, a.er_rate as 'Actual', b.er_rate as 'Billed' 
from "PR_TSUTA" as a 
left join CL_SUTA as b on(a.tp_no=b.loc_no)
left join CL_MAST as c on(b.loc_no=c.loc_no) 
where c.yn_17 = 'A' and a.er_rate != b.er_rate 
order by a.tp_no

Thanks in advance

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

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

发布评论

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

评论(1

耳根太软 2024-09-25 23:58:37

试试这个:

SELECT
    a.tp_no AS ClientNum,
    c.name AS Company,
    a.state AS State,
    MAX(b.eff_date) AS Date
FROM "PR_TSUTA" AS a 
LEFT JOIN CL_SUTA AS b ON a.tp_no = b.loc_no
LEFT JOIN CL_MAST AS c ON b.loc_no = c.loc_no
WHERE c.yn_17 = 'A' AND a.er_rate != b.er_rate 
GROUP BY a.tp_no, c.name, a.state

如果您还需要 Actual 和 Billed 的值,那么您应该使用 greatest-n-per-组查询。

Try this:

SELECT
    a.tp_no AS ClientNum,
    c.name AS Company,
    a.state AS State,
    MAX(b.eff_date) AS Date
FROM "PR_TSUTA" AS a 
LEFT JOIN CL_SUTA AS b ON a.tp_no = b.loc_no
LEFT JOIN CL_MAST AS c ON b.loc_no = c.loc_no
WHERE c.yn_17 = 'A' AND a.er_rate != b.er_rate 
GROUP BY a.tp_no, c.name, a.state

If you also need the values of Actual and Billed then you should use a greatest-n-per-group query.

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