0

I should find out the dates of each customers' LAST orders.

select 
    Dimension.Customer.[Primary Contact],
    Fact.Sale.[Invoice Date Key],
    cast(Fact.sale.[Invoice Date Key] as nvarchar) as castt
from 
    Dimension.Customer
inner join 
    Fact.Sale  on Dimension.Customer.[Customer Key]= Fact.Sale.[Customer Key]

group by 
    Dimension.Customer.[Primary Contact], Fact.sale.[Invoice Date Key]
order by 
    Dimension.Customer.[Primary Contact]

But instead I get this message :

Conversion failed when converting the nvarchar value 'Unknown' to data type int

What am I doing wrong?

  • 1
    My guess is that either Customer or Customer Key is a varchar datatype and you are joining on those two columns. Also as has been stated in many of your recent questions, three part naming in the list of columns has been deprecated. Do yourself a favor and use aliases in your query. It will be much clearer and require a lot less typing. – Sean Lange Aug 22 '22 at 13:07
  • 1
    Does this answer your question? [Get top 1 row of each group](https://stackoverflow.com/questions/6841605/get-top-1-row-of-each-group) – Thom A Aug 22 '22 at 13:10
  • 1
    I suppose if they keep ignoring the advice of others, then they can expect to not get advice/answers from others, @SeanLange ... – Thom A Aug 22 '22 at 13:18
  • Dimension.Customer.Customer and Fact.Sale.[Customer Key] is of the same data type? – Laxmikant Aug 22 '22 at 13:28
  • @SeanLange I only used once three part naming and after getting comment i changed it. There is no tpn here ((. – Javid Hesenov Aug 24 '22 at 05:57
  • @Larnu i will check out get top 1 row... answers. – Javid Hesenov Aug 24 '22 at 05:59
  • 1
    @Laxmikant Dimension.Customer.Customer and Fact.Sale.[Customer Key] is wrong Dimension.Customer.[Customer Key]and Fact.Sale.[Customer Key] is right one. I have already changed it. As for the data type, yes it is. – Javid Hesenov Aug 24 '22 at 06:06
  • 1
    @Larnu i found the answer there. Appreciate it. – Javid Hesenov Aug 24 '22 at 06:23

0 Answers0