进度 SQL 找不到列或未指定查询

Progress SQL Column cannot be found or is not specified for query

我正在针对 Progress 10.2B07 数据库编写 SQL 并收到以下错误“无法找到列 'OUTERINVOICEHEADER.MEMBERID' 或未指定查询 (13865)。

这里是查询:

select concat(substring(OuterInvoiceHeader.sold_to_cust_nbr, 1, 6) + '-', OuterInvoiceHeader.sold_to_cust_seq) as MemberID, 
sum(OuterInvoiceHeader.net_weight) as TotalInvoicePounds, 
sum(OuterInvoiceHeader.net_weight / 2000) as TotalTons, 
sum(OuterInvoiceHeader.invoice_amt) as InvoiceAmount, 
sum(InvoiceSurcharges.Surcharge) as Surcharges, 
sum(OuterInvoiceHeader.invoice_amt - InvoiceSurcharges.Surcharge) as Total,
sum(Returns.qty_received) as PoundsReturned
from AXS.PUB.ivc_header OuterInvoiceHeader

inner join
(select m.invoice_nbr, sum(m.extension) Surcharge  from AXS.PUB.ivc_mchgs m
inner join
AXS.PUB.ivc_header h
on h.invoice_nbr = m.invoice_nbr
group by m.invoice_nbr) InvoiceSurcharges
on OuterInvoiceHeader.invoice_nbr = InvoiceSurcharges.invoice_nbr

left outer join
(select concat(substring(ReturnHeader.ship_to_nbr, 1, 6)+'-',InnerInvoiceHeader.sold_to_cust_seq) as ReturnMemberID, 
ReturnHeader.invoice_nbr as ReturnInvoiceNum, 
qty_received
from AXS.PUB.return_hdr ReturnHeader
inner join
AXS.PUB.ivc_header InnerInvoiceHeader
on ReturnHeader.invoice_nbr = InnerInvoiceHeader.invoice_nbr
inner join AXS.PUB.return_line ReturnLine
on ReturnHeader.claim_nbr = ReturnLine.claim_nbr
where ReturnInvoiceNum = '0001010914'
group by ReturnMemberID, ReturnInvoiceNum, qty_received) Returns
on OuterInvoiceHeader.MemberID = Returns.ReturnMemberID
--on OuterInvoiceHeader.invoice_nbr = Returns.ReturnInvoiceNum

where OuterInvoiceHeader.sold_to_cust_nbr =  '000837' and     OuterInvoiceHeader.invoice_date between '06/01/2016' and '06/30/2016' and  OuterInvoiceHeader.invoice_status = '5804' and OuterInvoiceHeader.invoice_type='5601'
group by MemberID

问题出在左连接;如果未注释,注释掉的子句 "on OuterInvoiceHeader.invoice_nbr = Returns.ReturnInvoiceNum" 将起作用。 "on OuterInvoiceHeader.MemberID = Returns.ReturnMemberID" 子句给了我错误。

我不明白的是,这两个都引用了顶部SELECT语句中的一列,唯一的区别是一个是串联而另一个不是。

我希望我在这里只见树木不见森林,答案很简单,所以如果有人有任何建议或问题,我会洗耳恭听。

试试这个:

我将对别名 MemberID 的引用替换为实际的连接列 CONCAT(SUBSTRING(OuterInvoiceHeader.sold_to_cust_nbr, 1, 6)+'-', OuterInvoiceHeader.sold_to_cust_seq)

SELECT                     CONCAT(SUBSTRING(OuterInvoiceHeader.sold_to_cust_nbr, 1, 6)+'-', OuterInvoiceHeader.sold_to_cust_seq) AS MemberID
                    , SUM(OuterInvoiceHeader.net_weight) AS TotalInvoicePounds
                    , SUM(OuterInvoiceHeader.net_weight / 2000) AS TotalTons
                    , SUM(OuterInvoiceHeader.invoice_amt) AS InvoiceAmount
                    , SUM(InvoiceSurcharges.Surcharge) AS Surcharges
                    , SUM(OuterInvoiceHeader.invoice_amt - InvoiceSurcharges.Surcharge) AS Total
                    , SUM(Returns.qty_received) AS PoundsReturned
FROM                       AXS.PUB.ivc_header OuterInvoiceHeader
                      INNER JOIN
                              (SELECT m.invoice_nbr
                                   , SUM(m.extension) Surcharge
                               FROM   AXS.PUB.ivc_mchgs m
                                    INNER JOIN AXS.PUB.ivc_header h ON h.invoice_nbr = m.invoice_nbr
                               GROUP BY m.invoice_nbr) InvoiceSurcharges ON OuterInvoiceHeader.invoice_nbr = InvoiceSurcharges.invoice_nbr
                      LEFT OUTER JOIN
                                  (SELECT CONCAT(SUBSTRING(ReturnHeader.ship_to_nbr, 1, 6)+'-', InnerInvoiceHeader.sold_to_cust_seq) AS ReturnMemberID
                                       , ReturnHeader.invoice_nbr AS ReturnInvoiceNum
                                       , qty_received
                                   FROM   AXS.PUB.return_hdr ReturnHeader
                                        INNER JOIN AXS.PUB.ivc_header InnerInvoiceHeader ON ReturnHeader.invoice_nbr = InnerInvoiceHeader.invoice_nbr
                                        INNER JOIN AXS.PUB.return_line ReturnLine ON ReturnHeader.claim_nbr = ReturnLine.claim_nbr
                                   WHERE  ReturnInvoiceNum = '0001010914'
                                   GROUP BY ReturnMemberID
                                        , ReturnInvoiceNum
                                        , qty_received) Returns ON CONCAT(SUBSTRING(OuterInvoiceHeader.sold_to_cust_nbr, 1, 6)+'-', OuterInvoiceHeader.sold_to_cust_seq) = Returns.ReturnMemberID
--on OuterInvoiceHeader.invoice_nbr = Returns.ReturnInvoiceNum

WHERE OuterInvoiceHeader.sold_to_cust_nbr = '000837'
     AND OuterInvoiceHeader.invoice_date BETWEEN '06/01/2016' AND '06/30/2016'
     AND OuterInvoiceHeader.invoice_status = '5804'
     AND OuterInvoiceHeader.invoice_type = '5601'
GROUP BY CONCAT(SUBSTRING(OuterInvoiceHeader.sold_to_cust_nbr, 1, 6)+'-', OuterInvoiceHeader.sold_to_cust_seq);

基本上你需要记住 SQL 语句的执行顺序:

  1. FROM 子句
  2. WHERE 子句
  3. GROUP BY 子句
  4. HAVING 子句
  5. SELECT 子句
  6. ORDER BY 子句

这是一个计算列别名,因此是错误。您应该考虑使用整个表达式而不是

on concat(substring(OuterInvoiceHeader.sold_to_cust_nbr, 1, 6) + '-', OuterInvoiceHeader.sold_to_cust_seq) = Returns.ReturnMemberID

而不是 on OuterInvoiceHeader.MemberID = Returns.ReturnMemberID。同样,更改您使用相同别名的任何其他地方。您可以而且应该只在外部查询中使用该别名,而不是在同一查询中。