0

更新更清晰

SQL Sever 2000。我试图让这个查询更加独特。

查询:

USE MyDatabase
GO

SELECT MAX(x.provider_entry_id) as provider_entry_id,  -- this ID is the PK
     x.provider_entry_type_id, -- the entry for the specific provider type (the ID)
     x.provider_entry, -- the actual provider entry (the ID)
     x.provider_entry_visit_dt -- the date the entry was created
FROM tbl_claimant_provider_entry x
JOIN (SELECT p.provider_entry_type_id,
             p.provider_entry,
             MAX(provider_entry_visit_dt) AS max_date
        FROM tbl_claimant_provider_entry p
        WHERE provider_entry_clmnt = 4963 -- change this for you user 
        GROUP BY p.provider_entry_type_id, p.provider_entry) y ON y.provider_entry_type_id = x.provider_entry_type_id
                          AND y.max_date = x.provider_entry_visit_dt
GROUP BY x.provider_entry_type_id, x.provider_entry, x.provider_entry_visit_dt

返回:

provider_entry_id   provider_entry_type_id  provider_entry  provider_entry_visit_dt
1052                109                     1088            2013-01-22 00:00:00.000
1051                109                     1665            2013-01-23 00:00:00.000
1049                130                     264             2013-01-01 00:00:00.000
1050                130                     1126            2013-01-02 00:00:00.000
1045                132                     NULL            2013-01-22 00:00:00.000
1047                132                     260             2013-01-22 00:00:00.000
1044                132                     1115            2013-01-10 00:00:00.000
1048                132                     1130            2013-01-22 00:00:00.000
1043                142                     1356            2013-01-10 00:00:00.000

provider_entry_type_id我希望缩小此列表的范围,以便根据最新的仅向我显示每个唯一的实例provider_entry_visit_dt

所以结果将是(请记住,provider_entry_visit_dt 不需要打破平局,这只是我的一个错误):

provider_entry_id   provider_entry_type_id  provider_entry  provider_entry_visit_dt
1051                109                     1665            2013-01-23 00:00:00.000
1050                130                     1126            2013-01-02 00:00:00.000
1048                132                     1130            2013-01-22 00:00:00.000
1043                142                     1356            2013-01-10 00:00:00.000
4

3 回答 3

1

您需要从 group by 语句中删除 created_date。您可以在其上放置一个函数以将其保留在查询中(即类似于您对 provider_entry_id 的函数)。例如:

SELECT MAX(x.provider_entry_id) as provider_entry_id,  -- this ID is the PK
         MAX(x.created_date),
         x.provider_entry_type_id, -- the entry for the specific provider type (the ID)
         MIN(x.provider_entry) -- the actual provider entry (the ID)
    FROM tbl_claimant_provider_entry x
    JOIN (SELECT p.provider_entry_type_id,
                 p.provider_entry,
                 MAX(created_date) AS max_date
            FROM tbl_claimant_provider_entry p
            WHERE provider_entry_clmnt = 4963 -- change this for you user ID
        GROUP BY p.provider_entry_type_id, p.provider_entry) y ON y.provider_entry_type_id = x.provider_entry_type_id
                              AND y.max_date = x.created_date
GROUP BY x.provider_entry_type_id
于 2013-01-23T16:05:45.060 回答
1

我认为您需要删除外部GROUP BY子句

SELECT  x.*
FROM    tbl_claimant_provider_entry x
        INNER JOIN 
        (
            SELECT  p.provider_entry_type_id,
                    MAX(created_date) AS max_date
            FROM    tbl_claimant_provider_entry p
            WHERE   provider_entry_clmnt = 4963 -- change this for you user ID
            GROUP   BY p.provider_entry_type_id
        ) y ON  y.provider_entry_type_id = x.provider_entry_type_id AND 
                y.max_date = x.created_date
于 2013-01-23T16:04:38.247 回答
0

这是有效的解决方案,感谢所有试图提供帮助的人:

SELECT  b.*
FROM    dbo.tbl_claimant_provider_entry AS b 
INNER JOIN
            (SELECT provider_entry_type_id, MAX(provider_entry_visit_dt) AS maxdate
                            FROM    dbo.tbl_claimant_provider_entry
                            GROUP BY provider_entry_type_id) AS m ON b.provider_entry_type_id = m.provider_entry_type_id AND b.provider_entry_visit_dt = m.maxdate
WHERE   (b.provider_entry_clmnt = 4963)
于 2013-01-23T21:59:45.010 回答