水晶报表的SQL查询

时间:2018-10-15 19:26:08

标签: sql crystal-reports

我很难通过Crystal报表从SQL数据库获取信息。由于无法获得SQL查询来返回所需的数据,因此我正在考虑直接构造查询,而不是让Crystal Reports构造该查询。

我正在使用的表如下所示:


    Table: system.view_episode_summary_current
    +------------+----------------+------------+----------------+------------------------+
    | patient_id | episode_number | admit_date | discharge_date | attending_practitioner |
    +------------+----------------+------------+----------------+------------------------+
    | 111        | 4              | 01/05/2017 |                | 4444                   |
    +------------+----------------+------------+----------------+------------------------+
    | 222        | 8              | 03/17/2017 |                | 5555                   |
    +------------+----------------+------------+----------------+------------------------+
    | 222        | 9              | 03/20/2017 |                | 6666                   |
    +------------+----------------+------------+----------------+------------------------+
    | 333        | 2              | 10/08/2017 |                |                        |
    +------------+----------------+------------+----------------+------------------------+

    Table: system.user_practitioner_assignment
    +------------+----------------+---------------------+--------------------+
    | patient_id | episode_number | backup_practitioner | date_of_assignment |
    +------------+----------------+---------------------+--------------------+
    | 111        | 4              |                     |                    |
    +------------+----------------+---------------------+--------------------+
    | 222        | 8              | 7777                | 03/17/2017         |
    +------------+----------------+---------------------+--------------------+
    | 222        | 8              | 4444                | 05/18/2017         |
    +------------+----------------+---------------------+--------------------+
    | 222        | 9              |                     |                    |
    +------------+----------------+---------------------+--------------------+
    | 333        | 2              | 4444                | 10/08/2017         |
    +------------+----------------+---------------------+--------------------+
    | 333        | 2              | 5555                | 10/19/2017         |
    +------------+----------------+---------------------+--------------------+

我需要SQL查询以从 system.view_episode_summary_current 返回 Patient_id episode_number admit_date 。员工X是 attending_practitioner 最近分配的 backup_practitioner 时,表就会出现。

我已经能够获得Crystal Reports返回以下任一信息:


    +-------------+------------+----------------+------------+
    | {?Staff_ID} | patient_id | episode_number | admit_date |
    +-------------+------------+----------------+------------+
    | 4444        | 111        | 4              | 01/05/2017 |
    +-------------+------------+----------------+------------+

或者这个:


    +-------------+------------+----------------+------------+
    | {?Staff_ID} | patient_id | episode_number | admit_date |
    +-------------+------------+----------------+------------+
    | 4444        | 222        | 8              | 03/17/2017 |
    +-------------+------------+----------------+------------+

但是这不是我需要的结果:


    +-------------+------------+----------------+------------+
    | {?Staff_ID} | patient_id | episode_number | admit_date |
    +-------------+------------+----------------+------------+
    | 4444        | 111        | 4              | 01/05/2017 |
    +-------------+------------+----------------+------------+
    | 4444        | 222        | 8              | 03/17/2017 |
    +-------------+------------+----------------+------------+

我无法弄清楚如何使Crystal Reports OR语句正常工作,并且不确定是否使用了正确的JOIN类型。这就是为什么我要查看直接的SQL查询命令而不是让Crystal Reports构造SQL查询的原因。

更新:

所以我尝试了几件事,但没有成功。

首先,SQL处理程序对查询中的别名有些变幻无常,因此我删除了它们。 (我想如果别名名称的长度至少为3个字符,我可以使用别名,但是我需要对其进行更多测试。)

接下来,它在NULL子句上抛出错误,所以我对其进行了调整,直到通过它们为止。

这是查询当前的样子:

SELECT * 
  FROM (SELECT system.view_episode_summary_current.patient_id, 
               system.view_episode_summary_current.episode_number, 
               system.view_episode_summary_current.admit_date,
               system.view_episode_summary_current.attending_practitioner,
               CASE WHEN (system.user_practitioner_assignment.date_of_assignment IS NOT NULL)
                     AND (LAG(system.user_practitioner_assignment.date_of_assignment) OVER
                             ( PARTITION BY system.user_practitioner_assignment.patient_id
                                   ORDER BY system.user_practitioner_assignment.date_of_assignment
                             ) IS NOT NULL) THEN 
                             LAG(system.user_practitioner_assignment.backup_practitioner) OVER
                                 ( PARTITION BY system.user_practitioner_assignment.patient_id
                                       ORDER BY system.user_practitioner_assignment.date_of_assignment
                                 ) 
                END AS Last_Backup
        FROM system.view_episode_summary_current
       INNER -- WRITE JOINS LIKE THIS!!!
        JOIN system.user_practitioner_assignment
          ON system.view_episode_summary_current.patient_id = system.user_practitioner_assignment.patient_id
         AND system.view_episode_summary_current.episode_number = system.user_practitioner_assignment.episode_number
       ) TMP
 WHERE TMP.attending_practitioner = '4444'
    OR TMP.last_backup = '4444';

现在,它在OVER子句上引发错误。我假设这意味着它已经够老了,它不知道如何处理SQL窗口函数。所以现在我想弄清楚如何使用自连接和MAX()子句重写LAG内容。

我是SQL的新手。例如,根据我观察到的情况,我假设SQL查询中的大写字母(大部分)是不相关的,但我不确定。我一直在互联网上寻求帮助,但没有发现任何可以使我工作或理解的有用信息(在这一点上,大多数感觉像是外语)。我在工作内部和外部都知道SQL的人很困惑,但这是事实。我不在这里,请帮忙。

1 个答案:

答案 0 :(得分:0)

您应使用显式联接语法(请参见粗体字)。

但是,这样的事情可能会解决问题:

SELECT * 
  FROM (SELECT a.patient_id, 
               a.episode_number, 
               a.admit_date,
               a.attending_practitioner,
               CASE WHEN B.date_of_assignment IS NOT NULL
                     AND LAG(B.date_of_assignment) OVER
                             ( PARTITION BY B.patient_id
                                   ORDER BY B.date_of_assignment
                             ) IS NOT NULL THEN 
                             LAG(B.backup_practitioner) OVER
                                 ( PARTITION BY B.patient_id
                                       ORDER BY B.date_of_assignment
                                 ) 
                END AS Last_Backup
        FROM system.view_episode_summary_current a
       INNER -- WRITE JOINS LIKE THIS!!!
        JOIN system.user_practitioner_assignment b
          ON a.patient_id = b.patient_id
         AND a.episode_number = b.episode_number
       ) TMP
 WHERE TMP.attending_practitioner = '4444'
    OR TMP.last_backup = '4444';