1
SELECT DISTINCT
        Positions.Issuer_Name
    FROM
     Positions
        LEFT JOIN
        (
            SELECT DISTINCT
              [Assetid]
            FROM
                Report
        ) Report ON
            Report.[Assetid] = Positions.CUSIP //here instead of Report.[Assetid] i need to use `"right([Assetid], Len([Assetid])-1)"`

我需要使用"right([Assetid], Len([Assetid])-1)"它来删除第一个字母并进行比较"Positions.CUSIP"

写这个更好的方法是什么?

而不是像这样写

" ) Report ON Report.right([Assetid], Len([Assetid])-1) = Positions.CUSIP"

我应该先声明变量并先做,"right([Assetid], Len([Assetid])-1)"但是我如何在 JOIN 中使用它呢?

4

2 回答 2

4

这取决于您要执行的操作,您是要根据当时的right()值进行比较,还是要在以下right()值之前进行比较:

SELECT DISTINCT p.Issuer_Name
FROM Positions p
LEFT JOIN
(
    SELECT DISTINCT Right([Assetid], Len(Assetid)-1) TrimmedAssetid, [Assetid]
    FROM Report
) r 
    ON r.TrimedAssetid = p.CUSIP 

或者

SELECT DISTINCT p.Issuer_Name
FROM Positions p
LEFT JOIN
(
    SELECT DISTINCT [Assetid]
    FROM Report
) r 
    ON Right(r.Assetid,  Len(r.Assetid)-1)) = Positions.CUSIP 
于 2012-08-01T18:01:57.977 回答
0

尝试这个 :

AssetID 是您需要与别名一起使用的列

SELECT DISTINCT
    Positions.Issuer_Name
FROM
 Positions
    LEFT JOIN
    (
        SELECT DISTINCT
          [Assetid]
        FROM
            Report
    ) Report ON
        right(Report.[Assetid], Len(Report.[Assetid])-1) = Positions.CUSIP
于 2012-08-01T17:59:52.830 回答