I want to be able to do this:
INSERT INTO TABLE_1(<LIST OF COLUMNS>)
SELECT <LIST OF ROWS> FROM (SELECT DISTINCT <OTHER COLUMNS> FROM TABLE_2);
How can I do this? I receive an error when I try to do it now.
Note that <LIST OF COLUMNS> is the same in both cases I use it, and also not that the fields in <OTHER ROWS> could, but do not necessarily exist in <LIST_OF COLUMNS>.
OK, a little philosophical treatise here…
“Distinct” means “reject duplicates”. And to detect duplicated rows you have compare them for equality first.
If you just compare rows on all columns, everything is fine: two rows are either equal or they are not, there is no third possibility. So you always know whether a row should be rejected as a duplicate or not.
However, if you try to compare rows by comparing a subset of their columns, you run into trouble: rows that are equal when compared on the subset of their columns may not be equal when compared on all columns. So are they duplicates or not?
So making rows distinct on the subset of columns can only be done:
MAX,COUNT,SUMetc..This is the reason why
GROUP BYmust cover all non-aggregated columns. ADISTINCTis really just aGROUP BYon all columns.