2012-11-23 192 views
0

請檢查下表。多列RDLC報告問題

表1

Key Title  Type 
---------------------- 
A1  Test1  A 
A2  Test2  A 
B1  Test1  B 
B2  Test2  B 
B3  Test3  B 
C1  Test1  C 
C2  Test2  C 

表2

Id Name Address  A  B   C 
--------------------------------------------------- 
1 Soham Add1   A1  B1,B3  C2 
2 Varun Add2   A1,A2 B1,B2,B4  C1 

我RDLC報告看起來像ID = 1

Name : Soham 
Address : Add1 
Type : A 
     A1 - Yes 
     A2 - No 

Type : B 
     B1 - Yes 
     B2 - No 
     B3 - Yes 
     B4 - No 

Type : C 
     C1 - No 
     C2 - Yes 

和報告看起來像ID = 2

Name : Varun 
Address : Add2 
Type : A 
     A1 - Yes 
     A2 - Yes 

Type : B 
     B1 - Yes 
     B2 - Yes 
     B3 - No 
     B4 - Yes 

Type : C 
     C1 - Yes 
     C2 - No 

如果我不使用Sub報表,我如何才能實現單個查詢。 或者我怎樣才能達到這個使用子報告

+0

其中SQL ... MySQL,SQL Server? – DRapp

回答

0

如果你使用MySQL,那麼你可以使用「FIND_IN_SET()」函數,並像這樣應用...它會返回一個結果集以正確的順序爲您的RDLC通過簡單的數據分組...

select 
     PreQuery.* 
    from 
     (select 
       t2.id, 
       t2.`name`, 
       t2.Address, 
       t1.`type`, 
       t1.`key`, 
       case when FIND_IN_SET(t1.`key`, t2.A) > 0 then 'yes' else 'no ' end HasKey 
      from 
       table1 t1, 
       table2 t2 
      where 
       t1.`type` = 'A' 
     UNION  
     select 
       t2.id, 
       t2.`name`, 
       t2.Address, 
       t1.`type`, 
       t1.`key`, 
       case when FIND_IN_SET(t1.`key`, t2.B) > 0 then 'yes' else 'no ' end HasKey 
      from 
       table1 t1, 
       table2 t2 
      where 
       t1.`type` = 'B' 
     UNION  
     select 
       t2.id, 
       t2.`name`, 
       t2.Address, 
       t1.`type`, 
       t1.`key`, 
       case when FIND_IN_SET(t1.`key`, t2.C) > 0 then 'yes' else 'no ' end HasKey 
      from 
       table1 t1, 
       table2 t2 
      where 
       t1.`type` = 'C') PreQuery 
    order by 
     PreQuery.`name`, 
     PreQuery.`type`, 
     PreQuery.`key` 

如果您使用的是SQL-Server中,改變從FIND_IN_SET()來CHARINDEX()

現在函數調用,即使上面的查詢會得到你什麼你想要什麼,你有什麼是非常糟糕的數據庫設計。你應該確實有與用戶的密鑰和他們有什麼...比如另外一個表...

UserHasTable 
userHasID int auto-increment 
userid  int   <-- to link to the person 
hasKey  varchar(??) <-- to correspond with the 'key' they have. 

然後,記錄將是每個人會像

UserHasID UserID HasKey 
1   1  A1 
2   1  B1 
3   1  B3 
4   1  C2 
5   2  A1 
6   2  A2 
7   2  B1 
8   2  B2 
9   2  B4 
10   2  C1 

然後,您的查詢可以簡化,而不必明確分解每個A,B,C列