dt1_a <- sqldf("
SELECT Brand, Year,
sum(SalesQuantity) AS sumQSales,
avg(SalesPrice) AS avgPrice
FROM tSales GROUP BY Brand
")
str(dt1_a)
My guess given just the query is that you are not grouping enough variables.
When you have aggregating functions in your query ( sum
and avg
in this case), all other variables may need to be included in the group by
query. (@G.Grothendieck advised that it is not always necessary. Since I've never been able to get it to work without, I must have stopped trying before finding the exception. Thanks!)
Try this:
dt1_a <- sqldf("
SELECT Brand, Year,
sum(SalesQuantity) AS sumQSales,
avg(SalesPrice) AS avgPrice
FROM tSales GROUP BY Brand, Year
")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.