2

我正在 SQL Server 2008 中创建一个 SSRS 报告。结果表根据业务部门进行分组。我的要求是在每组设置后显示总和。

这是查询:

    SELECT 
DATENAME(MONTH, A.scheduledstart) Months
,A.scheduledstart ScheduledStart
,B.name BusinessUnit
,CASE WHEN FC.renre_topbrokername='Yes' THEN 'Broker'
      WHEN FC.renre_typevendorpartner<>0 THEN 'Vendor'
      WHEN FC.renre_typemediacontact<>0 THEN 'Media'
      WHEN FC.renre_typeinvestmentmanager<>0 THEN 'Investment Manager'
      WHEN FC.renre_typeclient<>0 THEN 'Client'
      WHEN FC.renre_typeinvestor<>0 THEN 'Investor'
      WHEN FC.renre_typemitigationresources<>0 THEN 'Mitigation'
      WHEN FC.renre_typeriskaccountingsp<>0 THEN 'Risk/Accounting Services Provider'
      WHEN FC.renre_typeindustryanalyst<>0 THEN 'Industry Analyst'
      WHEN FC.renre_typeratingagencies<>0 THEN 'Rating Agency'
      WHEN FC.renre_typecommunity<>0 THEN 'Community'
      WHEN FC.renre_typeresearchprovider<>0 THEN 'Research Provider'
      WHEN FC.renre_typefederal<>0 THEN 'Federal, State or Local Regulator'
      WHEN FC.renre_typeindustryassociation<>0 THEN 'Industry Association'
      WHEN FC.renre_typeboardmember<>0 THEN 'Board Member'
      WHEN FC.renre_typeinvestmentbank<>0 THEN 'Investment Bank'
 END AttendeeType
,AP.partyidname Name
,CASE WHEN FC.renre_keycontact=0 THEN 'No' ELSE 'Yes' END isKeyContact
,A.owneridname Owner
,COUNT(AP.partyidname) AttendeeCount

from 
FilteredAppointment A JOIN FilteredBusinessUnit B
ON A.owningbusinessunit=B.businessunitid
JOIN FilteredActivityParty AP ON A.activityid = AP.activityid
JOIN FilteredContact FC ON FC.contactid = AP.partyid

WHERE 
A.scheduledstart='2014-05-31 15:00:00.000'
and
AP.participationtypemask=6

group by B.name,A.scheduledstart,AP.partyidname,
CASE WHEN FC.renre_topbrokername='Yes' THEN 'Broker'
      WHEN FC.renre_typevendorpartner<>0 THEN 'Vendor'
      WHEN FC.renre_typemediacontact<>0 THEN 'Media'
      WHEN FC.renre_typeinvestmentmanager<>0 THEN 'Investment Manager'
      WHEN FC.renre_typeclient<>0 THEN 'Client'
      WHEN FC.renre_typeinvestor<>0 THEN 'Investor'
      WHEN FC.renre_typemitigationresources<>0 THEN 'Mitigation'
      WHEN FC.renre_typeriskaccountingsp<>0 THEN 'Risk/Accounting Services Provider'
      WHEN FC.renre_typeindustryanalyst<>0 THEN 'Industry Analyst'
      WHEN FC.renre_typeratingagencies<>0 THEN 'Rating Agency'
      WHEN FC.renre_typecommunity<>0 THEN 'Community'
      WHEN FC.renre_typeresearchprovider<>0 THEN 'Research Provider'
      WHEN FC.renre_typefederal<>0 THEN 'Federal, State or Local Regulator'
      WHEN FC.renre_typeindustryassociation<>0 THEN 'Industry Association'
      WHEN FC.renre_typeboardmember<>0 THEN 'Board Member'
      WHEN FC.renre_typeinvestmentbank<>0 THEN 'Investment Bank'
 END
 ,FC.renre_keycontact
 ,CASE WHEN FC.renre_keycontact=0 THEN 'No' ELSE 'Yes' END 
 ,A.owneridname

这是输出:

在此处输入图像描述

在输出的第 8 行,我需要在 column 下打印该组中的记录总数(即 7 条)AttendeeCount。可以有多个组集,因此在这种情况下,总计数将在每个组之后。

请帮忙。

4

2 回答 2

2

您应该保持当前的查询数据集,并将分组和聚合构建到 SSRS 报告本身中。

您已经从数据库中查询了数据,您现在只需要在报告中使用该数据...

创建一个报告并添加一个表格(或矩阵,如果需要,但看起来不像)。使用上面的查询创建数据集。将表链接到数据集,然后通过右键单击表并根据您的分组要求添加分组来添加组。然后,SSRS 可以自动为您添加小计等。

于 2013-06-27T08:10:03.750 回答
2

在 SQLServer2008 中用于操作结果集,您可以将 GROUP BY 子句与 GROUPING SETS 一起使用

SELECT DATENAME(MONTH, A.scheduledstart) Months
       ,A.scheduledstart ScheduledStart
       ,B.name BusinessUnit
       ,CASE WHEN FC.renre_topbrokername='Yes' THEN 'Broker'
             WHEN FC.renre_typevendorpartner<>0 THEN 'Vendor'
             WHEN FC.renre_typemediacontact<>0 THEN 'Media'
             WHEN FC.renre_typeinvestmentmanager<>0 THEN 'Investment Manager'
             WHEN FC.renre_typeclient<>0 THEN 'Client'
             WHEN FC.renre_typeinvestor<>0 THEN 'Investor'
             WHEN FC.renre_typemitigationresources<>0 THEN 'Mitigation'
             WHEN FC.renre_typeriskaccountingsp<>0 THEN 'Risk/Accounting Services Provider'
             WHEN FC.renre_typeindustryanalyst<>0 THEN 'Industry Analyst'
             WHEN FC.renre_typeratingagencies<>0 THEN 'Rating Agency'
             WHEN FC.renre_typecommunity<>0 THEN 'Community'
             WHEN FC.renre_typeresearchprovider<>0 THEN 'Research Provider'
             WHEN FC.renre_typefederal<>0 THEN 'Federal, State or Local Regulator'
             WHEN FC.renre_typeindustryassociation<>0 THEN 'Industry Association'
             WHEN FC.renre_typeboardmember<>0 THEN 'Board Member'
             WHEN FC.renre_typeinvestmentbank<>0 THEN 'Investment Bank' END AttendeeType
       ,AP.partyidname Name
       ,CASE WHEN FC.renre_keycontact=0 THEN 'No' ELSE 'Yes' END isKeyContact
       ,A.owneridname Owner
       ,COUNT(AP.partyidname) AttendeeCount
FROM FilteredAppointment A JOIN FilteredBusinessUnit B
  ON A.owningbusinessunit=B.businessunitid
    JOIN FilteredActivityParty AP ON A.activityid = AP.activityid
    JOIN FilteredContact FC ON FC.contactid = AP.partyid
WHERE A.scheduledstart='2014-05-31 15:00:00.000'
  AND AP.participationtypemask=6
GROUP BY GROUPING SETS(
  (B.name,A.scheduledstart,AP.partyidname,
   CASE WHEN FC.renre_topbrokername='Yes' THEN 'Broker'
        WHEN FC.renre_typevendorpartner<>0 THEN 'Vendor'
        WHEN FC.renre_typemediacontact<>0 THEN 'Media'
        WHEN FC.renre_typeinvestmentmanager<>0 THEN 'Investment Manager'
        WHEN FC.renre_typeclient<>0 THEN 'Client'
        WHEN FC.renre_typeinvestor<>0 THEN 'Investor'
        WHEN FC.renre_typemitigationresources<>0 THEN 'Mitigation'
        WHEN FC.renre_typeriskaccountingsp<>0 THEN 'Risk/Accounting Services Provider'
        WHEN FC.renre_typeindustryanalyst<>0 THEN 'Industry Analyst'
        WHEN FC.renre_typeratingagencies<>0 THEN 'Rating Agency'
        WHEN FC.renre_typecommunity<>0 THEN 'Community'
        WHEN FC.renre_typeresearchprovider<>0 THEN 'Research Provider'
        WHEN FC.renre_typefederal<>0 THEN 'Federal, State or Local Regulator'
        WHEN FC.renre_typeindustryassociation<>0 THEN 'Industry Association'
        WHEN FC.renre_typeboardmember<>0 THEN 'Board Member'
        WHEN FC.renre_typeinvestmentbank<>0 THEN 'Investment Bank'
   END
   ,FC.renre_keycontact
   ,CASE WHEN FC.renre_keycontact=0 THEN 'No' ELSE 'Yes' END 
   ,A.owneridname), (B.name))
于 2013-06-27T08:58:47.803 回答