1

ぐぅ..イライラする。教科書では、相互関連サブクエリを使用せずに次のステートメントを書き直すように求めています。

SELECT DISTINCT V.VENDOR_NAME, I.INVOICE_NUMBER, I.INVOICE_DATE, I.INVOICE_TOTAL 
FROM VENDORS V
JOIN INVOICES I 
    ON V.VENDOR_ID = I.VENDOR_ID
WHERE I.INVOICE_DATE IN 
    (SELECT MIN(INVOICE_DATE) FROM INVOICES IV WHERE IV.VENDOR_ID = V.VENDOR_ID)
AND I.INVOICE_NUMBER IN 
    (SELECT MIN(INVOICE_NUMBER) FROM INVOICES IV WHERE IV.VENDOR_ID = V.VENDOR_ID)
ORDER BY V.VENDOR_NAME

どうやってやるの?とにかく、サブクエリなしで書き直すことは考えられません:S

4

2 に答える 2

2

LEFT JOINサブクエリをまったく含まない単純なものとして記述できます。

SELECT DISTINCT V.VENDOR_NAME, I.INVOICE_NUMBER, I.INVOICE_DATE, I.INVOICE_TOTAL 
FROM VENDORS V
JOIN INVOICES I 
    ON V.VENDOR_ID = I.VENDOR_ID
LEFT JOIN INVOICES I2
    ON V.VENDOR_ID = I2.VENDOR_ID
   AND (I2.INVOICE_NUMBER < I.INVOICE_NUMBER OR
        I2.INVOICE_DATE   < I.INVOICE_DATE)
WHERE I2.INVOICE_NUMBER IS NULL;

でテストするSQLfiddle

于 2013-02-20T18:00:16.000 に答える
1

これを試して:

SELECT
    DISTINCT V.VENDOR_NAME,
    I.INVOICE_NUMBER,
    I.INVOICE_DATE,
    I.INVOICE_TOTAL 
FROM VENDORS V
    JOIN INVOICES I 
        ON V.VENDOR_ID = I.VENDOR_ID
    JOIN
    (
        SELECT
            VENDOR_ID,
            MIN(INVOICE_DATE) MIN_INVOICE_DATE,
            MIN(INVOICE_NUMBER) MIN_INVOICE_NUMBER
        FROM INVOICES
        GROUP BY VENDOR_ID
    ) MV
    ON V.VENDOR_ID = MV.VENDOR_ID
       AND I.INVOICE_DATE= MV.MIN_INVOICE_DATE
       AND I.INVOICE_NUMBER = MV.MIN_INVOICE_NUMBER
ORDER BY V.VENDOR_NAME
于 2013-02-20T17:50:19.033 に答える