【问题标题】:Crystal Reports: Place row data from grouped records into columnsCrystal Reports:将分组记录中的行数据放入列中
【发布时间】:2011-11-02 18:06:46
【问题描述】:

好的,我的 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:房东、建筑师或承包商),信息需要放在相关列中。

不确定执行此操作的最佳方法?任何帮助将不胜感激。

【问题讨论】:

  • 你能改变你的数据源吗?
  • 是的,我也可以访问存储的 proc 和 C# 代码。对我来说,在哪里以及如何获取正确格式的数据是最好的方式?

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


【解决方案1】:

您可以通过使用 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 在第三行。

【讨论】:

  • 所以...我确实最终在水晶公式中使用全局变量来解决我的问题,但在我完成后我看到了你的回复。我什至不知道我可以将 MAX() 与字符串值一起使用。我一直在寻找类似的东西,所以这非常棒。出于好奇,我还是尝试了您的解决方案,效果很好!谢谢!我的实际问题比我最初的示例显示的字段和表连接多得多。我最终不得不使用一个表变量,然后将其连接到另一个表,因为我的一个字段是文本类型,不能用于分组。再次感谢!
【解决方案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 屈服于您的意愿(它不会发生)。

【讨论】:

  • 我真的很想在存储过程中这样做,但是在正确格式化数据时遇到了问题。您的解决方案很好,但我的真实数据有更多字段,例如电话号码,稍后需要格式化。在此处将它们与回车串在一起后尝试修改值会出现问题。无论如何感谢您的回复...我喜欢您的想法!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多