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

不想你离开。 提交于 2019-12-07 07:06:57

问题


Using Reporting Services in SQL Server 2005: Is there a way to count only records that are not null; similar to "COUNTA" in Excel? I would think this would be very simple process, but nothing I have tried has worked. For example, I have tried using the following expression for "Completed", which is one column I am trying to count: =count(IIF(Fields!Completed.Value="END")) However, this throws the "wrong number of arguments" error. "Completed" will have a value of "End" or be null.

If necessary, I can try to work this into my SQL query, but the query is already incredibly complicated.

Also, I've found very little documentation for how to calculate report totals, and how to total from groups. Would anyone have any recommendations on what to use as a reference?

Update: upon further inspection, the expression =SUM(IIF(IsNothing(Fields!Completed.Value),0,1)) DOES indeed return the appropriate number of records. I made the mistake of thinking that the report would tally up the number of records in the actual report with "end" for a value. Since the report groups on "Completed", "End" only shows up once in the report for each unique ID (also being grouped on, above Completed). So I really need to be counting (suming?) based on subtotals...if that's even possible.


回答1:


=SUM(IIF(IsNothing(Fields!Completed.Value),0,1))



回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/3038738/sql-server-2005-reporting-services-how-to-count-rows-that-are-not-null-any-hin

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