我遇到以下问题,我想知道如何最好地在SQL中解决它。 我有一个如下表:
我想在另一个表中创建如下所示的跨度:
2019-06-24 19:15:05 -> 2019-06-24 23:59:00
2019-06-25 00:00:00 -> 2019-06-26 18:47:39
2019-06-26 19:02:40 -> 2019-06-26 23:59:00
2019-06-27 00:00:00 -> 2019-06-27 23:59:00
2019-06-28 00:00:00 -> 2019-06-28 23:59:00
2019-06-29 00:00:00 -> 2019-06-29 23:59:00
2019-06-30 00:00:00 -> 2019-06-30 23:59:00
2019-07-01 00:00:00 -> 2019-07-01 23:59:00
2019-07-02 00:00:00 -> 2019-07-02 01:58:32
2019-07-02 02:01:10 -> 2019-07-02 23:59:00
2019-07-03 00:00:00 -> 2019-07-03 23:59:00
2019-07-04 00:00:00 -> 2019-07-04 23:59:00
2019-07-05 00:00:00 -> 2019-07-05 01:35:00
因此,基本上,我的跨度需要从FromTimes到ToTimes,并在每天之间的每个午夜使用新行
有人可以让我知道最好的方法吗?我尝试过游标在范围内的每一天进行迭代,但是我认为这不是最好的方法。有人可以给些指导吗?
答案 0 :(得分:1)
我不太明白为什么你会在午夜前一分钟停下来。我将使用递归CTE获得完整的一天:
#pragma comment(lib,"pthreadVC2.lib")
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<pthread.h>
#include<string.h>
#include<time.h>
pthread_t runTheGame;
//***unneccessary code is hidden***
void timerTick()//function for doing countdown
{
for (;;)
{
restTime--;
if (restTime <= 0)
{
pthread_kill(runTheGame,SIGQUIT);//VS doesn't recognize the SIGQUIT
if (score >= goalOfLevel[arcadeLevel - 1])
{
//code is not written yet
}
}
_sleep(1000);
}
}
//***unneccessary code is hidden***
void gameRunning()//Real game loop. Run by pthread_create().
{
//***unneccessary code is hidden***
}
//***unneccessary code is hidden***
void game(int arcade)//function for initializing the game
{
arcadeLevel = arcade;
boardRange = 4;
oversize = 2048;
score = 0;
revive = 0;
doubleScoreOrNot = 1;
if (arcadeLevel > 0)
{
restTime = timeOfLevel[arcadeLevel - 1];
if (passivePower[0] == 1)
{
boardRange++;
}
if (passivePower[1] == 1)
{
revive = 1;
}
if (passivePower[3] == 1)
{
oversize = 1024;
}
if (passivePower[4] == 1)
{
score=goalOfLevel[arcadeLevel-1]/10;
}
if (passivePower[5] == 1)
{
restTime += restTime /20*3;
}
if (passivePower[7] == 1)
{
boardRange--;
doubleScoreOrNot = 2;
}
}
pthread_create(&runTheGame, NULL, gameRunning, NULL);//after all these initialization, real game starts here
}
Here是db <>小提琴。
如果可以超过100天,则需要向查询中添加with cte as (
select fromtime,
(case when datediff(day, fromtime, totime) = 0
then totime
else dateadd(day, 1, convert(date, fromtime))
end) as dayendtime,
totime
from t
union all
select dayendtime as fromtime,
(case when datediff(day, dayendtime, totime) = 0
then totime
else dateadd(day, 1, dayendtime)
end) as dayendtime,
totime
from cte
where dayendtime < totime
)
select fromtime, dateadd(minute, -1, dayendtime)
from cte;
。