What is wrong with this SQL statement?

September 3, 2009 - 1:03 am

I have a SQL statement that selects seven columns, six of which come from case statements. It returned duplicate rows, so I made it a select distinct. That eliminated the duplicates. Then I decided I wanted to add an eighth column - sum(total). Now the duplicate rows I thought I had eliminated have returned. The sql is still select distinct, but when I have the sum column, it seems as though the distinct aspect is somehow overriden. What is wrong here?
Its just SQL in a SQL processor. My editor will not allow more than one "Distinct" anywhere in the statement. There are no functions and I cannot use Ifs and Ors. Nulls were a problem, but I’m using the case statements to fill Nulls with acceptable values. I’d like a temp table myself, but I’m not authorized to create one.

On some SQL processors you can’t use SUM and Distinct in the same statement. To get around this run your SQL with the Distinct clause and dump the results into a temporary table, then run the sum function against the temp table.

3 Responses to “What is wrong with this SQL statement?”

  1. firstythirsty Says:

    You need to use the distinct clause in the sum function too
    References :

  2. notadummyrat Says:

    On some SQL processors you can’t use SUM and Distinct in the same statement. To get around this run your SQL with the Distinct clause and dump the results into a temporary table, then run the sum function against the temp table.
    References :

  3. JsK Says:

    could be like a null related problem?
    space issue?
    are you calling functions?
    try using if and or statements to cover the duplicates?

    kinda hard for me to answer without actually looking at your database and the actual SQL statement.
    References :

Leave a Reply