【问题标题】:build report settings from sql that override defaults从覆盖默认值的 sql 构建报告设置
【发布时间】:2011-05-03 01:41:13
【问题描述】:

我正在尝试构建一个允许用户自定义报告的报告系统,并且每个用户都可以在数据网格之上创建自定义报告。

 ID GRIDID               USER_ID REPORTNAME  REPORTPAYLOAD CREATED            
--- -------------------- ------- ----------- ------------- ------------------ 
  4 CompleteReportView1  User1   Completed   (CLOB)        4/18/2011 8:40:05  
  6 CompleteReportView1  User1   All         (CLOB)        4/18/2011 8:40:48  
 10 CompleteReportView1          Completed   (CLOB)        4/18/2011 8:40:05  
 12 CompleteReportView1          All         (CLOB)        4/18/2011 8:40:48  
 16 CompleteReportView1          Default     (CLOB)        4/18/2011 9:53:38  
 18 CompleteReportView1  User2   Completed   (CLOB)        4/18/2011 8:40:05  
 20 CompleteReportView1  User2   All         (CLOB)        4/18/2011 8:40:48  
 33 CompleteReportView1  User3   Default     (CLOB)        4/18/2011 9:53:38  

我希望特定用户的报告列表包括他们设置的所有报告,以及 User_Id 为 null 的报告。如果用户的报告名称与 user_id=null 报告相同,则只返回他们的报告。

这是我想要返回的数据集: User_Id = User1

 ID GRIDID               USER_ID REPORTNAME  REPORTPAYLOAD CREATED            
--- -------------------- ------- ----------- ------------- ------------------ 
  4 CompleteReportView1  User1   Completed   (CLOB)        4/18/2011 8:40:05  
  6 CompleteReportView1  User1   All         (CLOB)        4/18/2011 8:40:48  
 16 CompleteReportView1          Default     (CLOB)        4/18/2011 9:53:38  

User_Id = 用户 2

 ID GRIDID               USER_ID REPORTNAME  REPORTPAYLOAD CREATED            
--- -------------------- ------- ----------- ------------- ------------------ 
 16 CompleteReportView1          Default     (CLOB)        4/18/2011 9:53:38  
 18 CompleteReportView1  User2   Completed   (CLOB)        4/18/2011 8:40:05  
 20 CompleteReportView1  User2   All         (CLOB)        4/18/2011 8:40:48  

User_Id = User3

 ID GRIDID               USER_ID REPORTNAME  REPORTPAYLOAD CREATED            
--- -------------------- ------- ----------- ------------- ------------------ 
 10 CompleteReportView1          Completed   (CLOB)        4/18/2011 8:40:05  
 12 CompleteReportView1          All         (CLOB)        4/18/2011 8:40:48  
 33 CompleteReportView1  User3   Default     (CLOB)        4/18/2011 9:53:38  

有人可以帮助我需要的 sql 吗?如果这样做最有意义,我愿意修改表结构。

【问题讨论】:

    标签: sql oracle


    【解决方案1】:

    嗯,有几种方法可以做到这一点,在某种程度上,这取决于您希望成为与数据库无关的程度。像这样的东西应该适用于大多数数据库:

    select *
    from   T
    where  USER_ID = :user
    union
    select *
    from   T
    where  USER_ID is null
    and    REPORTNAME not in (
        select REPORTNAME
        from   T
        where  USER_ID = :user
    ) 
    

    假设T是上表的名称,:user是User1、User2等...

    这是否是构建表格的最佳方式是另一个问题。如果您真的只想要默认报告,您可能需要一种不同的方法。另请注意,如果表变大,这可能效果不佳。

    【讨论】:

    • 问题的第二部分,即没有处理的问题是,对于 User3 和 User null 有 reportname = 'Default' 的记录,我只想要 User3 的报告(如果存在)。跨度>
    • 我更新了 sql,我认为现在应该考虑到这一点。
    猜你喜欢
    • 2015-06-30
    • 2018-02-21
    • 1970-01-01
    • 2016-05-03
    • 1970-01-01
    • 1970-01-01
    • 2011-12-20
    • 1970-01-01
    相关资源
    最近更新 更多