Firebird 如何在带有子查询列的 ORDER BY 中使用 IIF
Firebird how to use IIF in ORDER BY with a subquery's column
以下查询不起作用,因为 Firebird (2.1) 无法识别子查询的列。
我如何重写它才能工作?
SELECT I.InvoiceID,
I.PayByDate,
(SELECT FIRST 1 I2.PayByDate
FROM Invoices I2
WHERE I2.OriginalInvoiceID = I.InvoiceID
AND I2.IsDraft < 1
ORDER BY I2.InvoiceID DESC) AS NewPayByDate
FROM Invoices I
WHERE
I.IsDraft < 1
ORDER BY
IIF(NewPayByDate IS NULL,
PayByDate,
NewPayByDate),
I.InvoiceRefNum
使用子查询:
SELECT InvoiceID, PayByDate, NewPayByDate
FROM (SELECT I.*
(SELECT FIRST 1 I2.PayByDate
FROM Invoices I2
WHERE I2.OriginalInvoiceID = I.InvoiceID AND
I2.IsDraft < 1
ORDER BY I2.InvoiceID DESC
) AS NewPayByDate
FROM Invoices I
WHERE I.IsDraft < 1
) I
ORDER BY IIF(NewPayByDate IS NULL, PayByDate, NewPayByDate),
I.InvoiceRefNum;
我还建议在 ORDER BY
:
中使用 COALESCE()
ORDER BY COALESCE(NewPayByDate, PayByDate),
I.InvoiceRefNum;
以下查询不起作用,因为 Firebird (2.1) 无法识别子查询的列。 我如何重写它才能工作?
SELECT I.InvoiceID,
I.PayByDate,
(SELECT FIRST 1 I2.PayByDate
FROM Invoices I2
WHERE I2.OriginalInvoiceID = I.InvoiceID
AND I2.IsDraft < 1
ORDER BY I2.InvoiceID DESC) AS NewPayByDate
FROM Invoices I
WHERE
I.IsDraft < 1
ORDER BY
IIF(NewPayByDate IS NULL,
PayByDate,
NewPayByDate),
I.InvoiceRefNum
使用子查询:
SELECT InvoiceID, PayByDate, NewPayByDate
FROM (SELECT I.*
(SELECT FIRST 1 I2.PayByDate
FROM Invoices I2
WHERE I2.OriginalInvoiceID = I.InvoiceID AND
I2.IsDraft < 1
ORDER BY I2.InvoiceID DESC
) AS NewPayByDate
FROM Invoices I
WHERE I.IsDraft < 1
) I
ORDER BY IIF(NewPayByDate IS NULL, PayByDate, NewPayByDate),
I.InvoiceRefNum;
我还建议在 ORDER BY
:
COALESCE()
ORDER BY COALESCE(NewPayByDate, PayByDate),
I.InvoiceRefNum;