Crystal Reports:将分组记录中的行数据放入列中

时间:2011-11-02 01:05:16

标签: c# visual-studio-2008 crystal-reports

好的,我的Crystal Reports数据源的行如下所示:

|--------+----------+---------+------------+-----------+-----------+---------|
| SiteNo | SiteName | SiteMgr | ContType   | ContName  | ContState | ContZip |
|--------+----------+---------+------------+-----------+-----------+---------|
| 1262   | S. Belt  | Joe B.  | Landlord   | Mike      | CA        | 90017   |
| 1262   | S. Belt  | Joe B.  | Architect  | Paul      | TX        | 77040   |
| 1262   | S. Belt  | Joe B.  | Contractor | Chris     | AZ        | 85016   |
|--------+----------+---------+------------+-----------+-----------+---------|

有数百个站点编号(SiteNo),每个站点编号有三行...报告中的每个记录都需要格式化如下:

|------------+------------+------------+------------|
|    Site    |  Landlord  | Architect  | Contractor |
|------------+------------+------------+------------|
| 1262       | Mike       | Paul       | Chris      |
| S. Belt    | CA         | TX         | AZ         |
| Joe B.     | 90017      | 77040      | 85016      |
|------------+------------+------------+------------|

由于数据源的前三列(SiteNo,SiteName,SiteMgr)对于特定站点始终相同,因此报表按SiteNo分组。我有这个部分想通了。我把它放在组页脚中。然后,我正在努力的部分是,根据联系类型(ContType:房东,建筑师或承包商),信息需要进入相关列。

不确定最好的方法吗?任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

我认为,如果您的数据受到相对较好的约束(如您的示例中所述),那么最佳选择是修改存储过程以返回需要在报告中显示的数据。

例如,一个选项是:

SELECT (SiteNo + CHAR(13) + SiteName + CHAR(13) + SiteMgr) AS SiteDetails ,
       (ContName + CHAR(13) + ContState + CHAR(13) + ContZip) AS LandlordDetails ,
       (SELECT ContName + CHAR(13) + 
               ContState + CHAR(13) + 
               ContZip 
          FROM Contacts 
         WHERE SiteNo = c.SiteNo and ContType = 'Architect') AS ArchitectDetails ,
       (SELECT ContName + CHAR(13) + 
               ContState + CHAR(13) + 
               ContZip 
          FROM Contacts 
         WHERE SiteNo = c.SiteNo and ContType = 'Contractor') AS ContractorDetails 
 FROM Contacts c
WHERE c.ContType = 'Landlord'

此方法检索所有房东详细信息,然后执行子查询以检索其他相关联系人。这是非常具体的问题,可能不是最好的通用解决方案。但是,我已经使用Crystal Reports进行了广泛的处理(比我更关心的事情)并且总是发现在SQL中生成所需的数据可以更容易,而不是试图将Crystal弯曲到您的意愿(它不会发生。)

答案 1 :(得分:2)

你可以通过使用Crystal公式实现这一点,但我认为如果你修改了你的SQL查询会更容易理解和维护,如下所示:

select SiteNo,
       max(SiteName)     SiteName,
       max(SiteMgr)      SiteMgr,
       max(case ContType
               when 'Landlord' then ContName
               else NULL
           end)          LandlordContName,
       max(case ContType
               when 'Landlord' then ContState
               else NULL
           end)          LandlordContState,
       max(case ContType
               when 'Landlord' then ContZip
               else NULL
           end)          LandlordContZip,
       max(case ContType
               when 'Architect' then ContName
               else NULL
           end)          ArchitectContName,
       max(case ContType
               when 'Architect' then ContState
               else NULL
           end)          ArchitectContState,
       max(case ContType
               when 'Architect' then ContZip
               else NULL
           end)          ArchitectContZip,
       max(case ContType
               when 'Contractor' then ContName
               else NULL
           end)          ContractorContName,
       max(case ContType
               when 'Contractor' then ContState
               else NULL
           end)          ContractorContState,
       max(case ContType
               when 'Contractor' then ContZip
               else NULL
           end)          ContractorContZip
from Contacts
group by SiteNo

延长报告的详细信息部分,使其可以有三行,然后放置:

  • SiteNo,LandlordContName,ArchitectContName和ContractorContName在第一行;
  • 第二行的SiteName,LandlordContState,ArchitectContState和ContractorContState;
  • SiteMgr,LandlordContZip,ArchitectContZip和ContractorContZip在第三行。