I need your help. I want to show all the column on a table where I can use DISTINCT clause and MIN function.
--Here is a sample code that I've tried but failed to show the expected output
SELECT * FROM TABLE
WHERE DOCUMENT_NUMBER IN (
SELECT DISTINCT DOCUMENT_NUMBER
FROM TABLE
)
and DESIRED_DUE_DATE IN (
SELECT MIN(DESIRED_DUE_DATE)
FROM TABLE
GROUP BY DOCUMENT_NUMBER)
order by DOCUMENT_NUMBER desc;
Expected output should be the DOCUMENT_NUMBER are all distinct or rather no duplicate and the DESIRED_DUE_DATE of all distinct DOCUMENT_NUMBER is at minimum value while showing all the column on the table.
the above code is still showing me duplicate Document_Number .
It is simpler:
SELECT DOCUMENT_NUMBER, MIN(DESIRED_DUE_DATE) DESIRED_DUE_DATE
FROM TABLE
GROUP BY DOCUMENT_NUMBER
ORDER BY DOCUMENT_NUMBER desc;
GROUP BY
returns distinct values of DOCUMENT_NUMBER
.
If you want all the columns of the table:
SELECT t.*
FROM TABLE t INNER JOIN (
SELECT DOCUMENT_NUMBER, MIN(DESIRED_DUE_DATE) DESIRED_DUE_DATE
FROM TABLE
GROUP BY DOCUMENT_NUMBER
) g on g.DOCUMENT_NUMBER = t.DOCUMENT_NUMBER AND g.DESIRED_DUE_DATE = t.DESIRED_DUE_DATE
ORDER BY t.DOCUMENT_NUMBER desc;
Your first condition is unnecessary.
Your second would work with a correlation clause instead of group by
:
SELECT t.*
FROM TABLE t
WHERE t.DESIRED_DUE_DATE = (SELECT MIN(t2.DESIRED_DUE_DATE)
FROM TABLE t2
WHERE t2.DOCUMENT_NUMBER = t.DOCUMENT_NUMBER
)
order by t.DOCUMENT_NUMBER desc;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.