SQL Server中的JOIN条件

时间:2011-01-01 06:11:25

标签: sql-server

在两个表上应用连接条件后,我想要在左表的记录中最大的记录

我的查询

SELECT a1.*, 
       t.*, 
       ( a1.trnratefrom - t.trnratefrom )AS minrate, 
       ( a1.trnrateto - t.trnrateto )    AS maxrate 
FROM   (SELECT a.srno, 
               trndate, 
               b.trnsrno, 
               Upper(Rtrim(Ltrim(b.trnstate)))   AS trnstate, 
               Upper(Rtrim(Ltrim(b.trnarea)))    AS trnarea, 
               Upper(Rtrim(Ltrim(b.trnquality))) AS trnquality, 
               Upper(Rtrim(Ltrim(b.trnlength)))  AS trnlength, 
               Upper(Rtrim(Ltrim(b.trnunit)))    AS trnunit, 
               b.trnratefrom, 
               b.trnrateto, 
               a.remark, 
               entdate 
        FROM   mstprodrates a 
               INNER JOIN trnprodrates b 
                 ON a.srno = b.srno)a1 
       INNER JOIN (SELECT c.srno, 
                          trndate, 
                          d.trnsrno, 
                          Upper(Rtrim(Ltrim(d.trnstate)))   AS trnstate, 
                          Upper(Rtrim(Ltrim(d.trnarea)))    AS trnarea, 
                          Upper(Rtrim(Ltrim(d.trnquality))) AS trnquality, 
                          Upper(Rtrim(Ltrim(d.trnlength)))  AS trnlength, 
                          Upper(Rtrim(Ltrim(d.trnunit)))    AS trnunit, 
                          d.trnratefrom, 
                          d.trnrateto, 
                          c.remark, 
                          entdate 
                   FROM   mstprodrates c 
                          INNER JOIN trnprodrates d 
                            ON c.srno = d.srno) AS t 
         ON a1.trnstate = t.trnstate 
            AND a1.trnquality = t.trnquality 
            AND a1.trnunit = t.trnunit 
            AND a1.trnlength = t.trnlength 
            AND a1.trnarea = t.trnarea 
            AND a1.remark = t.remark 
WHERE  t.srno = (SELECT MAX(srno) 
                 FROM   a1 
                 WHERE  srno < a1.srno) 

1 个答案:

答案 0 :(得分:0)

如果你想说,

您希望记录存在于左表中但不在右表中,然后使用LEFT OUTER JOIN。