By that time no function transformation, the great god answers,
CodePudding user response:
SELECTT1. Brand, t1. TotalDays - IFNULL (t2) midDays, 0) days
The FROM
(SELECT
T1. Brand,
DATEDIFF (MAX (t1) enddate), MIN (t1. Startdate)) + 1 totalDays
The FROM t_sales t1
GROUP BY t1. Brand) t1
LEFT the JOIN (
The SELECT t.b rand, MIN (t.m idDays) midDays FROM
(SELECT
T1. Brand,
(CASE WHEN t1. Enddate & lt; T2. Startdate AND t1. Id & lt;> T2. Id THEN DATEDIFF (t2) startdate, t1, enddate) - 1 the ELSE 0 end) midDays
The FROM t_sales t1 JOIN t_sales t2 ON t1. Brand=t2. Brand
) t WHERE t.m idDays!=0 GROUP BY t.b rand
) t2 ON t1. Brand=t2. Brand