Use the same data for SQL & Google Sheets calculations but can't understand why in SQL and Google Sheets I have different values for the total. 2 use cases are described below
- I have 2 tables:
- 1st table data columns: ordernumber, sales number, status, month_id, dealsize
- 2nd table data columns: ordernumber, year_id, productline, city, country, territory
- The task is: to find out the number of motorcycle sales (Productline = ‘Motorcycles’) in 2003 by region (Territory)
I wrote such a request and got the answer
SELECT
year_country.year_id year,
year_country.territory territory,
year_country.productline product,
sum(sales_size.sales) total_sales
FROM sales_size
JOIN year_country
ON sales_size.ordernumber = year_country.ordernumber
WHERE year_country.productline = "Motorcycles" AND year_id = "2003"
GROUP by year_country.territory
;
But when I re-checked the calculations just in Google Sheets I found out that the total is wrong. In Google Sheets, I just put the content of both table into one and then used SUMIF formula.
=sumifs([range],[range],"Motorcycles",[range],"NA",[range],"2003")
- APAC = 12058.72
- EMEA = 50956.2
- NA = 65887.91
- Then the same 2 tables
- The task is: to find the average cost of the order (SALES) by year
I wrote such a request and got the answer
SELECT
year_country.year_id year,
round(avg(sales_size.sales)) average_sales
FROM sales_size
JOIN year_country
ON sales_size.ordernumber = year_country.ordernumber
GROUP by year_id
;
But when I re-checked the calculations just in Google Sheets I found out that the average is also wrong. In Google Sheets, I just put the content of both table into one and then used AVERAGEIF formula.
=AVERAGEIF([range],[year],[range])
- 2003 = 5454
- 2004 = 5298
- 2005 = 4958
Source tables