我有一个用于MS SQL DB的litte SQL字符串。
"SELECT"
" [Navision4].[dbo].[3S Company A_S$Item].[No_],[Navision4].[dbo].[3S Company A_S$Item].[Description] AS Description,[Navision4].[dbo].[3S Company A_S$Item].[Description 2], [Navision4].[dbo].[3S Company A_S$Item].[Item Status], [Navision4].[dbo].[3S Company A_S$Item].[New Sales Price DK], [Navision4].[dbo].[3S Company A_S$Item].[New List Price DK],[Navision4].[dbo].[3S Company A_S$Item].[New Sales Price No], [Navision4].[dbo].[3S Company A_S$Item].[New List Price No], [Navision4].[dbo].[3S Company A_S$Item].[New Sales Price S], [Navision4].[dbo].[3S Company A_S$Item].[New List Price S],[Navision4].[dbo].[3S Company A_S$Item Cross Reference].[Cross-Reference No_], [Navision4].[dbo].[3S disponibelt for export].[Disponibelt], [Navision4].[dbo].[3S Lagersaldo].[Quantity] AS Lagerantal, [Navision4].[dbo].[3S i_bestilling].[Expected Receipt Date] AS ToDispDate "
"FROM [Navision4].[dbo].[3S Company A_S$Item] "
"LEFT OUTER JOIN [Navision4].[dbo].[3S Company A_S$Item Cross Reference] ON [Navision4].[dbo].[3S Company A_S$Item].[No_] = [Navision4].[dbo].[3S Company A_S$Item Cross Reference].[Item No_] AND [Navision4].[dbo].[3S Company A_S$Item Cross Reference].[Cross-Reference Type No_] = 'EAN' "
"LEFT OUTER JOIN [Navision4].[dbo].[3S disponibelt for export] ON [Navision4].[dbo].[3S disponibelt for export].[Item No_] = [Navision4].[dbo].[3S Company A_S$Item].[No_] "
"LEFT OUTER JOIN [Navision4].[dbo].[3S Lagersaldo] ON [Navision4].[dbo].[3S Company A_S$Item].[No_] = [Navision4].[dbo].[3S Lagersaldo].[Item no_] "
"LEFT OUTER JOIN [Navision4].[dbo].[3S i_bestilling] ON [Navision4].[dbo].[3S Company A_S$Item].[No_] = [Navision4].[dbo].[3S i_bestilling].[No_] "
"WHERE [Navision4].[dbo].[3S Company A_S$Item].[No_] <> '' " & strWhereItemCategory & " " & strWhereItemVendor & " " & strWhereItemStatus & " " & strWhereItemC & " " & strWhereItemE & " " & strWhereQty & " " & strWhereMisc & " "
"ORDER BY [Navision4].[dbo].[3S Company A_S$Item].[" & strOrderSort & "] " & strSort & ""
但是我想从这个OUTER LEFT JOIN中只选择SELECT TOP 1 &#34; LEFT OUTER JOIN [Navision4]。[dbo]。[3S Company A_S $ Item Cross Reference] ON [Navision4]。[dbo]。[3S Company A_S $ Item]。[No_] = [Navision4]。[ dbo]。[3S公司A_S $项目交叉参考]。[项目编号]和[导航4]。[dbo]。[3S公司A_S $项目交叉参考]。[交叉参考类型No_] =&#39; EAN&# 39; &#34;
我该怎么做?
答案 0 :(得分:1)
而不是左连接使用外部应用,如
Outer Apply(select top 1 * from [Navision4].[dbo].[3S Company A_S$Item Cross
Reference] WHERE [Navision4].[dbo].[3S Company A_S$Item].[No_] = [Navision4].
[dbo].[3S Company A_S$Item Cross Reference].[Item No_] AND [Navision4].[dbo].[3S
Company A_S$Item Cross Reference].[Cross-Reference Type No_] = 'EAN' Order BY someValue) o
在Order BY someValue
中将someValue
更改为相应的列。
还可以使用别名来获得更易读的语句。
最后声明:
SELECT [Navision4].[dbo].[3S Company A_S$Item].[No_] ,
[Navision4].[dbo].[3S Company A_S$Item].[Description] AS Description ,
[Navision4].[dbo].[3S Company A_S$Item].[Description 2] ,
[Navision4].[dbo].[3S Company A_S$Item].[Item Status] ,
[Navision4].[dbo].[3S Company A_S$Item].[New Sales Price DK] ,
[Navision4].[dbo].[3S Company A_S$Item].[New List Price DK] ,
[Navision4].[dbo].[3S Company A_S$Item].[New Sales Price No] ,
[Navision4].[dbo].[3S Company A_S$Item].[New List Price No] ,
[Navision4].[dbo].[3S Company A_S$Item].[New Sales Price S] ,
[Navision4].[dbo].[3S Company A_S$Item].[New List Price S] ,
o.[Cross-Reference No_] ,
[Navision4].[dbo].[3S disponibelt for export].[Disponibelt] ,
[Navision4].[dbo].[3S Lagersaldo].[Quantity] AS Lagerantal ,
[Navision4].[dbo].[3S i_bestilling].[Expected Receipt Date] AS ToDispDate
FROM [Navision4].[dbo].[3S Company A_S$Item]
OUTER APPLY ( SELECT TOP 1
*
FROM [Navision4].[dbo].[3S Company A_S$Item Cross Reference]
WHERE [Navision4].[dbo].[3S Company A_S$Item].[No_] = [Navision4].[dbo].[3S Company A_S$Item Cross Reference].[Item No_]
AND [Navision4].[dbo].[3S Company A_S$Item Cross Reference].[Cross-Reference Type No_] = 'EAN'
ORDER BY someValue
) o
LEFT OUTER JOIN [Navision4].[dbo].[3S disponibelt for export] ON [Navision4].[dbo].[3S disponibelt for export].[Item No_] = [Navision4].[dbo].[3S Company A_S$Item].[No_]
LEFT OUTER JOIN [Navision4].[dbo].[3S Lagersaldo] ON [Navision4].[dbo].[3S Company A_S$Item].[No_] = [Navision4].[dbo].[3S Lagersaldo].[Item no_]
LEFT OUTER JOIN [Navision4].[dbo].[3S i_bestilling] ON [Navision4].[dbo].[3S Company A_S$Item].[No_] = [Navision4].[dbo].[3S i_bestilling].[No_]