是否可以将此外部申请转换为左连接?
Is it possible to convert this Outer apply to Left Join?
SELECT
hb.custnum,hb.custname as Cname,dv.custname as [CustName],'0' + hb.cellnum as cellnum,dv.ccelnumber,
UPPER(hb.subd) as subd,UPPER(dv.BilStAdd) as BilStAdd,hb.entity,dv.BilCtAdd
FROM [cust] as dv
OUTER APPLY (
SELECT TOP 1 * FROM ##custnumtbl WHERE ##custnumtbl.CustNum = dv.custnum ORDER BY totaldue DESC
) hb
Where
'0' + hb.cellnum <> dv.ccelnumber
or RTRIM(LTRIM(hb.custname)) <> RTRIM(LTRIM(dv.CustName))
or UPPER(hb.subd) <> RTRIM(LTRIM(UPPER(dv.BilStAdd)))
or UPPER(hb.entity) <> RTRIM(LTRIM(UPPER(dv.BilCtAdd)))
(注意:OP 在写完这个答案后确定了数据库。它不适用于 SQL Server 2000。)
是的,如果你愿意的话。最简单的方法是使用 window 函数,例如 row_number()
:
SELECT hb.custnum,hb.custname as Cname, dv.custname as [CustName],
'0' + hb.cellnum as cellnum, dv.ccelnumber,
UPPER(hb.subd) as subd,UPPER(dv.BilStAdd) as BilStAdd,hb.entity,dv.BilCtAdd
FROM [cust] dv LEFT JOIN
(SELECT c.*,
ROW_NUMBER() OVER (PARTITION BY c.CustNum ORDER BY totaldue DESC) as seqnum
FROM ##custnumtbl c
) hb
ON hb.CustNum = dv.custnum AND seqnum = 1
WHERE '0' + hb.cellnum <> dv.ccelnumber or
RTRIM(LTRIM(hb.custname)) <> RTRIM(LTRIM(dv.CustName)) or
UPPER(hb.subd) <> RTRIM(LTRIM(UPPER(dv.BilStAdd))) or
UPPER(hb.entity) <> RTRIM(LTRIM(UPPER(dv.BilCtAdd)));
我认为 outer apply
的性能可能更好。
对于 SQL Server 2000:
SELECT
hb.custnum,hb.custname as Cname,dv.custname as [CustName],'0' + hb.cellnum as cellnum,dv.ccelnumber,
UPPER(hb.subd) as subd,UPPER(dv.BilStAdd) as BilStAdd,hb.entity,dv.BilCtAdd
FROM [cust] as dv
LEFT JOIN(
SELECT b.*
FROM (
SELECT CustNum, totaldue = MAX(totaldue)
FROM ##custnumtbl
GROUP BY CustNum
)a
INNER JOIN ##custnumtbl b
ON b.CustNum = a.CustNum
AND b.totaldue = a.totaldue
)hb
ON hb.CustNUm = dv.custnum
Where
'0' + hb.cellnum <> dv.ccelnumber
or RTRIM(LTRIM(hb.custname)) <> RTRIM(LTRIM(dv.CustName))
or UPPER(hb.subd) <> RTRIM(LTRIM(UPPER(dv.BilStAdd)))
or UPPER(hb.entity) <> RTRIM(LTRIM(UPPER(dv.BilCtAdd)))
正如 M.Ali 所指出的,如果您不使用区分大小写的排序规则,则可能需要删除 UPPER
.
SELECT
hb.custnum,hb.custname as Cname,dv.custname as [CustName],'0' + hb.cellnum as cellnum,dv.ccelnumber,
UPPER(hb.subd) as subd,UPPER(dv.BilStAdd) as BilStAdd,hb.entity,dv.BilCtAdd
FROM [cust] as dv
OUTER APPLY (
SELECT TOP 1 * FROM ##custnumtbl WHERE ##custnumtbl.CustNum = dv.custnum ORDER BY totaldue DESC
) hb
Where
'0' + hb.cellnum <> dv.ccelnumber
or RTRIM(LTRIM(hb.custname)) <> RTRIM(LTRIM(dv.CustName))
or UPPER(hb.subd) <> RTRIM(LTRIM(UPPER(dv.BilStAdd)))
or UPPER(hb.entity) <> RTRIM(LTRIM(UPPER(dv.BilCtAdd)))
(注意:OP 在写完这个答案后确定了数据库。它不适用于 SQL Server 2000。)
是的,如果你愿意的话。最简单的方法是使用 window 函数,例如 row_number()
:
SELECT hb.custnum,hb.custname as Cname, dv.custname as [CustName],
'0' + hb.cellnum as cellnum, dv.ccelnumber,
UPPER(hb.subd) as subd,UPPER(dv.BilStAdd) as BilStAdd,hb.entity,dv.BilCtAdd
FROM [cust] dv LEFT JOIN
(SELECT c.*,
ROW_NUMBER() OVER (PARTITION BY c.CustNum ORDER BY totaldue DESC) as seqnum
FROM ##custnumtbl c
) hb
ON hb.CustNum = dv.custnum AND seqnum = 1
WHERE '0' + hb.cellnum <> dv.ccelnumber or
RTRIM(LTRIM(hb.custname)) <> RTRIM(LTRIM(dv.CustName)) or
UPPER(hb.subd) <> RTRIM(LTRIM(UPPER(dv.BilStAdd))) or
UPPER(hb.entity) <> RTRIM(LTRIM(UPPER(dv.BilCtAdd)));
我认为 outer apply
的性能可能更好。
对于 SQL Server 2000:
SELECT
hb.custnum,hb.custname as Cname,dv.custname as [CustName],'0' + hb.cellnum as cellnum,dv.ccelnumber,
UPPER(hb.subd) as subd,UPPER(dv.BilStAdd) as BilStAdd,hb.entity,dv.BilCtAdd
FROM [cust] as dv
LEFT JOIN(
SELECT b.*
FROM (
SELECT CustNum, totaldue = MAX(totaldue)
FROM ##custnumtbl
GROUP BY CustNum
)a
INNER JOIN ##custnumtbl b
ON b.CustNum = a.CustNum
AND b.totaldue = a.totaldue
)hb
ON hb.CustNUm = dv.custnum
Where
'0' + hb.cellnum <> dv.ccelnumber
or RTRIM(LTRIM(hb.custname)) <> RTRIM(LTRIM(dv.CustName))
or UPPER(hb.subd) <> RTRIM(LTRIM(UPPER(dv.BilStAdd)))
or UPPER(hb.entity) <> RTRIM(LTRIM(UPPER(dv.BilCtAdd)))
正如 M.Ali 所指出的,如果您不使用区分大小写的排序规则,则可能需要删除 UPPER
.