sql - Crystal 报表 SQL 表达式

标签 sql crystal-reports

我无法弄清楚如何从辅助表中提取值,以用作每个记录的选择标准。

我正在 Windows 7 上使用 Crystal Reports 2011,通过 ODBC 连接到 Oracle 11g 数据库。

我正在创建一个员工目录,该目录利用来自两个位置的信息:

  1. 表:教师
  2. 查看:PVSIS_CUSTOM_TEACHERS

教师表设置有您可预测的字段:id、姓氏、名字、电话、地址、城市、州、邮政编码等。

该 View 具有以下可用字段:

  1. 教师 ID
  2. FIELD_NAME
  3. TEXT_VALUE

我正在使用的数据库应用程序允许我创建与主教师表相关的“自定义字段”。事实上,我创建的字段实际上存储在单独的表中,但随后可以通过 PVSIS_CUSTOM_TEACHERS View 进行访问。由于数据库应用程序允许创建任意数量的“自定义字段”,因此 View 中可以包含任意数量的记录,这些记录可以与教师表中的记录联系起来。

已经创建了许多自定义字段,但就我当前项目而言,只有其中 3 个很重要:

  • empDirSupRecord
  • empDirSupPhone
  • empDirSupAddr

我个人教师记录的 View 如下所示:

TeacherID    Field_Name         Text_Value
1            empDirSupRecord    
1            empDirSupPhone     1
1            empDirSupAddr      1
1            AnotherField       another_value
1            YetAnotherField    yetanother_value

(This would indicate that I've asked for my phone and address to be suppressed, but would still want my name to be included in the directory)

These fields will each contain a '1' if the user has asked that their phone number, or address not be published, or if we need to suppress the entire record altogether.

When I first started my report, I pulled both the table and view into the database expert and linked them together with teachers.id = pvsis_custom_teachers.teacherid. However, this causes each teacher's name to print on the report once for every record with their teacher id in the view. Since that's not the behavior I want, I removed the view from the database expert, and tried using SQL Expression fields to retrieve the contents of the custom field. This is where I'm currently stuck. I would need to write the sql in a way that selects the correctly named field, for each of the teacher records as the record is being processed by the report.

Currently, my sql expressions statement is written as:

(SELECT text_value FROM pvsis_custom_teachers WHERE field_name = 'empDirSupRecord' AND teacherid = '1')

我需要做的是弄清楚如何让报告智能地选择teacherid =(无论当前正在处理什么teacherid)的记录。我不确定 SQL 表达式字段是否是实现此目的的方法,因此如果我当前的方法不起作用,我绝对愿意接受替代建议。

感谢您的浏览。 :-)

最佳答案

您已经快完成 SQL 表达式了。您可以引用带双引号的字段名称的主查询。所以您要寻找的是:

case when "teacher"."id" is null then null
else (SELECT max(text_value)
      FROM pvsis_custom_teachers 
      WHERE field_name = 'empDirSupRecord' AND teacherid = "teacher"."id")
end

请注意,如果没有进行 null 检查和使用 max(),CR 可能会提示,因为它希望确保只返回标量。

另一种可能对性能要求较低的方法是像您首先描述的那样连接表和 View 。然后,您可以按 {teacher.id} 进行分组,并通过变量跟踪 View 中的每个字段名称。但这需要更多的工作和更多的公式。例如这样的事情:

//Place this formula in the Group Header
whileprintingrecords;
stringvar empDirSupRecord:="";

//Place this formula in the Details section
whileprintingrecords;
stringvar empDirSupRecord;
if {pvsis_custom_teachers.field_name} = 'empDirSupRecord'
      then empDirSupRecord:={pvsis_custom_teachers.text_value}

//Place this formula in the Group Footer
whileprintingrecords;
stringvar empDirSupRecord;

关于sql - Crystal 报表 SQL 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14969805/

相关文章:

sql - 统计双方通话次数 "exchanges"

Python 和 sql 服务器

crystal-reports - 将 int 转换为字符串的 Crystal 报告问题

crystal-reports - Crystal Reports 中的条件运行总计

c# - Visual Studio 2010 Crystal 报告 wpf

crystal-reports - 如何抑制 Crystal 报表中条形图上的特定条形?

java - 如何在sql表中自动插入日期和时间

mysql - 使用 Google Cloud SQL 连接 3 个表,其中多个事件与每个表中的同一唯一 ID 相关联

mysql - 将 MySQL 查询转换为 PostgreSQL

asp.net - 数据库错误,供应商代码 17