1
select table1.t1 from
(
(
select   
         ItemCategory.Name as Category,
         InventoryItems.Name as ItemName,
         sum(SalesItems.Quantity) as Quantity,
         (InventoryItems.Weight*sum(SalesItems.Quantity)) as Weight,
         sum(SalesItems.Amount) as Amount
   from SalesInvoices 
   inner join Sales on Sales.ID = SalesInvoices.SalesID 
   inner join SalesItems on SalesItems.SalesID = Sales.ID
   inner join InventoryItems on InventoryItems.ID = SalesItems.InventoryItemID 
   inner join ItemCategory on ItemCategory.ID = InventoryItems.ItemCategoryID 
   inner join BusinessPartners on Sales.BusinessPartnerID = BusinessPartners.ID
   where SalesInvoices.Date >= '2013-07-1' and SalesInvoices.Date <= '2013-11-7'
   group by ItemCategory.Name,InventoryItems.Name,InventoryItems.Weight
) as t1,
(
select  
        ItemCategory.Name as Category,
        InventoryItems.Name as ItemName,
        sum(SalesAdjustmentItems.AdjustedQuantity)*-1 as Quantity,
        (sum(SalesAdjustmentItems.AdjustedQuantity)*InventoryItems.Weight)*-1 as  
         Weight,
         sum(SalesAdjustmentItems.AmountReturn)*-1 as Amount
 from SalesInvoices 
 inner join Sales on Sales.ID = SalesInvoices.SalesID 
 inner join SalesItems on SalesItems.SalesID = Sales.ID
 inner join SalesAdjustmentItems on SalesAdjustmentItems.SalesItemID = SalesItems.ID 
 inner join InventoryItems on InventoryItems.ID = SalesItems.InventoryItemID
 inner join ItemCategory on ItemCategory.ID = InventoryItems.ItemCategoryID 
 inner join SalesAdustment on SalesAdustment.SalesInvoiceID = SalesInvoices.ID
 inner join BusinessPartners on Sales.BusinessPartnerID = BusinessPartners.ID 
 where SalesAdustment.Date>= '2013-07-1' and SalesAdustment.Date <= '2013-11-7'
 group by ItemCategory.Name,InventoryItems.Name,InventoryItems.Weight
 ) as t2
 )
 as table1

このクエリで間違っていること。1 番目のクエリは販売用で、2 番目のクエリは販売返品用です。売上と返品の差額を求めたい。しかし、私にエラーを与えています。

ありがとう

4

2 に答える 2

5

SQL マイナス演算子は、EXCEPTたとえば、請求書のない売上を見つけるために知られています。

-- Sales minus SalesInvoices
SELECT ID
  FROM Sales
EXCEPT
SELECT SalesID
  FROM SalesInvoices;
于 2013-11-07T08:35:53.600 に答える