首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >运行3层T报表查询

运行3层T报表查询
EN

Stack Overflow用户
提问于 2017-01-05 20:51:39
回答 1查看 75关注 0票数 1

第一个问题所以请原谅任何错误..。

我正试图为一个SSRS报告编写一个SQL查询,当涉及到我的联接时,我完全感到困惑。

背景:我有三张相关的表格

  • Publisher--这本质上是一个人的列表
  • Publisher Reports --这是一个记录列表(与Publisher表相关),详细说明了他们在一个月内完成的工作。
  • Report month --这是与特定月份和年份相关的记录列表(与Publisher Reports表相关)。在这些记录中,它们有一个指标来表明它们是否与前六个月期间有关。

我想做的是得到一个出版商的名单,谁没有提交一个出版商的报告,是相关的报告月记录,在过去6个月。我想要的输出是“on”列中的发行者列表,其中包含下一列中缺少的报表月份。

我真的很纠结怎么做..。我认为这是一个三步的过程.

-步骤1-获取过去6个月中包含的报告月列表

代码语言:javascript
复制
WITH ACTIVE6MREPM AS
(
SELECT          r.jajw_name,
                r.jajw_CalendarDate
FROM            jajw_reportmonthBase r
WHERE           r.jajw_IncludedIn6MonthReport = '1'
),

--STEP 2 - Get list of all publishers
ACTIVEPUBS AS
(
SELECT          c.FullName,
                c.ContactId
FROM            ContactBase c
WHERE           c.statecode = '0'
AND             c.jajw_CongregationAssignment != 640840001 
AND             c.jajw_CongregationAssignment != 640840006 
AND             c.jajw_CongregationAssignment != 640840005
--AND               q.jajw_FieldServiceGroups = (@Field_Service_Group)
),

--STEP 3 - Get List of Publisher Reports for the selected Report Months
RELEVANTREPORTS AS
(
SELECT          r.jajw_reportId AS Publisher_Report_GUID,
                r.jajw_PublisherId AS Publisher_GUID,
                r.jajw_ReportMonthId AS ReportMonth_GUID,
                m.jajw_name AS ReportMonth_Name
FROM            jajw_reportBase r
INNER JOIN      jajw_reportmonthBase m ON r.jajw_ReportMonthId = m.jajw_reportmonthId
WHERE           r.jajw_ReportPeriod6Month = '1'
ORDER BY        m.jajw_CalendarDate

在这三个之后,我想创建我的列表,如上文所述,这是一个让我困惑的地方!任何帮助都将不胜感激!

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-05 21:21:32

我想你可以大大缩短你的代码.这是一次没有测试数据的尝试。确保读取注释并添加联接条件并检查where子句。

代码语言:javascript
复制
with cte as(
SELECT          r.jajw_reportId AS Publisher_Report_GUID,
                r.jajw_PublisherId AS Publisher_GUID,
                r.jajw_ReportMonthId AS ReportMonth_GUID,
                m.jajw_name AS ReportMonth_Name,
                c.FullName,
                c.ContactId
FROM            jajw_reportBase r
INNER JOIN      jajw_reportmonthBase m ON 
                r.jajw_ReportMonthId = m.jajw_reportmonthId
INNER JOIN      ContactBase c on                           --what ever condition is appropiate
WHERE           r.jajw_ReportPeriod6Month = '1'
                and m.jajw_IncludedIn6MonthReport = '1'    --maybe put this here, or does the above do the same thing?
ORDER BY        m.jajw_CalendarDate)

select 
    p2.FullName,
    p2.ReportMonth_Name
from cte p
right join(select distinct 
                ReportMonth_Name, FullName 
            from ContactBase 
            left join jajw_reportmonthBase on 1=1) p2 on p2.FullName = p.FullName and p2.ReportMonth_Name = p.ReportMonth_Name  
where 
    ContactId in (select ContactId from cte group by ContactId having count(distinct ReportMonth_GUID) < 6)
    and p.FullName is null

这里是一个使用测试数据的例子.

代码语言:javascript
复制
declare @pub table (pubname varchar(56), ReportMonthName varchar(16))
insert into @pub (pubname,ReportMonthName) values
('a','Jan'),
('a','Feb'),
('a','Mar'),
('a','Apr'),
--publisher a is missing May and Jun
('b','Jan'),
('b','Feb'),
('b','Mar'),
('b','Jun')
--publisher b is missing Apr and May

declare @dt table (ReportMonthName varchar(16))
insert into @dt (ReportMonthName) values
('Jan'),
('Feb'),
('Mar'),
('Apr'),
('May'),
('Jun')



select 
    p2.pubname
    ,p2.ReportMonthName
from @pub p
right join(
select distinct
    p.pubname
    ,d.ReportMonthName
from @pub p
left join @dt d on 1=1)p2 on p2.pubname = p.pubname and p2.ReportMonthName = p.ReportMonthName where p.pubname is null 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41494497

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档