选择连接2个表的列

时间:2016-02-23 06:52:57

标签: sql sql-server

我想从table1和table 2中选择列,但我无法选择它们:

SELECT table1._____
    ,table2.____
    FROM
    (
    SELECT TOP 1 ALLOCATIONS_FW.VEHICLE_ID_FW
    ,ALLOCATIONS_FW.END_DATE_FW
    ,ALLOCATIONS_FW.START_DATE_FW 
    ,ALLOCATIONS_FW.LOCATION_CODE_FW 
    ,ALLOCATIONS_FW.COMPANY_CODE_FW 
    ,ALLOCATIONS_FW.VEHICLE_RECORD_NUMBER_FW
    FROM ALLOCATIONS_FW
    WHERE ALLOCATIONS_FW.START_DATE_FW = (SELECT MAX( ALLOCATIONS_FW.START_DATE_FW)
             FROM ALLOCATIONS_FW
             WHERE ALLOCATIONS_FW.VEHICLE_ID_FW IN
                    (SELECT VEHICLE_ID_FW FROM ALLOCATIONS_FW WHERE VEHICLE_ID_FW = ALLOCATIONS_FW.VEHICLE_ID_FW)
             AND ALLOCATIONS_FW.RECORD_NUMBER_FW = %RECORD_NUMBER_FW%
             )
    AND ALLOCATIONS_FW.RECORD_NUMBER_FW = %RECORD_NUMBER_FW%
    ) AS table1
    JOIN
    (
    SELECT TOP 1 ALLOCATIONS_FW.VEHICLE_ID_FW
    ,ALLOCATIONS_FW.END_DATE_FW
    ,ALLOCATIONS_FW.START_DATE_FW 
    ,ALLOCATIONS_FW.LOCATION_CODE_FW 
    ,ALLOCATIONS_FW.COMPANY_CODE_FW
    ,ALLOCATIONS_FW.DRIVER_NAME_FW 
    FROM ALLOCATIONS_FW
    WHERE VEHICLE_ID_FW=(SELECT VEHICLE_ID_FW FROM ALLOCATIONS_FW 
                            WHERE ALLOCATIONS_FW.RECORD_NUMBER_FW = %RECORD_NUMBER_FW%)
    AND END_DATE_FW = (SELECT MAX( ALLOCATIONS_FW.END_DATE_FW)
             FROM ALLOCATIONS_FW
             WHERE VEHICLE_ID_FW=ALLOCATIONS_FW.VEHICLE_ID_FW)

    ORDER BY ALLOCATIONS_FW.START_DATE_FW DESC
    ) AS OLDALLOC
    ON table1.column1=table2.column1 

我想选择table1和table2列,我该怎么做?因为我试过了

table1.VEHICLE_ID_FW
table1.ALLOCATIONS_FW.VEHICLE_ID_FW

两者都不起作用

1 个答案:

答案 0 :(得分:0)

试试这样,你不应该在子查询中使用order by子句。我刚评论过。请试试这个。

SELECT *
FROM (
    SELECT TOP 1 ALLOCATIONS_FW.VEHICLE_ID_FW
        ,ALLOCATIONS_FW.END_DATE_FW
        ,ALLOCATIONS_FW.START_DATE_FW
        ,ALLOCATIONS_FW.LOCATION_CODE_FW
        ,ALLOCATIONS_FW.COMPANY_CODE_FW
        ,ALLOCATIONS_FW.VEHICLE_RECORD_NUMBER_FW
    FROM ALLOCATIONS_FW
    WHERE ALLOCATIONS_FW.START_DATE_FW = (
            SELECT MAX(ALLOCATIONS_FW.START_DATE_FW)
            FROM ALLOCATIONS_FW
            WHERE ALLOCATIONS_FW.VEHICLE_ID_FW IN (
                    SELECT VEHICLE_ID_FW
                    FROM ALLOCATIONS_FW
                    WHERE VEHICLE_ID_FW = ALLOCATIONS_FW.VEHICLE_ID_FW
                    )
                AND ALLOCATIONS_FW.RECORD_NUMBER_FW = % RECORD_NUMBER_FW %
            )
        AND ALLOCATIONS_FW.RECORD_NUMBER_FW = % RECORD_NUMBER_FW %
    ) AS table1
INNER JOIN (
    SELECT TOP 1 ALLOCATIONS_FW.VEHICLE_ID_FW
        ,ALLOCATIONS_FW.END_DATE_FW
        ,ALLOCATIONS_FW.START_DATE_FW
        ,ALLOCATIONS_FW.LOCATION_CODE_FW
        ,ALLOCATIONS_FW.COMPANY_CODE_FW
        ,ALLOCATIONS_FW.DRIVER_NAME_FW
    FROM ALLOCATIONS_FW
    WHERE VEHICLE_ID_FW = (
            SELECT VEHICLE_ID_FW
            FROM ALLOCATIONS_FW
            WHERE ALLOCATIONS_FW.RECORD_NUMBER_FW = % RECORD_NUMBER_FW %
            )
        AND END_DATE_FW = (
            SELECT MAX(ALLOCATIONS_FW.END_DATE_FW)
            FROM ALLOCATIONS_FW
            WHERE VEHICLE_ID_FW = ALLOCATIONS_FW.VEHICLE_ID_FW
            )
    --ORDER BY ALLOCATIONS_FW.START_DATE_FW DESC
    ) AS OLDALLOC ON table1.column1 = table2.column1