2011-05-02 40 views
1

我想构建一个允许用户自定义报表的报表系统,并且每个用户都可以在数据网格上创建一个自定义报表。从SQL生成覆盖默认值的报表设置

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的。如果用户具有与user_id = null报告相同的报告名称,则仅返回他们的报告。

这里是我想返回的数据集: USER_ID =用户1

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 =用户3

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帮助我需要?如果这样做最好,我愿意修改表格结构。

回答

2

那么,有几种方法可以做到这一点,并在一定程度上取决于你想要如何与数据库无关。这样的事情应该在大多数DBS工作:

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是用户1,用户2,等等

这是否是建表的最佳途径或不是一个不同的问题。如果你真的只想要默认报告,你可能需要一个不同的方法。还要注意,如果表变大,这可能表现不佳。

+0

问题的第二部分,即没有处理的是,User3和User null的reportname ='Default'的记录,如果存在,我只想要User3的报告。 – Chris 2011-05-02 17:29:19

+0

我更新了SQL,我认为现在应该考虑到这一点。 – 2011-05-02 19:46:25