SQL Server 2005 Reporting Services: How to count rows that are not null? Any hints for calculating totals?

北战南征 提交于 2019-12-05 17:21:42
=SUM(IIF(IsNothing(Fields!Completed.Value),0,1))

Generally, if you are trying to sum a column value then you must remember that NULL + anything is NULL. So to get around this, you can do something like:

SELECT SUM(coalesce(col1,0)) col1Sum
  FROM your_table
 WHERE <conditions>

What that code does is make sure that if col1 has a NULL value, we make it 0 before trying to sum so that we will always get a valid sum result.

So when doing things like getting report totals, etc., it's important to remember this step when summing up your values.

As for getting records that are NOT NULL, you need to define what that means. Does that mean "every column in the row must be NOT NULL", only certain ones, etc?

In general, you can check a column for not null by issuing:

SELECT *
  FROM your_table
 WHERE col1 IS NOT NULL

You would need to repeat that WHERE clause condition for all columns you want to not be NULL.

In your last comment you say "I really need to be counting (suming?) based on subtotals...if that's even possible"

A way that I work around this in SSRS is to compute a dataset for the report which has columns for my subtotal values. Basically I precalculate the subtotals, and then I have those values available everywhere in my report. This can be done in a report datamart, or in the stored procedure that produces the dataset. Although this is kind of awkward, I find that it is sometimes easier than trying to bend SSRS to my will. Thought I would mention it in case it is useful.

You can execute this code to get a visual for what I mean -

-- use this code to create a sample data set 
-- Note that I am simply stuffing the correct subtotal values in - 
-- but in reality, you would calculate these from your base data
CREATE TABLE SampleDataSet( Salesperson varchar(50), Region varchar(20), 
Country varchar(30), Sales float, RegionSales float, CountrySales float, GrandTotalSales float ) 

INSERT INTO SampleDataSet Values( 'Brown', 'East', 'Canada', 1000.40, 6780.00, 108686.56, 705043.89 ) 
INSERT INTO SampleDataSet Values( 'Smith', 'East', 'Canada', 3420.76, 6780.00, 108686.56, 705043.89 ) 
INSERT INTO SampleDataSet Values( 'Williams', 'East', 'Canada', 2358.84, 6780.00, 108686.56, 705043.89 ) 
INSERT INTO SampleDataSet Values( 'Simons', 'West', 'Canada', 6298.68, 101906.56, 108686.56, 705043.89 ) 
INSERT INTO SampleDataSet Values( 'Miller', 'West', 'Canada', 95607.88, 101906.56, 108686.56, 705043.89 )
INSERT INTO SampleDataSet Values( 'Knight', 'North', 'UK', 596357.33, 596357.33, 596357.33, 705043.89 ) 

-- inspect data 
SELECT * FROM  SampleDataSet

SELECT Region, SUM(Sales) as RegionSales FROM  SampleDataSet
GROUP BY Region

SELECT Country, SUM(Sales) as CountrySales FROM  SampleDataSet
GROUP BY Country

SELECT SUM(Sales) as GrandTotalSales FROM  SampleDataSet
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!