【问题标题】:combine multiple results and pick the non-null value , or assign null if all results return null合并多个结果并选择非 null 值,如果所有结果都返回 null,则分配 null
【发布时间】:2017-02-02 14:49:52
【问题描述】:

我有这样的记录:

我想查询并返回这样的报告结果(没有任何可用的 BI 工具,仅使用 SSMS):

我试过了:

select distinct A.Document_Title, A.First_Reviewer, A.Second_Reviewer, A.Third_Reviewer,
(select B.First_review from View as B where B.Title = A.Title) as First_Review,
(select B.Second_review from View as B where B.Title = A.Title) as Second_Review,
(select B.Third_review from View as B where B.Title = A.Title) as Third_Review
From
View A

我收到错误,因为我的子查询返回多个结果,我也尝试使用 Coalesce,但意识到它用于组合多个列。 任何帮助表示赞赏。谢谢。

【问题讨论】:

  • 我根据证据优势删除了plsql标签。
  • 这里使用的逻辑有点好奇。我会复制该视图,并对其进行编辑以准确返回您想要的内容。

标签: sql sql-server tsql reporting


【解决方案1】:

你似乎只需要一个group by

select a.document_title,
       a.first_reviewer, a.second_reviewer, a.third_reviewer,
       max(first_review) as first_review,
       max(second_review) as second_review,
       max(third_review) as third_review
from viewA a
group by a.document_title, a.first_reviewer, a.second_reviewer;

你的观点可能是不正确的。这种聚合应该可能发生在视图中。如果它使用pivot,那么查询可能会留在不必要的列中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    • 2013-05-12
    • 1970-01-01
    • 2019-04-12
    • 2020-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多