Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Analysis Server and Reporting Services (2008)
 Displaying empty rows in a report

Author  Topic 

L_i_L_i
Starting Member

6 Posts

Posted - 2013-10-20 : 12:36:16
Hi!

I have a table that looks like this:

[Date] [Time interval] [Number of calls] [PilotID]

20130303 01:00-02:00 34 0100
20130303 01:00-02:00 23 0110
20130303 03:00-04:00 12 0100

etc....

My problem is that there are time intervals with no calls. But I want to show a report with 24 rows for time interval for every day. So if there are no calls, I want to show 0. PilotID and Date will be parameters. PilotID is a parameter with multiple values, so user can pick startdate and enddate and pilot(es). If u pick more than one pilot, the sum of calls will be shown (still 24 rows for a day). I don't know if those empty rows should be made in SQL, I tried Left Outer Join, but I couldn't get it right because of parameter used for PilotID. Any ideas how to deal with this?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-10-20 : 13:28:55
[code]
SELECT m.*,COALESCE(n.[Number of calls],0) AS [Number of calls]
FROM
(
SELECT [Date],[Time interval],PilotID
FROM (SELECT DISTINCT [Time interval] FROM Table)t
CROSS JOIN (SELECT DISTINCT [Date] FROM Table WHERE [Date] BETWEEN @StartDate AND @EndDate)d
CROSS JOIN (SELECT DISTINCT PilotID FROM Table WHERE PilotID IN (Your list values...))p
)m
LEFT JOIN Table n
ON n.PilotID = m.PilotID
AND n.[Date] = m.[Date]
AND n.[Time interval] = m.[Time interval]
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

L_i_L_i
Starting Member

6 Posts

Posted - 2013-10-22 : 16:20:16
Thank you very much, you made my day! :)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-10-23 : 02:09:56
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -