sql从表中选择日期范围条件

时间:2019-02-06 13:52:49

标签: mysql sql

我有这张桌子

CREATE TABLE `tarif` (                  
  `tarif_id` int(11) NOT NULL AUTO_INCREMENT,
  `start_tarif` date NOT NULL,
  `end_tarif` date NOT NULL,
  `day_tarif` varchar(50) NOT NULL,
  PRIMARY KEY (`tarif_id`)
);

INSERT INTO `tarif` VALUES (1, '2019-02-01', '2019-02-10', '10'),
                           (2, '2019-02-11', '2019-02-20', '20'),
                           (3, '2019-02-21', '2019-02-28', '10'),
                           (4, '2019-03-01', '2019-02-10', '15');

如何获取day_tarif2019-02-05之间的2019-02-15并计算sum = day_tarif * number of date betwwen 2019-02-05 and 2019-02-15

5 个答案:

答案 0 :(得分:1)

select SUM(day_tarif) * COUNT(tarif_id) 
from tarif
where end_date >= '2019-02-05' AND start_date <= '2019-02-15'

这个问题已经问过好几次了。请确保在搜索重复项之前先搜索StackOverflow:SQL query to select dates between two dates

答案 1 :(得分:1)

您可以使用DATEDIFF使用以下解决方案:

SELECT SUM((DATEDIFF(LEAST(`end_tarif`, '2019-02-15'), GREATEST(`start_tarif`, '2019-02-05')) + 1) * `day_tarif`) AS sumCustom
FROM `tarif` 
WHERE `end_tarif` >= '2019-02-05' AND `start_tarif` <= '2019-02-15'
  

demo on dbfiddle.uk

<罢工> 您可以使用SUMCOUNT使用以下解决方案:

SELECT SUM(`day_tarif`) * COUNT(`tarif_id`) 
FROM `tarif`
WHERE `end_tarif` >= '2019-02-05' AND `start_tarif` <= '2019-02-15'

答案 2 :(得分:1)

尝试一下!

select day_tarif , sum(day_tarif * count (*)) as sum
where start_tarif between '2019/02/05' and '2019/02/15' and end_tarif between '2019/02/05' and '2019/02/15'
group by day_tarif;

答案 3 :(得分:1)

您可以尝试以下代码:

 WITH cte AS(
 SELECT *
 FROM tablename
 WHERE end_tarif >= '2019-02-05' AND start_tarif <= '2019-02-15'
 )
 SELECT day_tarif, day_tarif * COUNT(tarif_id) AS 'SUM'
 FROM cte
 group by day_tarif;

答案 4 :(得分:1)

我想这可能就是你想要的...

Declare @tarif as table (                  
  tarif_id int NOT NULL ,
  start_tarif date NOT NULL,
  end_tarif date NOT NULL,
  day_tarif varchar(50) NOT NULL
);

INSERT INTO @tarif VALUES (1, '2019-02-01', '2019-02-10', '10'),
                           (2, '2019-02-11', '2019-02-20', '20'),
                           (3, '2019-02-21', '2019-02-28', '10'),
                           (4, '2019-03-01', '2019-02-10', '15');

-- Declare parameters
Declare @paramstart date, @paramend date
Set @paramstart='2019-02-05'
Set @paramend='2019-02-15'

-- Set up loop
Declare @mincount int, @maxcount int, @myval int, @curstart date, @curend date,@curtarif int, @mytarif int

Set @mincount=(Select MIN(tarif_id) from @tarif where  end_tarif >= '2019-02-05' AND start_tarif <= '2019-02-15')
Set @maxcount=(Select Max(tarif_id) from @tarif where  end_tarif >= '2019-02-05' AND start_tarif <= '2019-02-15')
Set @mytarif=0


-- Do loop
WHile @mincount<=@maxcount
BEGIN

Set @curstart=(Select start_tarif from @tarif where  tarif_id=@mincount)
Set @curend=(Select end_tarif from @tarif where  tarif_id=@mincount)
Set @curtarif=(Select cast(day_tarif as int) from @tarif where  tarif_id=@mincount)

IF @paramstart between @curstart and @curend 
    BEGIN
    Set @mytarif=@mytarif+((DATEDIFF(day,@paramstart,@curend)+1) * @curtarif)
    END

IF @paramend between @curstart and @curend 
    BEGIN
    Set @mytarif=@mytarif+((DATEDIFF(day,@curstart,@paramend)+1) * @curtarif)
    END

IF @paramstart not between @curstart and @curend  and @paramend not between @curstart and @curend
    BEGIN
    Set @mytarif=@mytarif+((DATEDIFF(day,@curstart,@curend)+1) * @curtarif)
    END

Set @mincount=@mincount+1
END

Select @mytarif as tarif