Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL查询以显示表中包含数据的最新行,或为空_Sql_Oracle - Fatal编程技术网

SQL查询以显示表中包含数据的最新行,或为空

SQL查询以显示表中包含数据的最新行,或为空,sql,oracle,Sql,Oracle,我有一个Oracle DB表结构,其中的行是通过面试过程填充的,每个“客户”都可以多次经历面试过程,即使是在同一天。表结构如下所示: Table: Customers ------------- CustomerId ... Table: Questions ------------- QuestionId | QuestionText 0 | Last Location? 1 | Last Color? 2 | Last Food? 3

我有一个Oracle DB表结构,其中的行是通过面试过程填充的,每个“客户”都可以多次经历面试过程,即使是在同一天。表结构如下所示:

Table: Customers
-------------
CustomerId
...

Table: Questions
-------------
QuestionId | QuestionText
0          | Last Location?
1          | Last Color?
2          | Last Food?
3          | Last Drink?

Table: Answers
-------------
Id | CustomerId | QuestionId | AnswerText | Created_On
1    0            0            Chicago      08/15/2017 7:56:34 AM
2    0            0            Laramie      08/16/2017 9:27:23 AM
3    0            0            Null         08/17/2017 6:34:56 AM
4    0            1            Null         08/15/2017 7:56:34 AM
5    0            1            Green        08/16/2017 9:27:23 AM
6    0            1            Blue         08/17/2017 6:34:56 AM
7    0            2            Pizza        08/15/2017 7:56:34 AM
8    0            2            Null         08/16/2017 9:27:23 AM
9    0            2            Null         08/17/2017 6:34:56 AM
10   0            3            Null         08/15/2017 7:56:34 AM
11   0            3            Null         08/16/2017 9:27:23 AM
12   0            3            Null         08/17/2017 6:34:56 AM
LastAnswer_QueryResult
-------------
Id | CustomerId | QuestionId | AnswerText | Created_On
3    0            0            Null         08/17/2017 6:34:56 AM
6    0            1            Blue         08/17/2017 6:34:56 AM
9    0            2            Null         08/17/2017 6:34:56 AM
12   0            3            Null         08/17/2017 6:34:56 AM
目前,我们通过查询每个问题的最新时间戳数据并显示结果来显示“最后一次访谈”,无论结果是否有值或为空,如下所示:

Table: Customers
-------------
CustomerId
...

Table: Questions
-------------
QuestionId | QuestionText
0          | Last Location?
1          | Last Color?
2          | Last Food?
3          | Last Drink?

Table: Answers
-------------
Id | CustomerId | QuestionId | AnswerText | Created_On
1    0            0            Chicago      08/15/2017 7:56:34 AM
2    0            0            Laramie      08/16/2017 9:27:23 AM
3    0            0            Null         08/17/2017 6:34:56 AM
4    0            1            Null         08/15/2017 7:56:34 AM
5    0            1            Green        08/16/2017 9:27:23 AM
6    0            1            Blue         08/17/2017 6:34:56 AM
7    0            2            Pizza        08/15/2017 7:56:34 AM
8    0            2            Null         08/16/2017 9:27:23 AM
9    0            2            Null         08/17/2017 6:34:56 AM
10   0            3            Null         08/15/2017 7:56:34 AM
11   0            3            Null         08/16/2017 9:27:23 AM
12   0            3            Null         08/17/2017 6:34:56 AM
LastAnswer_QueryResult
-------------
Id | CustomerId | QuestionId | AnswerText | Created_On
3    0            0            Null         08/17/2017 6:34:56 AM
6    0            1            Blue         08/17/2017 6:34:56 AM
9    0            2            Null         08/17/2017 6:34:56 AM
12   0            3            Null         08/17/2017 6:34:56 AM
新的要求是用一个值显示每个问题的最新答案,如果问题从未被回答,则为null。如果我能正确查询,上面示例数据的结果将如下所示:

MostRecentAnswer_QueryResult
Id | CustomerId | QuestionId | AnswerText | Created_On
2    0            0            Laramie      08/16/2017 9:27:23 AM
6    0            1            Blue         08/17/2017 6:34:56 AM
7    0            2            Pizza        08/15/2017 7:56:34 AM
12   0            3            Null         08/17/2017 6:34:56 AM

到目前为止,我能想到的最佳方法是将最旧的行插入临时表,然后在循环中,使用较新的时间戳更新存在的值。完成后,使用最新的时间戳更新所有空值。有没有一种方法可以在不循环和插入临时表的情况下实现这一点?

分析函数来拯救我!按
customerid
questionid
进行分区,并为每个分区中的行分配一个
行编号()。如果我们不必担心
应答文本为空
,我们只需通过在desc
上创建的
订购即可

为了处理
null
,我们首先根据
answertext
是否为null来排序。这可以通过
大小写
表达式轻松实现(见下文)

然后在外部查询中,我们选择
行编号
为1的行(在
客户ID
问题ID
的每个组合中)

您可以尝试:

select MAX(a.id) ID, a.CustomerId , q.QuestionId,LISTAGG(AnswerText,' * ')  WITHIN GROUP (ORDER BY ID) AnswerText, max(a.Created_On) Created_On
    from Questions q
    left join Answers a
    on a.QuestionId = q.QuestionId
    where a.AnswerText is not null
    group by a.CustomerId , q.QuestionId 
    UNION ALL 
    select MAX(a.id) ID, a.CustomerId , q.QuestionId ,LISTAGG(AnswerText,' *')  WITHIN GROUP (ORDER BY ID) AnswerText, max(a.Created_On) Created_On
     from Questions q
    left join Answers a
    on a.QuestionId = q.QuestionId
    where a.AnswerText is  null AND
    Q.QuestionId NOT IN(
         select q.QuestionId
         from Questions q
      left join Answers a
      on a.QuestionId = q.QuestionId
      where a.AnswerText is not null
      group by a.CustomerId , q.QuestionId
    )
    group by a.CustomerId , q.QuestionId 
    ORDER BY QuestionId
之后,您将得到“*”之间的首个名称应答文本

谢谢您关于“时间戳”的注释-我不得不混淆实际的列名,而忘记了它是一个保留字。固定在样本中。