簡體   English   中英

來自2個訪問查詢的SQL子查詢

[英]SQL Sub-Query from 2 Access Queries

我有兩個查詢,我想使用一個子查詢合並為一個查詢,但是我無法找出創建該子查詢的正確語法。

查詢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*')))

當您需要執行一個查詢(在這種情況下為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(非標准)怪癖。

至少在我使用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`

除此之外

  • 您需要將任何*通配符轉換為%通配符
  • 如果您確實在使用MySQL(如當前標記所示),則需要用`(未移位的〜鍵)替換[]字段分隔符

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM