Elexer
Elexer

Reputation: 153

MySQL sum rows by date

I have db with orders i need sum price by date range

id company item date_start date_end price
1 B item1 2021-12-01 2021-12-10 5
2 B item2 2021-12-20 2021-12-25 10
SELECT company, SUM(price) AS company_sum FROM `orders` GROUP BY company

Price 1 day of item1 = 5

Price 1 day of item2 = 10

I need sum with days

Upvotes: 0

Views: 145

Answers (2)

Akina
Akina

Reputation: 42632

I need sum final price, 1 day booking item1 = 5 date_start = 2021-12-01 date_end = 2021-12-10 So, have 10 days, 10 * 5 = 50 – Elexer

SELECT company, 
       SUM(price * (1 + DATEDIFF(date_end, date_start))) AS company_sum 
FROM `orders` 
GROUP BY company

Upvotes: 5

amirsalah
amirsalah

Reputation: 123

Try this

SELECT company, SUM(price) AS company_sum 
FROM `orders` 
WHERE DATE >= '2021-12-01' AND DATE < '2021-12-20'
GROUP BY company

Upvotes: 1

Related Questions