存储为键值对的日期的SQL查询

时间:2016-08-17 16:05:54

标签: sql oracle

我有以下结构的表

// Section 8.4.2 Decision trees in C#

// Listing 8.15 Object oriented decision tree (C#)

abstract class Decision {
  // Tests the given client 
  public abstract void Evaluate(Client client);
}

class DecisionResult : Decision {
  public bool Result { get; set; }
  public override void Evaluate(Client client) {
    // Print the final result
    Console.WriteLine("OFFER A LOAN: {0}", Result ? "YES" : "NO");
  }
}


// Listing 8.16 Simplified implementation of Template method
class DecisionQuery : Decision {
  public string Title { get; set; }
  public Decision Positive { get; set; }
  public Decision Negative { get; set; }
  // Primitive operation to be provided by the user
  public Func<Client, bool> Test { get; set; }

  public override void Evaluate(Client client) {
    // Test a client using the primitive operation
    bool res = Test(client);
    Console.WriteLine("  - {0}? {1}", Title, res ? "yes" : "no");
    // Select a branch to follow
    if (res) Positive.Evaluate(client);
    else Negative.Evaluate(client);
  }
}

static void MainDecisionTrees()
{
  // The tree is constructed from a query
  var tree =
      new DecisionQuery
      {
        Title = "More than $40k",
        // Test is specified using a lambda function
        Test = (client) => client.Income > 40000,
        // Sub-trees can be 'DecisionResult' or 'DecisionQuery'
        Positive = new DecisionResult { Result = true },
        Negative = new DecisionResult { Result = false }
      };

  // Test a client using this tree
  // Create client using object initializer
  var john = new Client {
      Name = "John Doe", Income = 40000, YearsInJob = 1,
      UsesCreditCard = true, CriminalRecord = false 
    };
  tree.Evaluate(john);
}

private static void Main(string[] args)
{
  MainDecisionTrees();
}

如果我想在2000-06-01T:00:00,2010-06-11T15:00:00之间的两个日期之间检索记录,请按如下方式编写查询。

  **JobData**  - Table
---------------------------
    id   - Number    
    key   - Varchar
    value - Varchar



        Data:
    -----------------
    ID            value                key  
-----------------------------------------------   
    21212   2010-06-11T15:00:00      Job_End
    21213   2008-12-01T23:59:00     Job_Start
    21214   2008-12-01T23:59:00     Job_Start
    21215   2018-06-11T15:00:00     Job_End
    21216   2015-06-11T15:00:00     Job_End

但是我得到0条记录,请有人让我知道我失踪的地方。

注意: 数据库是ORACLE

感谢。

2 个答案:

答案 0 :(得分:0)

当然它无法正常工作,因为您要求的值同时为Job_StartJob_End。如果您确实想要在2000-06-01T:00:002010-06-11T15:00:00之间返回所有记录,那么这应该是正确的:

SELECT *
FROM JobData jd
WHERE jd.key IN ('Job_Start', 'Job_End')
  AND jd.value BETWEEN '2000-06-01T00:00:00' AND '2010-06-11T15:00:00'

答案 1 :(得分:-1)

您的查询不正确。

SELECT * FROM  JobData jd
WHERE 
jd.key IN ('Job_Start', 'Job_End')
AND (jd.key = 'Job_Start' and jd.value BETWEEN '2000-06-01T:00:00' AND '2010-06-11T15:00:00')
AND (jd.key = 'Job_End' and jd.value BETWEEN '2000-06-01T:00:00' AND '2010-06-11T15:00:00')

您应该将其更改为

SELECT * FROM  JobData jd
WHERE 
jd.key IN ('Job_Start', 'Job_End')
AND (TO_DATE(jd.value,'YYYY-MM-DDTHH24:MI:SS') BETWEEN TO_DATE('2000-06-01T00:00:00', 'YYYY-MM-DDTHH24:MI:SS') AND TO_DATE('2010-06-11T15:00:00', 'YYYY-MM-DDTHH24:MI:SS'))