从Join Query中选择基于Single Column的唯一数据

时间:2014-10-16 11:20:31

标签: sql cucm

查询

SELECT DISTINCT 
    device.name as device,
    devicepool.name as devicepool,
    device.description AS Phone_Description,
    NumPlan.dNOrPattern as Primary_did,
    applicationuser.name
    FROM applicationuser 
    LEFT OUTER JOIN applicationuserdevicemap
        on applicationuserdevicemap.fkapplicationuser = applicationuser.pkid 
    LEFT OUTER JOIN device 
        on device.pkid =applicationuserdevicemap.fkdevice
    LEFT OUTER JOIN DeviceNumPlanMap 
        on DeviceNumPlanMap.fkDevice=Device.pkid 
    FULL JOIN NumPlan 
        on NumPlan.pkid = DeviceNumPlanMap.fkNumPlan AND NumPlan.dNOrPattern NOT LIKE '%#%'
    LEFT OUTER JOIN devicepool
        on device.fkdevicepool = devicepool.pkid 
    WHERE (DeviceNumPlanMap.NumPlanIndex=1 OR DeviceNumPlanMap.NumPlanIndex IS NULL)
        AND device.name like 'SEP%'
        AND device.description NOT LIKE '%IPCOM%'
        AND applicationuser.name='actuser'
    ORDER BY devicepool.name, NumPlan.dNOrPattern

结果是

device          | devicepool| phone_description |   primary_did | name
--------------------------------------------------------------------------
SEPA40CC3956A58 | DBL_Test  | Auto 8964         | 8964          | actuser
SEP00230432ABDC | DP_W00301 | Auto 8968         | 4082600       | actuser
SEP001BD5E86761 | DP_W00301 | SEP001B54520367   | 7001          | actuser
SEP00260B5E1997 | DP_W00301 | Auto 8965         | 8965          | actuser
SEP00175A756654 | DP_W06851 | Dobson,Ronda      |               | actuser
SEP00175A756654 | DP_W06851 | Dobson,Ronda      | 8505998965    | actuser
SEP001B54520367 | DP_WA6201 | SEP001B54520367   |               | actuser

我需要使用Primaru DID的结果为NULL或空白。这导致设备列中的非唯一条目与SEP00175A756654重复两次。

理想的结果应该是,

     device         | devicepool| phone_description |   primary_did | name
    --------------------------------------------------------------------------
    SEPA40CC3956A58 | DBL_Test  | Auto 8964         | 8964          | actuser
    SEP00230432ABDC | DP_W00301 | Auto 8968         | 4082600       | actuser
    SEP001BD5E86761 | DP_W00301 | SEP001B54520367   | 7001          | actuser
    SEP00260B5E1997 | DP_W00301 | Auto 8965         | 8965          | actuser
    SEP00175A756654 | DP_W06851 | Dobson,Ronda      | 8505998965    | actuser
    SEP001B54520367 | DP_WA6201 | SEP001B54520367   |               | actuser

我需要解决方法如何在这里使用内连接。如果原因也可以解释。

0 个答案:

没有答案