English 中文(简体)
Select max date row from results
原标题:

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

最佳回答

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.

问题回答

暂无回答




相关问题
摘录数据

我如何将Excel板的数据输入我的Django应用? I m将PosgreSQL数据库作为数据库。

Postgres dump of only parts of tables for a dev snapshot

On production our database is a few hundred gigabytes in size. For development and testing, we need to create snapshots of this database that are functionally equivalent, but which are only 10 or 20 ...

How to join attributes in sql select statement?

I want to join few attributes in select statement as one for example select id, (name + + surname + + age) as info from users this doesn t work, how to do it? I m using postgreSQL.

What text encoding to use?

I need to setup my PostgreSQL DB s text encoding to handle non-American English characters that you d find showing up in languages such as German, Spanish, and French. What character encoding should ...

SQL LIKE condition to check for integer?

I am using a set of SQL LIKE conditions to go through the alphabet and list all items beginning with the appropriate letter, e.g. to get all books where the title starts with the letter "A": SELECT * ...

热门标签