SQLRNNR (8/18/2014)
halifaxdal (8/18/2014)
unfortunately CTE is not allowed in this particular staging table creation, and even if it is allowed, 10 million records would be a lengthy dump.:crazy:So use a temp table.
Using that case statement (or a udf) in the group by is a recipe for causing your query to perform terribly.
Dumping to a temp table will often solve that.
Sorry I should have mentioned that temp is also not allowed: only "select", no declare, no CTE, no temp, no variables, no ORDER