0

我有以下代码可以完美运行。

我需要做的是使用 CTE 的结果作为查询结果中的列将其包含在查询中,但我无法找出包含它的方法。请问有什么想法吗?

我想要的实际上是这样的:

select a, 
b, 
c, 
d,

WITH invoicedates AS (
    SELECT 
        ROW_NUMBER() OVER (ORDER BY Inv_Date DESC) AS RowNumber,
        inv_Date, INVIT_PARTNO
    FROM Invoices join InvoiceItems on invit_invno = inv_no
    WHERE invit_partno  = stock_no and inv_canind <> 'Y' and inv_date >= DATEADD(yy, -1, getdate())
)
SELECT
   AVG(DATEDIFF(DD, O2.Inv_Date, O1.Inv_Date)) AS AverageFrequency
FROM invoicedates O1
LEFT JOIN invoicedates O2
    ON O2.RowNumber = O1.RowNumber + 1

从股票哪里............

给出 abcd 结果的结果输出

4

1 回答 1

0

您应该能够重新排序您使用 CTE 的方式:

    ; WITH invoicedates (RowNumber, inv_Date, INVIT_PARTNO) AS (
    SELECT 
        ROW_NUMBER() OVER (ORDER BY Inv_Date DESC) AS RowNumber,
        inv_Date, INVIT_PARTNO
    FROM Invoices join InvoiceItems on invit_invno = inv_no
    WHERE invit_partno  = stock_no and inv_canind <> 'Y' and inv_date >= DATEADD(yy, -1, getdate())
)

select a, 
b, 
c, 
d,
  AVG(DATEDIFF(DD, O2.Inv_Date, O1.Inv_Date)) AS AverageFrequency
FROM invoicedates O1
LEFT JOIN invoicedates O2
    ON O2.RowNumber = O1.RowNumber + 1
于 2013-01-23T16:16:06.023 回答