Je dois calculer un champ de date au moment de l'exécution sur la base de 2 autres champs.
---------------------------------------------------------- Run Date || Schedule || Next Bill run date -----------------------------[enter image description here][1]----------------------------- ---------------------------------------------------------- 11/29/2018 || 1st || 12/01/2018 ---------------------------------------------------------- 11/29/2018 || 15th || 12/15/2018 ---------------------------------------------------------- 12/02/2018 || 1st || 12/01/2019 ---------------------------------------------------------- 12/02/2018 || 15th || 12/15/2018 ---------------------------------------------------------- 12/16/2018 || 15th || 01/15/2019 ----------------------------------------------------------
Dans le scénario ci-dessus Facture suivante La date d'exécution doit être renseignée en fonction des 2 colonnes précédentes. Il y a 4 conditions sont là,
si le jour (Run_date) entre le 2 et le 15 du même mois et que la valeur de la planification est le 15, alors "Next Bill Run" sera le 15 du même mois.
si le jour (Run_date) entre le 16 (même mois) et le 1er (le mois prochain) et la valeur de la planification est le 15, alors "Next Bill Run" sera le 15 du mois suivant.
si le jour (Run_date) entre le 2 et le 15 du même mois et que la valeur de la planification est "1st", alors "Next Bill Run" sera le 1er du mois suivant.
si le jour (Run_date) entre le 16 (même mois) et le 1er (le mois prochain) et que la valeur de la planification est "1st", alors "Next Bill Run" sera le premier du mois suivant.
Veuillez consulter le tableau ci-dessus pour une meilleure compréhension. S'il vous plaît, aidez-moi à trouver une logique pour implémenter "Next Bill Run". J'apprécie votre aide.
Je pense utiliser la fonction 'case' pour obtenir la datepart () de la date d'exécution. Mais toujours confus avec la mise en œuvre
3 Réponses :
J'espère que cette requête ci-dessous vous aidera.
declare @firstDate as date = '01-'+month(getdate())+'-'+year(getdate()); select run_date,schedule, case when run_date between @firstDate and DATEADD(DAY,14,@firstDate) and schedule ='15th' then DATEADD(DAY,14,@firstDate) when run_date between DATEADD(DAY,15,@firstDate) and DATEADD(MONTH, 1, @firstDate) and schedule ='15th' then DATEADD(MONTH, 1, DATEADD(DAY,14,@firstDate)) when run_date between DATEADD(DAY,14,@firstDate) and schedule ='1st' then DATEADD(MONTH, 1, @firstDate) when run_date between DATEADD(DAY,15,@firstDate) and DATEADD(MONTH, 1 @firstDate) and schedule ='1st' then DATEADD(MONTH, 1 @firstDate) end as next_bill_run_date from your_table
dans SQL Server
set @lastDate := (SELECT LAST_DAY(now()) AS first_day); set @firstDate := (SELECT DATE_ADD(DATE_ADD(LAST_DAY(now()),INTERVAL 1 DAY),INTERVAL - 1 MONTH) AS first_day); select run_date,schedule, case when run_date between @firstDate and DATE_ADD(@firstDate, INTERVAL 14 DAY) and schedule ='15th' then DATE_ADD(@firstDate, INTERVAL 14 DAY) when run_date between DATE_ADD(@firstDate, INTERVAL 15 DAY) and DATE_ADD(@firstDate, INTERVAL 1 MONTH) and schedule ='15th' then date_add(DATE_ADD(@firstDate, INTERVAL 14 DAY), INTERVAL 1 MONTH) when run_date between @firstDate and DATE_ADD(@firstDate, INTERVAL 14 DAY) and schedule ='1st' then DATE_ADD(@firstDate, INTERVAL 1 MONTH) when run_date between DATE_ADD(@firstDate, INTERVAL 15 DAY) and DATE_ADD(@firstDate, INTERVAL 1 MONTH) and schedule ='1st' then DATE_ADD(@firstDate, INTERVAL 1 MONTH) end as next_bill_run_date from your_table
Merci Sandeep, mais je pense que ces codes sont pour MySQL DB. J'utilise actuellement SQL Server. Apprécier ton aide.
Je vais vous donner la requête du serveur SQL. avant cela, est-ce la requête que vous recherchiez?
Selon ma compréhension initiale, ça a l'air bien. Mais je dois le tester une fois avant d'être pleinement confiant. Merci encore pour votre aide.
Merci encore, Sandeep. J'ai codé en dur la valeur de @firstDate comme DATEADD (DAY, 1, EOMONTH (getdate (), - 1)) partout dans le code. Le résultat est toujours nul. Pouvez-vous me guider à ce sujet?
Oui, votre logique est correcte. Utilisez l'instruction case
avec certaines des fonctions DATE
dans SQL SERVER. Avec mes connaissances limitées avec les fonctions de date de SQL SERVER, j'ai encadré la requête ci-dessous. Vous pouvez toujours l'optimiser.
SELECT RUN_DATE,SCHEDULE, case when (DATEPART(month,run_date)=month(sysdatetime()) --checking same month and DATEPART(day, run_date) between 2 and 15 and Schedule='15th') --checking date between 2nd and 15th and schedule is 15th then DATEADD(month, DATEDIFF(month, 0,run_date), 0)+14 --selecting 15th of same month when (DATEPART(month,run_date)=month(sysdatetime()) and DATEPART(day, run_date) between 2 and 15 and Schedule='1st') --checking date between 2nd and 15th and schedule is 1st then DATEADD(month, DATEDIFF(month, 0,run_date)+1, 0) --selecting 1st of next month when (DATEPART(month,run_date)=month(sysdatetime()) or run_date=DATEADD(month, DATEDIFF(month, 0,run_date)+1,0)) and (((DATEPART(day, run_date) between 16 and 31) or (run_date=DATEADD(month, DATEDIFF(month, 0,run_date)+1,0))) and Schedule='1st') then case when (run_date=DATEADD(month, DATEDIFF(month, 0,run_date),0)) then DATEADD(month, DATEDIFF(month, 0,run_date),0) -- if 1st of next month, select first day of same month else DATEADD(month, DATEDIFF(month, 0,run_date)+1,0) --selecting 1st of next month end when (DATEPART(month,run_date)=month(sysdatetime()) or run_date=DATEADD(month, DATEDIFF(month, 0,run_date),0)) and (((DATEPART(day, run_date) between 16 and 31) or (run_date=DATEADD(month, DATEDIFF(month, 0,run_date),0))) and Schedule='15th') then case when (run_date=DATEADD(month, DATEDIFF(month, 0,run_date),0)) then DATEADD(month, DATEDIFF(month, 0,run_date),0)+14 -- if 1st of next month, select 15 of same month else DATEADD(month, DATEDIFF(month, 0,run_date)+1,0)+14 --selecting 15th of next month end end as NEXT_BILL_RUN_DATE FROM mytable ;
J'ai utilisé getdate () à la place de sysdatetime (). Il ne fonctionne toujours pas actuellement et remplit une valeur nulle dans NEXT_BILL_RUN_DATE. Pour être clair, la variable run_date est un type de données «date». Est-ce que cela crée ce problème?
Run_date doit être la colonne de date. La valeur next_bill_run_date est-elle nulle pour toutes les dates ou pour des dates spécifiques? Pouvez-vous indiquer la date pour laquelle il affiche null?
Pour chaque valeur de date, il affiche null. Cependant, j'ai trouvé la solution et publierai la réponse. Merci de votre aide.
Cas
quand planning = '1er'
puis DATEFROMPARTS (DATEPART (année, DATEADD (mois, 1, RUN_DATE)), DATEPART (mois, DATEADD (mois, 1, RUN_DATE)), 01)
quand planning = '15' et DATEPART (jour, RUN_DATE)> = 15
puis DATEFROMPARTS (DATEPART (année, DATEADD (mois, 1, RUN_DATE)), DATEPART (mois, DATEADD (mois, 1, RUN_DATE)), 15)
quand schedule = '15th' et DATEPART (day, RUN_DATE) <15
puis DATEFROMPARTS (DATEPART (année, RUN_DATE), DATEPART (mois, RUN_DATE)), 15)
fin AS NEXT_BILL_RUN_DATE
Bienvenue dans Stack! Pouvez-vous publier le code que vous avez écrit en essayant d'implémenter cette logique? Publiez ce que vous avez et nous vous aiderons à le résoudre.