为什么此访问权限报告会编辑基础查询?

时间:2019-01-07 22:28:54

标签: sql ms-access

我对Access报告有一个奇怪的问题。即,当使用报告时,它将编辑其源查询的代码,并中断查询。基础查询开始于:

select b.Cont_staff, b.tot_success, c.tot_fail
from
(select Cont_staff, count(Cont_staff) as tot_success
from
    (SELECT Cont_date, Cont_result, Cont_staff
    FROM dbo_UIH_contacts
    where dbo_UIH_contacts.Cont_result = 'Successful')
group by Cont_staff) as b
left join 
(select Cont_staff, count(Cont_staff) as tot_fail
from
    (SELECT Cont_date, Cont_result, Cont_staff
    FROM dbo_UIH_contacts
    where dbo_UIH_contacts.Cont_result = 'Unsuccessful') 
group by Cont_staff) as c
on b.cont_staff = c.cont_staff

该报告非常简单,报告详细信息中的Cont_stafftot_successtot_fail中的每个字段都有一个字段。报表标题中有静态标签。

查询在首次创建时可以正确运行,而报表在第一次打开时可以正确运行。但是,如果您关闭报告并重新打开它,它将查询代码更改为:

SELECT b.Cont_staff, b.tot_success, c.tot_fail
FROM 
    (SELECT Cont_staff, count(Cont_staff) AS tot_success 
    FROM 
    [SELECT Cont_date, Cont_result, Cont_staff FROM dbo_UIH_contacts where dbo_UIH_contacts].[Cont_result = 'Successful'] 
    AS [%$##@_Alias] 
GROUP BY Cont_staff)  AS b 
LEFT JOIN 
    (SELECT Cont_staff, count(Cont_staff) AS tot_fail 
    FROM 
    [SELECT Cont_date, Cont_result, Cont_staff FROM dbo_UIH_contacts where dbo_UIH_contacts].[Cont_result = 'Unsuccessful'] 
    AS [%$##@_Alias] 
GROUP BY Cont_staff)  AS c 
ON b.cont_staff = c.cont_staff

在Access中,它是一个没有换行符的文本字符串。更改后的部分放在方括号中,并包含[%$##@_Alias]

更新

问题似乎不是专门针对报告的,设置表单以使用查询时也会发生同样的事情。

2 个答案:

答案 0 :(得分:0)

尝试对最里面的查询使用别名。

select b.Cont_staff, b.tot_success, c.tot_fail
from (select Cont_staff, count(Cont_staff) as tot_success
      from (SELECT Cont_date, Cont_result, Cont_staff
            FROM dbo_UIH_contacts
            where dbo_UIH_contacts.Cont_result = 'Successful') AS Q1
      group by Cont_staff) as b
left join 
     (select Cont_staff, count(Cont_staff) as tot_fail
      from (SELECT Cont_date, Cont_result, Cont_staff
            FROM dbo_UIH_contacts
            where dbo_UIH_contacts.Cont_result = 'Unsuccessful') AS Q2 
      group by Cont_staff) as c
on b.cont_staff = c.cont_staff;

实际上,不确定您是否甚至需要最里面的查询。尝试:

select b.Cont_staff, b.tot_success, c.tot_fail
from (select Cont_staff, count(*) as tot_success
      FROM dbo_UIH_contacts
      where dbo_UIH_contacts.Cont_result = 'Successful'
      group by Cont_staff) as b
left join 
     (select Cont_staff, count(*) as tot_fail FROM dbo_UIH_contacts
      where dbo_UIH_contacts.Cont_result = 'Unsuccessful')
      group by Cont_staff) as c
on b.cont_staff = c.cont_staff;

或者一个交叉表:

TRANSFORM Count(ContDate) AS CountOfContDate
SELECT Cont_Staff
FROM dbo_UIH_contacts
GROUP BY Cont_Staff
PIVOT Cont_result IN ("Successful", "Unsuccessful");

答案 1 :(得分:0)

虽然我没有确定代码覆盖的确切原因,但是如果我将查询分为两个Access查询对象,则不会发生此问题。因此,Access似乎无法处理这种三级查询。

首先创建此查询(具有日期过滤代码,该代码是问题中查询的下一步)。我们称之为qry_Conts_by_dt

SELECT 
    dbo_UIH_contacts.Cont_key
    , dbo_UIH_contacts.Cont_date
    , dbo_UIH_contacts.Cont_result
    , dbo_UIH_contacts.Cont_staff
FROM dbo_UIH_contacts 
WHERE 
    (((dbo_UIH_contacts.Cont_date)>=[Forms]![frm_Cont_status_by_coord]![bx_start_dt] 
    And (dbo_UIH_contacts.Cont_date)<=[Forms]![frm_Cont_status_by_coord]![bx_end_dt])
;

然后引用第一个查询:

SELECT b.Cont_staff, b.tot_success, c.tot_fail
FROM 
    (SELECT Cont_staff, count(Cont_staff) as tot_success
    FROM  qry_Conts_by_dt
    WHERE qry_Conts_by_dt.Cont_result = 'Successful'
    GROUP BY Cont_staff)  AS b 
LEFT JOIN 
    (SELECT Cont_staff, count(Cont_staff) as tot_fail
    FROM  qry_Conts_by_dt
    WHERE qry_Conts_by_dt.Cont_result = 'Unsuccessful' 
    GROUP BY Cont_staff)  AS c ON b.cont_staff = c.cont_staff;

并使用第二个查询作为报告的记录源(或与此相关的表单)。