1


以下は私のデータテーブルです:

+----------+--------------+--------+-------+---------------+-------------+--------+
| RegionID | ReceivedDate | FdNo   | FmNo  | FromRegionID  | ToRegionID  | HFlag  |
+----------+--------------+--------+-------+---------------+-------------+--------+
| 26       | 2014-04-10   | 104152 | 1     | 26            | 27          | 1      |
| 26       | 2014-04-10   | 104152 | 1     | 27            | 26          | 0      |
| 26       | 2014-04-10   | 104152 | 2     | 26            | 27          | 1      |
| 26       | 2014-04-10   | 104152 | 2     | 27            | 26          | 0      |
| 26       | 2014-04-10   | 104152 | 3     | 26            | 27          | 1      |
| 26       | 2014-04-10   | 104152 | 3     | 27            | 26          | 0      |
| 26       | 2014-04-10   | 104152 | 4     | 26            | 27          | 1      |
| 26       | 2014-04-10   | 104152 | 4     | 27            | 26          | 0      |
+----------+--------------+--------+-------+---------------+-------------+--------+

fdNo の各グループから最後のレコードを選択したいと思います。サンプル テーブルでは、重複カウントが 2 のデータを指定しましたが、実際には 3,4 である可能性があります。

期待される出力:

+----+-------------------------+--------+---+----+----+---+
| 26 | 2014-04-10 15:35:30.000 | 104152 | 1 | 27 | 26 | 0 |
| 26 | 2014-04-10 15:35:30.000 | 104152 | 2 | 27 | 26 | 0 |
| 26 | 2014-04-10 15:35:44.000 | 104152 | 3 | 27 | 26 | 0 |
| 26 | 2014-04-10 15:35:44.000 | 104152 | 4 | 27 | 26 | 0 |
+----+-------------------------+--------+---+----+----+---+


元のクエリに Ans クエリを追加したい (そこから実際のテーブルが取得されます)

生のクエリ:

SELECT tblreceipt.regionid, 
       tblreceipt.receiveddate, 
       tblreceipt.folderno, 
       tblreceipt.formno, 
       tbltransite.dispatchfromregionid, 
       tbltransite.dispatchtoregionid, 
       tbltransite.horeceivedflag 
FROM   tblreceipt 
       LEFT OUTER JOIN tbltransite 
                    ON tblreceipt.folderno = tbltransite.folderno 
WHERE  tblreceipt.folderno = 104152 
4

1 に答える 1

0
With CTE As (
select  tblReceipt.RegionID,
        tblReceipt.ReceivedDate, 
        tblReceipt.FolderNo,
        tblReceipt.FormNo,
        tblTransite.DispatchFromRegionID,
        tblTransite.DispatchToRegionID,
        tblTransite.HOReceivedFlag,
        Row_Number() Over(Order By FmNo, HFlag) A Rn
        from     tblReceipt
                      left outer join
                 tblTransite
                      on tblReceipt.FolderNo = tblTransite.FolderNo
        where    tblReceipt.FolderNo = 104152
        )
        Select Top 1 From CTE Order By Rn Desc
于 2014-06-07T10:18:06.007 に答える