0

特定の月の第 1/第 2/第 3/第 4/最終平日を調べる必要があります。
また、繰り返しをスケジュールするための週末も必要です。

最初の平日を見つけることができます:

SELECT CASE Dayofweek('2012-11-04') 
         WHEN 1 THEN Date_add('2012-11-04', INTERVAL 1 DAY) 
         WHEN 7 THEN Date_add('2012-11-04', INTERVAL 2 DAY) 
         ELSE '2012-11-04' 
       END AS dateStart 

同様に、2 番目の 3 分の 1 などを取得する必要があります。

4

2 に答える 2

0

MySQL で:

CREATE TABLE calendar
(
  date date not null,
  isBus bit
);
ALTER TABLE calendar
ADD CONSTRAINT PK_calendar
PRIMARY KEY(date);


INSERT INTO calendar VALUES ('2012-10-01', 1);
INSERT INTO calendar VALUES ('2012-10-02', 1);
INSERT INTO calendar VALUES ('2012-10-03', 1);
INSERT INTO calendar VALUES ('2012-10-04', 1);
INSERT INTO calendar VALUES ('2012-10-05', 1);
INSERT INTO calendar VALUES ('2012-10-06', 0);
INSERT INTO calendar VALUES ('2012-10-07', 0);
INSERT INTO calendar VALUES ('2012-10-08', 1);
INSERT INTO calendar VALUES ('2012-10-09', 1);
INSERT INTO calendar VALUES ('2012-10-10', 1);
INSERT INTO calendar VALUES ('2012-10-11', 1);
INSERT INTO calendar VALUES ('2012-10-12', 1);
INSERT INTO calendar VALUES ('2012-10-13', 0);
INSERT INTO calendar VALUES ('2012-10-14', 0);
INSERT INTO calendar VALUES ('2012-10-15', 1);
INSERT INTO calendar VALUES ('2012-10-16', 1);
INSERT INTO calendar VALUES ('2012-10-17', 1);
INSERT INTO calendar VALUES ('2012-10-18', 1);
INSERT INTO calendar VALUES ('2012-10-19', 1);
INSERT INTO calendar VALUES ('2012-10-20', 0);
INSERT INTO calendar VALUES ('2012-10-21', 0);
INSERT INTO calendar VALUES ('2012-10-22', 1);
INSERT INTO calendar VALUES ('2012-10-23', 1);
INSERT INTO calendar VALUES ('2012-10-24', 1);
INSERT INTO calendar VALUES ('2012-10-25', 1);
INSERT INTO calendar VALUES ('2012-10-26', 1);
INSERT INTO calendar VALUES ('2012-10-27', 0);
INSERT INTO calendar VALUES ('2012-10-28', 0);
INSERT INTO calendar VALUES ('2012-10-29', 1);
INSERT INTO calendar VALUES ('2012-10-30', 1);
INSERT INTO calendar VALUES ('2012-10-31', 1);
INSERT INTO calendar VALUES ('2012-11-01', 1);
INSERT INTO calendar VALUES ('2012-11-02', 1);
INSERT INTO calendar VALUES ('2012-11-03', 0);
INSERT INTO calendar VALUES ('2012-11-04', 0);
INSERT INTO calendar VALUES ('2012-11-05', 1);
INSERT INTO calendar VALUES ('2012-11-06', 1);
INSERT INTO calendar VALUES ('2012-11-07', 1);
INSERT INTO calendar VALUES ('2012-11-08', 1);
INSERT INTO calendar VALUES ('2012-11-09', 1);
INSERT INTO calendar VALUES ('2012-11-10', 0);
INSERT INTO calendar VALUES ('2012-11-11', 0);
INSERT INTO calendar VALUES ('2012-11-12', 1);
INSERT INTO calendar VALUES ('2012-11-13', 1);
INSERT INTO calendar VALUES ('2012-11-14', 1);
INSERT INTO calendar VALUES ('2012-11-15', 1);
INSERT INTO calendar VALUES ('2012-11-16', 1);
INSERT INTO calendar VALUES ('2012-11-17', 0);
INSERT INTO calendar VALUES ('2012-11-18', 0);
INSERT INTO calendar VALUES ('2012-11-19', 1);
INSERT INTO calendar VALUES ('2012-11-20', 1);
INSERT INTO calendar VALUES ('2012-11-21', 1);
INSERT INTO calendar VALUES ('2012-11-22', 1);
INSERT INTO calendar VALUES ('2012-11-23', 1);
INSERT INTO calendar VALUES ('2012-11-24', 0);
INSERT INTO calendar VALUES ('2012-11-25', 0);
INSERT INTO calendar VALUES ('2012-11-26', 1);
INSERT INTO calendar VALUES ('2012-11-27', 1);
INSERT INTO calendar VALUES ('2012-11-28', 1);
INSERT INTO calendar VALUES ('2012-11-29', 1);
INSERT INTO calendar VALUES ('2012-11-30', 1);

次に、10 月のすべての木曜日 (5) を取得するには:

select @rowid:=@rowid+1, calendar.* from calendar, (SELECT @rowid:=0) as init 
where dayofweek(date) = 5
and month(date) = 10
order by date;

10 月の第 2 木曜日を取得するには:

select * from
(select @rowid:=@rowid+1 as seqNo, calendar.* from calendar, (SELECT @rowid:=0) as init 
where dayofweek(date) = 5
and month(date) = 10
order by date) x
where seqNo = 2;  
于 2012-10-18T14:50:17.867 に答える
0

SQL Server ではDATEPART(dw, your_date)、週末かどうかがわかります。通常、1 は土曜日を意味し、7 は日曜日を意味しますが、サーバーの構成によって変わる可能性があります。datepart 関数について読んで、その仕組みを理解してください

于 2012-10-18T14:26:35.650 に答える