简体   繁体   English

如何将这些查询与 Union All 连接起来?

[英]How do I connect these queries with Union All?

I'm trying to connect these queries but I'm new to the UNION all syntax.我正在尝试连接这些查询,但我是 UNION all 语法的新手。 Is it not as simple as just typing UNION ALL after each select statement?不就是在每个 select 语句后输入 UNION ALL 一样简单吗? It's telling me I have syntax errors but I'm not sure how to combine these otherwise without doing Subqueries.它告诉我我有语法错误,但我不确定如何在不执行子查询的情况下组合这些错误。

Below is the code I attempted to connect myself.下面是我尝试自己连接的代码。 I want it so that the below query will connect (duplicates are acceptable, thus Union)我想要它以便下面的查询将连接(重复是可以接受的,因此联合)

    select /*COMPANY,TYPE,*/A.OrderStatus, replace(ltrim(replace(A.SalesOrder,'0',' ')),' ','0') as SalesOrder, B.MWarehouse as Whse, A.Customer, 
A.EntrySystemDate as EntryDt, B.MCustRequestDat as ReqShip, 
B.MLineShipDate   as ShipDt,  MBackOrderQty as BOQty,A.CustomerPoNumber as PO, '','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','',''
from CompanyR.dbo.SorMaster A 
left join CompanyR.dbo.SorDetail B on A.SalesOrder = B.SalesOrder  

where B.MStockCode = '" & StckCd & "'  
and B.MBackOrderQty > 0  
AND B.NCommentType    NOT IN ('O','I')   
AND B.LineType        NOT IN('4','5', '7')   
AND A.OrderStatus     IN ('0' ,'1', '2', '3', '4', 'S') Order By B.MLineShipDate 

UNION ALL

select /*company,type,*/ '','','','','','','','','', replace(ltrim(replace(A.[Job],'0',' ')),' ','0') as Job,B.Component,A.[Warehouse]  
,A.[JobStartDate],A.[JobDeliveryDate],C.QtyIssued ,C.UnitQtyReqd * A.QtyToMake as GrossQtyRqd  
,((C.UnitQtyReqd * A.QtyToMake) - C.QtyIssued) as OpenQty   
from WipMaster A  
left Join BomStructure B on B.ParentPart = A.StockCode  
left Join WipJobAllMat C on C.Job = A.Job and B.Component = C.StockCode  

where 1 = 1  
and Component = '" & StckCd & "'  
and C.AllocCompleted <> 'Y'  
and (C.QtyIssued/C.UnitQtyReqd) <= (A.QtyToMake - A.QtyManufactured)  order by A.JobDeliveryDate 


UNION ALL

select /*company, type,*/ '','','','','','','','','', A.Job ,A.StockCode ,A.Warehouse as Whse ,A.QtyToMake-QtyManufactured as OutStdQty ,A.JobStartDate as StrtDt,  
A.JobDeliveryDate as DlvryDt  
from CompanyR.dbo.WipMaster A  where  Complete <> 'Y' and StockCode = '" & StckCd & "' order by A.JobDeliveryDate 

UNION ALL

SELECT /*compamu, type,*/ '','','','','','','','','', replace(ltrim(replace(GtrReference,'0',' ')),' ','0') as Ref ,[SourceWarehouse] as SrcWhse,[TargetWarehouse] as TrgtWhse ,  
ExpectedDueDate as ExpDueDt ,(GtrQuantity - QtyReceived) as OutStdQty   
  FROM CompanyR.[dbo].[GtrDetail] 
where 1 = 1 and (GtrQuantity - QtyReceived) <> 0 and StockCode = '" & StckCd & "' and TransferComplete <> 'Y' order by ExpectedDueDate

to make a union imagine that you are "stacking" one result on top of the other, each result must contain the same number of columns and the same data type要使联合想象您将一个结果“堆叠”在另一个结果之上,每个结果必须包含相同数量的列和相同的数据类型

select 'Text' textField,2 numberField,2022-04-16 dateField
union all
select 'Text2' textField,10 numberField,2022-04-22

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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