0

请检查下表。

表格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

如果我不使用子报告,我怎么能在单个查询中实现。或者我如何使用子报告来实现这一点

4

1 回答 1

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 列

于 2012-11-23T15:38:56.383 回答