Need to join two tables. One table has a 'datekey' column that is an int in the format YYYYMMDD from the dimdate table, and the other table has a 'CreatedOn' column that is a datetime. I am casting the datetime to a date initially because that is the only way the group by seems to work correctly. I keep getting the error "Operand type clash: date is incompatible with int" because of the "on d.datekey = BBB.datekey" line.. any suggestions on an easy workaround?
select
distinct CONVERT (date,convert(char(8),d.datekey )) as Date
, isnull(BBB.reccount,0) DimAccount
from dimdate d
left outer join(
Select
cast(createdon as date) datekey
, count(*) RecCount
from DimAccount
group by cast(createdon as date)
)BBB on d.datekey = BBB.datekey
where d.datekey like '2017%'
group by d.datekey
,BBB.RecCount
having MAX(DATEDIFF(dd, CONVERT(date, CAST(d.Datekey AS CHAR(8)), 101), GETDATE())) > -1
order by date desc