2012-11-23 2 views
0

아래 표를 확인하십시오.여러 열의 RDLC 보고서 문제

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 내 RDLC 보고서는 모양을

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

에 대한 식

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 

와 이드 = 2

모양을 보고서 1
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 

하위 보고서를 사용하지 않는 경우 어떻게 단일 쿼리를 수행 할 수 있습니까? 또는 하위 보고서를 사용하여 어떻게 달성 할 수 있습니까?

+0

어느 SQL ... MySQL, SQL-Server ??? – DRapp

답변

0

MySQL을 사용하는 경우 "FIND_IN_SET()"함수를 사용하여 다음과 같이 적용 할 수 있습니다. 올바른 순서로 결과 집합을 하나씩 반환합니다 당신은 SQL-Server를 사용하는 경우, 간단한 데이터 그룹화를 통해 ...

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` 

은, 위의 쿼리를 얻을 것이다, 비록

이제 CHARINDEX()에 FIND_IN_SET()에서 함수 호출을 변경하려면 무엇을 당신이 가지고있는 것은 정말 나쁜 데이터베이스 디자인입니다. 당신은 실제로 각 사람이 그 다음

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 

같은 것에 대한

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

그런 기록이 될 것이다 ... 사용자의 키와 같은 그들이 가지고 ... 또 다른 테이블이 있어야합니다 , 각 A, B, C 열을 명시 적으로 구분할 필요없이 쿼리를 단순화 할 수 있습니다