导致相同数据的两个子查询选择表

时间:2017-05-03 21:00:29

标签: sql sql-server join subquery

使用SQL Server Managment Studio 2016。

当我执行以下查询时,SUM(COLUMN_D)和SUM(COLUMN_E)显示相同的结果。但是,列D(TBL_1)的子查询表使用的信息与列E(TBL_2)的子查询表不同。

   SELECT EMPLOYEE, TBL_1.STATE, EMP.NAME, SUM(COLUMN_D) 'Column D', SUM(COLUMN_E) 'Column E'
   FROM
   (
       SELECT T1.*, T2.STATE
       FROM
           (
            SELECT DISTINCT EMPLOYEE, ACCOUNT, MAX(DATE) 'Date', 1 'COLUMN_D'
            FROM TRANSACTION
            GROUP BY EMPLOYEE, ACCOUNT
           ) T1
           LEFT JOIN LOCATION T2 ON T2.ACCOUNT=T1.ACCOUNT
           )TBL_1 

           JOIN
    (
        SELECT TT1.*,TT2.STATE
        FROM
            (
             SELECT DISTINCT ACCOUNT, PROVIDER, MAX(JOB_TITLE) 'Job', 1 'COLUMN_E'
             FROM EMPLOYEE_LIST
             GROUP BY ACCOUNT, PROVIDER
             )TT1
             LEFT JOIN LOCATION TT2 ON TT2.ACCOUNT=TT1.ACCOUNT
             )TBL_2 on TBL_2.ACCOUNT=TBL_1.ACCOUNT
    join EMPLOYER EMP on EMP.EMPID=TBL_1.EMPLOYEE and EMP.EMPID=TBL_2.PROVIDER
    WHERE TBL1.STATE IS NOT NULL
    GROUP BY EMPLOYEE, TBL_1.STATE, EMP.NAME
    ORDER BY EMPLOYEE, EMP.NAME

执行时,结果如下所示: Results

TBL_1的TRANSACTION表显示了交易次数; TBL_1和TBL_2的LOCATION表显示了不同的位置; TBL_2的EMPLOYEE_LIST表显示了员工列表和工作负载(例如,员工A的工作负载为40,但只有10个事务); EMPLOYER表显示更多详细信息,如EMPLOYEE的全名。

因此,结果应如下所示: Should Look Like

他们如何将TBL_2加入TBL_1,导致COLUMN_D和COLUMN_E的数字相同?

示例数据:TRANSACTION

    EMPLOYEE   ACCOUNT   DATE                       BILLED   CHGAMOUNT
    XAC425     100001    2017-03-14 00:00:00.000    B        200.05
    XAC468     100001    2017-03-10 00:00:00.000    B        158.69
    XAC468     100002    2017-03-07 00:00:00.000    B        10.23
    XAC499     100003    2017-03-07 00:00:00.000    B        30.99
    XAC499     100004    2017-03-25 00:00:00.000    B        25.99
    XAC499     100005    2017-03-10 00:00:00.000    B        101.22
    XAC499     100002    2017-03-07 00:00:00.000    B        105.89

样本数据:LOCATION

    EMPLOYEE   ACCOUNT   STATE        ACTIVE 
    XAC425     100001    CALIFORNIA   Y    
    XAC468     100001    UTAH         Y   
    XAC468     100002    UTAH         Y   
    XAC499     100003    ARIZONA      Y      
    XAC499     100004    ARIZONA      Y      
    XAC499     100005    ARIZONA      Y    
    XAC499     100002    ARIZONA      Y     

示例数据:EMPLOYEE_LIST

    PROVIDER   ACCOUNT   JOB TITLE LEVEL   
    XAC425     100001    WORKER    1       
    XAC468     100001    WORKER    1       
    XAC468     100002    WORKER    1       
    XAC499     100003    MANAGER   2            
    XAC499     100004    MANAGER   2
    XAC499     100005    MANAGER   2
    XAC499     100002    MANAGER   2

样本数据:雇主

    EMPID     NAME           Address            City     State  Zip
    XAC425    John Smith     123 Walnut Street  Irving   CA     90265
    XAC468    Kerry Wong     456 North Road     Chicago  IL     60658  
    XAC499    Tim Jones      12 Post Road       New York NY     00014      
    XAC500    Todd Bullock   1234 18th Street   Tucson   AZ     56586 

1 个答案:

答案 0 :(得分:0)

您错过了中间层(子查询的第一层)上的别名。请尝试以下方法:

SELECT EMPLOYEE, TBL_1.STATE, EMP.NAME, SUM(COLUMN_D) 'Column D', SUM(COLUMN_E) 'Column E'
   FROM
   (
       SELECT EMPLOYEE, ACCOUNT, [Date], COLUMN_D, T2.STATE
       FROM
           (
            SELECT DISTINCT EMPLOYEE, ACCOUNT, MAX(DATE) 'Date', 1 'COLUMN_D'
            FROM TRANSACTION
            GROUP BY EMPLOYEE, ACCOUNT
           ) T1
           LEFT JOIN LOCATION T2 ON T2.ACCOUNT=T1.ACCOUNT
           )TBL_1 

           JOIN
    (
        SELECT ACCOUNT, PROVIDER,  Job, COLUMN_E,TT2.STATE
        FROM
            (
             SELECT DISTINCT ACCOUNT, PROVIDER, MAX(JOB_TITLE) 'Job', 1 'COLUMN_E'
             FROM EMPLOYEE_LIST
             GROUP BY ACCOUNT, PROVIDER
             )TT1
             LEFT JOIN LOCATION TT2 ON TT2.ACCOUNT=TT1.ACCOUNT
             )TBL_2 on TBL_2.ACCOUNT=TBL_1.ACCOUNT
    join EMPLOYER EMP on EMP.EMPID=TBL_1.EMPLOYEE and EMP.EMPID=TBL_2.PROVIDER
    WHERE TBL1.STATE IS NOT NULL
    GROUP BY EMPLOYEE, TBL_1.STATE, EMP.NAME
    ORDER BY EMPLOYEE, EMP.NAME