Solved

SSRS Chart data and selections with no entries

Posted on 2014-10-02
4
363 Views
Last Modified: 2014-10-03
Hello Experts Exchange
I am developing a SSRS Report that has categories and a sum of Total hours.  I select the data, that is OK, but I have categories that have no data against them but I want them to show up on the chart as a zero value.

I have a table of data that lists all the categories.

Is there a way I can select the categories that have no data for them and set the value to zero and still keep my categories with data too?

Regards

SQLSearcher
0
Comment
Question by:SQLSearcher
  • 2
  • 2
4 Comments
 
LVL 22

Expert Comment

by:Nico Bontenbal
ID: 40358842
I suppose the data is in a SQL Server database. I think the easiest way is to use an outer join in your query to make sure all the categories are always returned in your dataset (with a zero value, if there is no data). This can be done with an outer join. See:
http://technet.microsoft.com/en-us/library/ms187518(v=sql.105).aspx
0
 

Author Comment

by:SQLSearcher
ID: 40358872
Hello Nicobo
This is the SQL I have, but its not working as there is one category that is not coming back as zero.

SELECT   a.[Category], Isnull(SUM(b.[Total Lost Hours]),0) AS [Total Lost Hours]
FROM  [dbo].[MachineLosses_Level1] a       
left outer join MachineLosses_New b on a.[Category] = b.[level 1]
WHERE     ([File Name] IN ('Htr01 (581)', 'Htr04 (584)', 'Htr3 (583)', 'Htr9 (589)', 'UKL (593)'))
GROUP BY a.[Category]
ORDER BY [Total Lost Hours] DESC

Open in new window


Can you see where my error is?

Regards

SQLSearcher
0
 
LVL 22

Accepted Solution

by:
Nico Bontenbal earned 500 total points
ID: 40359169
The outer join makes sure all the categories are in the result. But the where clause filters the empty categories out of the result. You can solve this by using a subquery. Something like this:
select
    [Category], 
    ISNULL([Total Lost Hours],0) AS [Total Lost Hours]
from
    [dbo].[MachineLosses_Level1] cat left join
    (SELECT   a.[Category], Isnull(SUM(b.[Total Lost Hours]),0) AS [Total Lost Hours]
    FROM  [dbo].[MachineLosses_Level1] a       
    left outer join MachineLosses_New b on a.[Category] = b.[level 1]
    WHERE     ([File Name] IN ('Htr01 (581)', 'Htr04 (584)', 'Htr3 (583)', 'Htr9 (589)', 'UKL (593)'))
    GROUP BY a.[Category]
    ) sub on cat.[Category] = sub.[Category]
ORDER BY 
    [Total Lost Hours] DESC

Open in new window


Google for "derived table subquery sql server" for more information on the subject. There is an example on this page:
http://msdn.microsoft.com/en-us/library/ms177634.aspx
It's example I.
0
 

Author Closing Comment

by:SQLSearcher
ID: 40359292
Thank you very much for your help.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sql 2014,  lock limit 5 34
Whats wrong in this query - Select * from tableA,tableA 11 32
insert wont work in SQL 14 22
SQL Server Connection String through a VPN 8 31
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question