简体   繁体   中英

SQL Sub-Query from 2 Access Queries

I have two queries that I would like to combine into one query using a subquery but I have not able to figure out the right syntax to create the subquery.

Query B is the query that needs to be referenced for Query A to work properly.

Any assistance would be helpful as I am just starting my education on Transact-SQL.

--These two queries are being migrated over from Access as separate queries--

QUERY 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'));

QUERY 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*')))

When you need to execute one query (B in this case) and then use its results on another query (A in this case) then the SQL standard offers you Common Table Expression (CTEs).

In your case the query (with the CTE) should take the form:

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

In your case (added some formatting):

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'
      )
  )
  ;

Please note that this query is not 100% correct in Transact-SQL, since it still has some MS-Access (non-standard) quirks.

MSAccess, at least when I was using it, did not properly support subqueries, so you had to do stuff like you've shown; in most cases converting it to SQL is just a matter of changing something like this

SELECT stuff 
FROM TableA 
INNER JOIN QueryB ON blah

to

SELECT stuff 
FROM TableA 
INNER JOIN (
   SELECT other_stuff 
   FROM TableB 
   WHERE blahB
) AS QueryB ON blah`

Other than that

  • you'll need to convert any * wildcards to % wildcards
  • if you really are using MySQL (as your current tagging suggests), you will need to replace your [ and ] field delimiters with ` (the unshifted ~ key)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM