Sql server SQL Server左联接

Sql server SQL Server左联接,sql-server,join,Sql Server,Join,这几天来,这个问题一直让我很忙。我试图用不同的想法重写它,但我一直有同样的问题。为了简化问题,我将部分查询放在一个视图中,该视图返回23条记录。使用左连接,我想将来自表tblDatPositionsCalc的字段添加到这23条记录中。正如您所看到的,在TBLDATA PosithSCACC中我有一个附加的条件,以便只考虑最近的记录。在这种情况下,它将返回21条记录。联接应位于两个字段上,即COLCOUNT和colId 我只是希望查询从视图返回23条记录,并尽可能从tblDatPositionsC

这几天来,这个问题一直让我很忙。我试图用不同的想法重写它,但我一直有同样的问题。为了简化问题,我将部分查询放在一个视图中,该视图返回23条记录。使用左连接,我想将来自表tblDatPositionsCalc的字段添加到这23条记录中。正如您所看到的,在TBLDATA PosithSCACC中我有一个附加的条件,以便只考虑最近的记录。在这种情况下,它将返回21条记录。联接应位于两个字段上,即COLCOUNT和colId

我只是希望查询从视图返回23条记录,并尽可能从tblDatPositionsCalc获取信息。实际上,视图中只有2条记录没有tblDatPositionsCalc中相应的id和帐户,这意味着在23条记录中,只有2条在来自表tblDatPositionsCalc的字段中缺少值

我的查询的问题是它只返回tblDatPositionsCalc中的21条记录。我不明白为什么。我试图在加入条件之后将日期条件移入,但没有帮助

SELECT TOP (100) PERCENT
        dbo.vwCurrPos.Account,
        dbo.vwCurrPos.Id,
        dbo.vwCurrPos.TickerBB,
        dbo.vwCurrPos.colEquityCode,
        dbo.vwCurrPos.colType, 
        dbo.vwCurrPos.colCcy,
        dbo.vwCurrPos.colRegion,
        dbo.vwCurrPos.colExchange,
        dbo.vwCurrPos.[Instr Type],
        dbo.vwCurrPos.colMinLastDay, 
        dbo.vwCurrPos.colTimeShift,
        dbo.vwCurrPos.Strike,
        dbo.vwCurrPos.colMultiplier,
        dbo.vwCurrPos.colBetaVol,
        dbo.vwCurrPos.colBetaEq,
        dbo.vwCurrPos.colBetaFloor, 
        dbo.vwCurrPos.colBetaCurv,
        dbo.vwCurrPos.colUndlVol,
        dbo.vwCurrPos.colUndlEq,
        dbo.vwCurrPos.colUndlFut,
        tblDatPositionsCalc_1.colLots, 
        dbo.vwCurrPos.[Open Positions],
        dbo.vwCurrPos.colListMatShift,
        dbo.vwCurrPos.colStartTime,
        tblDatPositionsCalc_1.colPrice,
        tblDatPositionsCalc_1.colMktPrice, 
        dbo.vwCurrPos.colProduct,
        dbo.vwCurrPos.colCalendar,
        CAST(dbo.vwCurrPos.colExpiry AS DATETIME) AS colExpiry,
        dbo.vwCurrPos.colEndTime, 
        CAST(tblDatPositionsCalc_1.colDate AS datetime) AS colDate,
        dbo.vwCurrPos.colFund,
        dbo.vwCurrPos.colExchangeTT,
        dbo.vwCurrPos.colUserTag
FROM dbo.vwCurrPos
LEFT OUTER JOIN dbo.tblDatPositionsCalc AS tblDatPositionsCalc_1
    ON tblDatPositionsCalc_1.colId = dbo.vwCurrPos.Id
    AND tblDatPositionsCalc_1.colAccount = dbo.vwCurrPos.Account
WHERE (tblDatPositionsCalc_1.colDate =
     (SELECT MAX(colDate) AS Expr1 FROM dbo.tblDatPositionsCalc))
ORDER BY
    dbo.vwCurrPos.Account,
    dbo.vwCurrPos.Id,
    dbo.vwCurrPos.colEquityCode,
    dbo.vwCurrPos.colRegion

知道问题的原因吗?

选项1 DrCopyPaste是正确的,因此from子句如下所示:

...
FROM dbo.vwCurrPos
LEFT OUTER JOIN dbo.tblDatPositionsCalc AS tblDatPositionsCalc_1
    ON tblDatPositionsCalc_1.colId = dbo.vwCurrPos.Id
    AND tblDatPositionsCalc_1.colAccount = dbo.vwCurrPos.Account
and (tblDatPositionsCalc_1.colDate =
     (SELECT MAX(colDate) AS Expr1 FROM dbo.tblDatPositionsCalc))
...
原因:left JOIN to column=some表达式的where子句限制带有fail to return for null=SOUTHES,因此该行将被删除

选项2与将代码推入更难维护的其他视图相反,您可以嵌套sql select语句

select 
    X.x1,X.x2,
    Y.*
from X
left join 
    (select Z.z1 as y1, Z.z2 as y2, Z.z3 as y3
     from Z
     where Z.z1 = (select max(Z.z1) from Z)
    ) as Y
on x.x1 = Y.y1 and X.x2 = Y.y2

这里的优点是您可以检查每个嵌套子查询并快速移出。虽然如果你仍然建立更多的逻辑来检查公共表表达式CTE的

HM,你会说我在TBLDATA PosithSCAC上有一个额外的条件,以便只考虑最近的记录,然后你说它只返回21个记录不是你的WHERE子句过滤掉你想要的吗?我理解正确吗?然后,将where条件移动到左外部联接的附加AND中可以解决此问题。在左外部联接之后添加条件可以解决此问题。