2016-07-27 15 views
1

私は3つの列を含むテーブルを持っています:いくつかのイベントの開始日と終了日とイベントID。テーブルにユニークな時間範囲を挿入します

新しい行を挿入して、開始日と終了日の間の期間がこのイベントの他の期間と交差しないことを確認する必要があります。

たとえば、私はイベントの2つの期間を持っています:1.08.2016から9.08.2016まで、10.08.2016から17.08.2016までです。開始日と終了日が1.08.2016〜17.08.2016の新しい行を挿入できないようにする

JavaScript、C#またはT-SQLでこの検証を行うにはどうすればよいですか?有効

+0

を返します。挿入されるstart_dateの値 max(終了の日の列)が挿入されるかどうかを確認する価値がありますか?すべてのイベントが連続していると仮定します。連続していない場合は、すべてのイベント行をループする必要があります。私は何かすべきことがない場合を除きますか? – sigint

+0

残念ながら、1.08.16-9.08.16と13.08.16-15.08.16のようにギャップが存在する可能性があります。 –

答えて

0
DECLARE @DataX TABLE (
    EventID   INT IDENTITY 
    ,StartDate  DATE 
    ,EndDate  DATE 
    ) 

INSERT INTO @DataX VALUES 
    ('2016-08-01','2016-08-09') 
    ,('2016-08-10','2016-08-17') 

DECLARE @NewStart   DATE = '2016-08-05' 
     ,@NewEnd   DATE = '2016-08-18' 
     ,@NewStartValid  TINYINT 
     ,@NewEndValid  TINYINT 

;WITH Validate 
    AS (
     SELECT SUM(CASE WHEN @NewStart BETWEEN StartDate AND EndDate THEN 1 ELSE 0 END) AS VStart, 
       SUM(CASE WHEN @NewEnd BETWEEN StartDate AND EndDate THEN 1 ELSE 0 END) AS VEnd 
      FROM @DataX 
     ) 

SELECT @NewStartValid = VStart, @NewEndValid = VEnd 
    FROM Validate 

SELECT @NewStartValid, @NewEndValid 

は0であり、無効この例では、開始日は、日付範囲の範囲内であることを示す0>である(結果:1)および終了日がない(結果:0)。

+0

無効とは、> 0ですが、データベースが正しいという前提を採用すると、複数の日付範囲内にある必要があります。私の元の考えは、1 - SUMの極性を逆転させることでした - 有効な1と無効な0を作る。しかし、その後、私は合計> 1について心配し始めました。 – DaveX

+0

これはt-SQLです。 – DaveX

+0

明日これを試してみます、ありがとう! –

0

ここは私のC#の解決策です。次のコントロールでこのフォーム(ウィンドウ)必要がありますを行うには:

  1. X1「のDataGridView」(表をデータの列を参照すること)

  2. X1「NumericUpDownを」(選択するにはイベントID)

  3. X2 "のDateTimePickerは"(開始日と終了日)

  4. はX1 "ボタンは、"(テーブルにイベントを追加するには)

  5. を選択します10

ここでは、コードを持っている:

// This is your table with data in three columns 
DataTable events = new DataTable(); 

// When the program starts 
public Form1() 
{ 
    InitializeComponent(); 

    // Add the columns to the table 
    events.Columns.Add("ID", typeof(int)); 
    events.Columns.Add("Start Date", typeof(DateTime)); 
    events.Columns.Add("End Date", typeof(DateTime)); 

    // Set data and formats 
    dataGridView1.DataSource = events; 
    dataGridView1.Columns[1].DefaultCellStyle.Format = "dd/MM/yyyy"; // The way the date is shown 
    dataGridView1.Columns[2].DefaultCellStyle.Format = "dd/MM/yyyy"; // Ex. 27-07-2016 
} 

// Add The row containing information about the event 
private void btnAdd_Click(object sender, EventArgs e) 
{ 
    // Weather or not the row should be added 
    bool addRow = true; 

    // Skip this if no rows are added yet 
    foreach (DataRow row in events.Rows) 
    { 
     // Row id and dates 
     int eventId = (int)row[0]; 
     DateTime rowStartDate = (DateTime)row[1]; 
     DateTime rowEndDate = (DateTime)row[2]; 

     // If new start date lies inside the timespan of an existing event 
     bool newStartDateBetween = rowStartDate.Date <= dateTimePicker1.Value.Date && rowEndDate.Date >= dateTimePicker1.Value.Date; 

     // If new end date lies inside the timespan of an existing event 
     bool newEndDateBetween = rowStartDate.Date <= dateTimePicker2.Value.Date && rowEndDate.Date >= dateTimePicker2.Value.Date; 

     // If any of these statements are true, show the error and dont add the new row 
     if (eventId == numericUpDown1.Value || newStartDateBetween || newEndDateBetween || dateTimePicker2.Value.Date < dateTimePicker1.Value.Date) 
     { 
      if (eventId == numericUpDown1.Value) 
       MessageBox.Show("Event ID already taken!"); 

      if (newStartDateBetween) 
       MessageBox.Show("The starting date is within the timespan of event ID: " + row[0] + "!"); 

      if (newEndDateBetween) 
       MessageBox.Show("The ending date is within the timespan of event ID: " + row[0] + "!"); 

      if (dateTimePicker2.Value.Date < dateTimePicker1.Value.Date) 
       MessageBox.Show("The ending date is before the starting date!"); 

      // Dont add the row 
      addRow = false; 
      break; 
     } 
    } 

    // Add the row if no errors 
    if (addRow) 
     events.Rows.Add((int)numericUpDown1.Value, dateTimePicker1.Value, dateTimePicker2.Value); 
} 

は、この情報がお役に立てば幸いです。

+0

残念ながら、私はbpm'online(それが何であるか知っていれば)と一緒に働いているので、助けにならないでしょう –

+0

それでは、ロジックを適用できますか? – MasterXD

+0

明日あなたのアイデアを試してみましょう。 –

0

以下は、それは次のようになり、あなたのテーブルに列 `id`、` start_date`と `end_date`を持っていると仮定すると、一緒にあなたの例から、次のいずれかのconfictsに

Declare @Table table (EventID int,StartDate Date,EndDate Date) 
Insert into @Table values 
(1,'2016-08-01','2016-08-09'), 
(2,'2016-08-10','2016-08-17') 

Declare @StartDate Date = '2016-08-05' 
Declare @EndDate Date = '2016-08-18' 

Select * 
     ,ValidStart = IIF(@StartDate Between StartDate and EndDate,0,1) 
     ,ValidEnd = IIF(@EndDate Between StartDate and EndDate,0,1) 
From @Table 
Where @StartDate Between StartDate and EndDate 
    or @EndDate Between StartDate and EndDate 

戻り

EventID StartDate EndDate  ValidStart ValidEnd 
1  2016-08-01 2016-08-09 0   1 
関連する問題