esafresa esafresa - 12 days ago 6
SQL Question

SQL - An element with the same key '' already exists in the ExpandoObject

I'm testing SQL queries on the the database
http://www.dofactory.com/sql/sandbox

My query is...

select datepart(dd, orderdate), sum(totalamount)
from [order]
group by datepart(dd, orderdate)
order by datepart(dd, orderdate)


But I get the error "Error: An element with the same key '' already exists in the ExpandoObject."

If I remove either the
select datapart(dd, orderdate)
or the
sum(totalamount)
portion the query runs fine. Can someone explain what's going on?

Answer

The error is essentially a duplicate column name error because the columns are both aggregates and do not have names.

Try:

select datepart(dd, orderdate) AS a, sum(totalamount) AS b  
from [order]  
group by datepart(dd, orderdate)  
order by datepart(dd, orderdate)  
Comments