如何使用SSRS中的一组特定数据进行行计数

时间:2016-04-28 19:50:05

标签: sql-server reporting-services

我有一份SSRS报告,显示按财务年度,财务季度和业务类型分组的总收入。我需要添加一个最终列,该列计算构成总收入数的行数,其中每行代表一个业务。

基本上我需要知道特定业务类型中有多少个体业务对该时间段的总数做出了贡献。我将在下面详细解释。

首先是提供报告数据的存储过程:

SELECT 
id
,assess_year
,CASE
    WHEN MONTH(date_received) IN (7,8,9) THEN 1
    WHEN MONTH(date_received) IN (10, 11, 12) THEN 2
    WHEN MONTH(date_received) IN (1, 2, 3) THEN 3
    WHEN MONTH(date_received) IN (4, 5, 6) THEN 3
    WHEN date_received is null THEN 0
    ELSE 0
END as Fiscal_Quarter
,s_gross_revenue
,CASE 
    WHEN LEFT(segment, 1) = 'A' THEN 'Accommodations' 
    WHEN segment BETWEEN 'B100' AND 'B699' THEN 'Restaurant' 
    WHEN segment BETWEEN 'B700' AND 'B999' THEN 'Retail' 
    WHEN LEFT(segment, 1) = 'C' THEN 'Attractions/Recreation' 
    WHEN LEFT(segment, 1) = 'D' THEN 'Travel Services' 
    WHEN LEFT(segment, 1) = 'E' THEN 'Rental Cars' 
    WHEN LEFT(segment, 1) = 'N' THEN 'Other' 
    ELSE 'UNDEFINED'
END as Segement
FROM mytable 
WHERE s_gross_revenue >0 AND assess_year IS NOT NULL 
    AND assess_year <> '' AND segment IS NOT NULL AND segment <>''
ORDER BY ASSESS_YEAR, FISCAL_QUARTER, SEGEMENT

我附加了一张图片(Raw Data Image),以显示该过程返回的原始数据的示例。

我在SSRS报告中按年,季度和业务类型进行所有分组。

我只需要一个公式或分组来计算构成总收入的每个业务的所有行。

我还附上了报告(SSRS Report Image)的图像,其中行列应该是行的空列。

因此,在SSRS报告图像中,业务类型&#34; Accommodations&#34; 2014/15财年和财政季度1在最后一栏中的行数应为4455,因为有4455个个体企业被列为2014/15季度收入的住宿。我根本不知道如何根据SSRS中的特定标准进行行计数。

(另外,我知道SQL中的季度看起来很奇怪,我们的业务年度从7月开始)

1 个答案:

答案 0 :(得分:1)

如果我正确理解您的问题,并且您使用的是SQL Server 2008+,那么您可以使用OVER(...)子句在保留详细数据的同时执行COUNT(...)

对于业务计数,它看起来像:

COUNT(*) OVER (PARTITION BY assess_year, Fiscal_quarter, Segement)

这是完全修改过的查询,但上面的部分确实是唯一的变化:

SELECT 
    id
    ,assess_year
    ,Fiscal_Quarter
    ,s_gross_revenue
    ,Segement
    ,COUNT(*) OVER (PARTITION BY assess_year, Fiscal_quarter, Segement) BusinessCount
FROM (
        SELECT 
            id
            ,assess_year
            ,CASE
                WHEN MONTH(date_received) IN (7,8,9) THEN 1
                WHEN MONTH(date_received) IN (10, 11, 12) THEN 2
                WHEN MONTH(date_received) IN (1, 2, 3) THEN 3
                WHEN MONTH(date_received) IN (4, 5, 6) THEN 4
                WHEN date_received is null THEN 0
                ELSE 0
            END as Fiscal_Quarter
            ,s_gross_revenue
            ,CASE 
                WHEN LEFT(segment, 1) = 'A' THEN 'Accommodations' 
                WHEN segment BETWEEN 'B100' AND 'B699' THEN 'Restaurant' 
                WHEN segment BETWEEN 'B700' AND 'B999' THEN 'Retail' 
                WHEN LEFT(segment, 1) = 'C' THEN 'Attractions/Recreation' 
                WHEN LEFT(segment, 1) = 'D' THEN 'Travel Services' 
                WHEN LEFT(segment, 1) = 'E' THEN 'Rental Cars' 
                WHEN LEFT(segment, 1) = 'N' THEN 'Other' 
                ELSE 'UNDEFINED'
            END as Segement
        FROM mytable 
        WHERE s_gross_revenue > 0 
            AND ISNULL(assess_year,'') <> '' 
            AND ISNULL(segement,'') <> ''
    ) data
ORDER BY ASSESS_YEAR, FISCAL_QUARTER, SEGEMENT

我在你的where子句中做了一个轻微的语法改变,它是相同的逻辑,但是一种简单的方法来做到这一点。如果SEGEMENTASSESS_YEAR是文本类型,则可以将NOT NULL AND NOT ''ISNULL(...,'') <> ''合并为SEGEMENT,而不是在单独的语句中检查ASSESS_YEARCOALESCE(...)ISNULL(...)值,否则你将不得不使用COALESCE(...)来完成同样的事情。如果愿意,count(<field name>)可以替换为//code get Exif information Metadata metadata = ImageMetadataReader.readMetadata(outputFile); Directory directory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class); if(directory == null) { logger.warn("no EXIF info."); outputFile.delete(); return; } JpegDirectory jpegDirectory = metadata.getFirstDirectoryOfType(JpegDirectory.class); int orientation; try { orientation = directory.getInt(ExifIFD0Directory.TAG_ORIENTATION); if(orientation != 1) { //rotate image int w = jpegDirectory.getImageWidth(); int h = jpegDirectory.getImageHeight(); ImageInformation imageInformation = new ImageInformation(orientation, w, h); AffineTransform affineTransform = getExifTransformation(imageInformation); InputStream pictureStream = new FileInputStream(outputFile); BufferedImage pictureBuffer = ImageIO.read(pictureStream); pictureStream.close(); if (pictureBuffer == null) { logger.warn("The picture buffer parsed is null."); } pictureBuffer = transformImage(pictureBuffer, affineTransform); //code do image transfer public static BufferedImage transformImage(BufferedImage image, AffineTransform transform) throws Exception { AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BICUBIC); BufferedImage destinationImage = op.createCompatibleDestImage(image, null ); Graphics2D g = destinationImage.createGraphics(); g.setBackground(Color.WHITE); g.clearRect(0, 0, destinationImage.getWidth(), destinationImage.getHeight()); destinationImage = op.filter(image, destinationImage); return destinationImage; }

只需在SSRS报告的id列中使用angularjs date time picker,这也完全可以实现。它应该给你相同的结果。