I'm trying something like:
INSERT INTO MyTable (
Col1
,Col2 )
OUTPUT DISTINCT -- issue is with DISTINCT
INSERTED.Col1
,@otherParameter
INTO IdListTable
SELECT ColA
,ColB
,SUM(ImportantNumber)
FROM MyOtherTable
GROUP BY ColA, ColB
Except SQL doesn't want me to use DISTINCT
in the OUTPUT
clause. The workaround I thought of was to create a temp table for the output, then INSERT DISTINCT
into the IdListTable
. Any ideas on a different workaround?
By using the WHERE clause with a DISTINCT clause in MySQL queries, we are putting a condition on the basis of which MySQL returns the unique rows of the result set.
The SELECT DISTINCT statement is used to return only distinct (different) values. Inside a table, a column often contains many duplicate values; and sometimes you only want to list the different (distinct) values.
This is why I get nervous about use of " distinct " - the spraddr table may include additional columns which you should use to filter out data, and " distinct " may be hiding that. Also, you may be generating a massive result set which needs to be filtered by the "distinct" clause, which can cause performance issues.
Answer. Yes, the DISTINCT clause can be applied to any valid SELECT query.
Replace IdListTable with a temporary table (or a table variable depending upon the number of rows) in the Output statement. Then run a second Insert statement into IdListTable from the temporary table with a Select Distinct.
INSERT INTO MyTable (
Col1,
Col2 )
OUTPUT
INSERTED.Col1,
@otherParameter
INTO #tempIdListTable
SELECT ColA,
ColB,
SUM(ImportantNumber)
FROM MyOtherTable
GROUP BY ColA, ColB
Insert into IdListTable
Select distinct col1, col2 from #tempIdListTable
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With