Search code examples
sqlssmsaggregate-functions

Ecommerce dataset - need assistance solving a question "what is the average time to ship per country?"


new to SQL and trying some different capabilities with practice datasets. Currently I am trying to find the number of days between the 'order date' for a package and the 'shipping date' and then finding the average difference per country. ("what is the average time to ship per country?)

I can create a new column with the number of days btwn 'order date' and 'shipping date' with the datediff function but unsure how to proceed when then trying to find the average of those days per country.

This is all I could come up with, but obviously doesn't work. Throws code:

Msg 195, Level 15, State 10, Line 73 'AVG' is not a recognized built-in function name.

Unsure what or how to even ask this question though I know its been asked before. Thanks in advance!

Select Country, AVG(DATEDIFF(day,[Order Date],[Shipping Date]) as DaysToShip)
From PortfolioProject..Ecommerce
Group by Country

(Working in Microsoft SSMS)


Solution

  • Select Country, 
    AVG(DATEDIFF(day,[Order Date],[Shipping Date]))as DaysToShip
    From your_table
    Group by Country