2011-05-02 67 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