美好的一天,
我有3张单独的表格。 2个表头(每行1行信息)和1个事务表(多行)。
我想将聚合表加入标题表。以下是我需要加入的3个表格。请有人协助使用语法。
ARTRNDETAIL:
SELECT
SalesOrder
TrnYear,
TrnMonth,
InvoiceDate,
StockCode,
QtyInvoiced
FROM ArTrnDetail
SORMASTER:
SELECT
SalesOrder,
OrderDate,
ReqShipDate
FROM SorMaster
SorDetail:
SELECT SalesOrder,
sum(MOrderQty) as OrderQty,
sum(MShipQty) as ShipQty,
MStockCode
FROM SorDetail
WHERE MOrderQty > 0
GROUP By SalesOrder,MStockCode
我加入了前两个,因为我希望使用下表将SorMaster的日期字段添加到ArTrnDetail表中:但是,我不知道如何将聚合表添加到其余的表中。请参阅下面的尝试:
SELECT
ArTrnDetail.SalesOrder
TrnYear,
TrnMonth,
InvoiceDate,
StockCode,
QtyInvoiced,
SorMaster.OrderDate,
SorMaster.ReqShipDate
FROM ArTrnDetail
INNER JOIN
SorMaster ON ArTrnDetail.SalesOrder = SorMaster.SalesOrder
我现在需要的是将aggr表加入到最终的top表中,但是加入SalesOrder和StockCode。
请有人帮忙。在此先感谢!
答案 0 :(得分:0)
您可以将两个查询放在CTE中并加入结果
;WITH CTE1 AS (
SELECT
ArTrnDetail.SalesOrder
TrnYear,
TrnMonth,
InvoiceDate,
StockCode,
QtyInvoiced,
SorMaster.OrderDate,
SorMaster.ReqShipDate
FROM ArTrnDetail
INNER JOIN SorMaster
ON ArTrnDetail.SalesOrder = SorMaster.SalesOrder)
,CTE2 AS (
SELECT SalesOrder,
sum(MOrderQty) as OrderQty,
sum(MShipQty) as ShipQty,
MStockCode
FROM SorDetail
WHERE MOrderQty > 0
GROUP By SalesOrder,MStockCode
)
SELECT <columns>
FROM CTE1
INNER JOIN CTE2
ON <your join condition>