我有两个查询,我想使用一个子查询合并为一个查询,但是我无法找出创建子查询的正确语法。
查询B是需要引用才能使查询A正常工作的查询。
当我刚刚开始学习Transact-SQL时,任何帮助都将有所帮助。
-这两个查询正作为单独的查询从Access迁移过来-
查询A:
SELECT Shipment.[Shipment Description], Shipment.[Load ID], Shipment.[Origin Name], Shipment.[Origin City], Shipment.[Origin State], Shipment.[Origin Zip], Shipment.[Origin Country], Shipment.[Destination Name], TMS_Shipment.[Destination State], Shipment.[Destination City], Shipment.[Destination Zip], Shipment.[Destination Country], Shipment.[Pickup To Date/Time], Shipment_Container.Pallets, Shipment_Container.Pieces, [QUERY B].[SumOfReference Number] AS 'Original Number of Pieces', Shipment_Container.Length, Shipment_Container.Width, Shipment_Container.Height, Shipment_Container.[Scaled Weight], Shipment_Container.[Stackability Indicator], Month([Shipment].[Pickup To Date/Time]) AS [Month], Year([Shipment].[Pickup To Date/Time]) AS [Year], [Shipment_Container].[Scaled Weight]/42000 AS [Weight Utilization],Round((100/[Width]),0) AS [# Wide], Round(([QUERY B].[SumOfReference Number]/(Round((100/[Width]),0)))/[Shipment_Container].[Stackability Indicator],0) AS [# Long], Load.[Service Code], (((Round(([QUERY B].[SumOfReference Number]/(Round((100/[Width]),0)))/[Shipment_Container].[Stackability Indicator],0))*[Shipment_Container].[Length])/(629*0.85)) AS Cube, Shipment.[Party Responsible for Freight cost], Load.[Number of Stops]
Into Qry_Utilization
FROM (Load INNER JOIN (Shipment_Container INNER JOIN Shipment ON Shipment_Container.[Shipment Description] = Shipment.[Shipment Description]) ON Load.[Load ID] = Shipment.[Load ID]) INNER JOIN [QUERY B] ON Shipment_Container.[Shipment Description] = [QUERY B].[Shipment Description]
WHERE (((Shipment_Container.Length)>1) AND ((Shipment_Container.Width)>1) AND ((Shipment_Container.Height)>1) AND ((Load.[Service Code])='TL' Or (Load.[Service Code])='SPTL' Or (Load.[Service Code])='SPFB' Or (Load.[Service Code])='TLMR'));
查询B:
(SELECT Shipment_Container_Reference.[Shipment Description], Shipment_Container_Reference.[Reference Type Desc], Sum(Shipment_Container_Reference.[Reference Number]) AS [SumOfReference Number]
FROM Shipment_Container_Reference
GROUP BY Shipment_Container_Reference.[Shipment Description], Shipment_Container_Reference.[Reference Type Desc]
HAVING (((Shipment_Container_Reference.[Reference Type Desc]) Like '*number of pieces*')))
答案 0 :(得分:0)
当您需要执行一个查询(在这种情况下为B),然后将其结果用于另一查询(在这种情况下为A)时,SQL标准将为您提供通用表表达式(CTE)。
对于您而言,查询(带有CTE)应采用以下形式:
with b as (
select ... -- all your SQL select here
)
select ... from a join b ... -- note that here you can use any table, as well as B
以您为例(添加了一些格式):
with b as
(
SELECT
Shipment_Container_Reference.[Shipment Description],
Shipment_Container_Reference.[Reference Type Desc],
Sum(Shipment_Container_Reference.[Reference Number]) AS [SumOfReference Number]
FROM Shipment_Container_Reference
GROUP BY Shipment_Container_Reference.[Shipment Description],
Shipment_Container_Reference.[Reference Type Desc] HAVING
(
(
(
Shipment_Container_Reference.[Reference Type Desc]
)
Like '*number of pieces*'
)
)
)
SELECT
Shipment.[Shipment Description],
Shipment.[Load ID],
Shipment.[Origin Name],
Shipment.[Origin City],
Shipment.[Origin State],
Shipment.[Origin Zip],
Shipment.[Origin Country],
Shipment.[Destination Name],
TMS_Shipment.[Destination State],
Shipment.[Destination City],
Shipment.[Destination Zip],
Shipment.[Destination Country],
Shipment.[Pickup To Date/Time],
Shipment_Container.Pallets,
Shipment_Container.Pieces,
[QUERY B].[SumOfReference Number] AS 'Original Number of Pieces',
Shipment_Container.Length,
Shipment_Container.Width,
Shipment_Container.Height,
Shipment_Container.[Scaled Weight],
Shipment_Container.[Stackability Indicator],
Month([Shipment].[Pickup To Date/Time]) AS [Month],
Year([Shipment].[Pickup To Date/Time]) AS [Year],
[Shipment_Container].[Scaled Weight]/42000 AS [Weight Utilization],
Round((100/[Width]),0) AS [# Wide],
Round(([QUERY B].[SumOfReference Number]/(Round((100/[Width]),0)))/[Shipment_Container].[Stackability Indicator],0) AS [# Long],
Load.[Service Code],
(((Round(([QUERY B].[SumOfReference Number]/(Round((100/[Width]),0)))/[Shipment_Container].[Stackability Indicator],0))*[Shipment_Container].[Length])/(629*0.85)) AS Cube,
Shipment.[Party Responsible for Freight cost],
Load.[Number of Stops] Into Qry_Utilization
FROM
(
Load
INNER JOIN
(
Shipment_Container
INNER JOIN Shipment ON Shipment_Container.[Shipment Description] = Shipment.[Shipment Description]
)
ON Load.[Load ID] = Shipment.[Load ID]
)
INNER JOIN [QUERY B] ON Shipment_Container.[Shipment Description] = [QUERY B].[Shipment Description]
WHERE
(
((Shipment_Container.Length)>1)
AND ((Shipment_Container.Width)>1)
AND ((Shipment_Container.Height)>1)
AND
(
(
Load.[Service Code]
)
='TL' Or
(
Load.[Service Code]
)
='SPTL' Or (Load.[Service Code])='SPFB' Or (Load.[Service Code])='TLMR'
)
)
;
请注意,此查询在Transact-SQL中不是100%正确的,因为它仍有一些MS-Access(非标准)怪癖。
答案 1 :(得分:0)
至少在我使用MSAccess时,MSAccess不能正确支持子查询,因此您必须执行显示的操作;在大多数情况下,将其转换为SQL只是更改这样的问题
SELECT stuff
FROM TableA
INNER JOIN QueryB ON blah
到
SELECT stuff
FROM TableA
INNER JOIN (
SELECT other_stuff
FROM TableB
WHERE blahB
) AS QueryB ON blah`
除此之外
*
通配符转换为%
通配符[
和]
字段定界符替换为`(未移位的〜键)