如何避免DB2查询中的冗余?

时间:2015-11-12 14:34:28

标签: sql db2

我试图在查询中每个驱动程序只返回一行。

目前我将我的驱动程序表的列Terminal_number链接到我的网站表的列First_field_insert,但在first_field_insert内有一些相同的值!结果它会产生重复的回报!

理想情况下,我想每个驱动程序只返回一行。

这就是我所拥有的:

SELECT D.DRIVER_ID, D.NAME, D.USER9 AS PHONE, 
       CASE D.TERMINAL_NUMBER WHEN '0' THEN FETCH FIRST ROW ONLY END,
       D.DRIVER_TYPE, S.FAX_PHONE_NUMBER
FROM DRIVER D,
     SITE S
WHERE ACTIVE_IN_DISP = 'True' 
  AND TERMINAL_NUMBER = FIRST_FIELD_INSERT 
  AND FAX_PHONE_NUMBER = :FAX_PHONE_NUMBER
ORDER BY DRIVER_ID, TERMINAL_NUMBER
WITH UR

2 个答案:

答案 0 :(得分:1)

(选择FAX_PHONE_NUMBER来自现场FIRST_FIELD_INSERT = CAST(TERMINAL_NUMBER AS VARCHAR(2))和FAX_PHONE_NUMBER =:FAX_PHONE_NUMBER FETCH FIRST 1 ROW ONLY)as REGION

答案 1 :(得分:0)

我喜欢@Oliver H建议的方向。这里的另一种可能性是在将其与DRIVER表连接之前在SITE表中预先选择必要的记录,但从性能角度来看肯定不是那么理想。

<html>
    <head>

    <style type="text/css">

    /*
    @tab Body
    @section body style
    @tip Set the background color for your email's body area.
    */
    #table{
        /*@editable*/background-color:#888888;
    }

    </style>
    </head>

    <body>
        <table id="table">
            <tr>
                <td>
                    <div mc:edit="content">
                        Lorem ipsum
                    </div>
                </td>
            </tr>
        </table>
    </body>
</html>