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.
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 010020130303 01:00-02:00 23 011020130303 03:00-04:00 12 0100etc.... 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],PilotIDFROM (SELECT DISTINCT [Time interval] FROM Table)tCROSS JOIN (SELECT DISTINCT [Date] FROM Table WHERE [Date] BETWEEN @StartDate AND @EndDate)dCROSS JOIN (SELECT DISTINCT PilotID FROM Table WHERE PilotID IN (Your list values...))p)mLEFT JOIN Table nON n.PilotID = m.PilotIDAND n.[Date] = m.[Date]AND n.[Time interval] = m.[Time interval][/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
L_i_L_i
Starting Member
6 Posts |
Posted - 2013-10-22 : 16:20:16
|
Thank you very much, you made my day! :) |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-23 : 02:09:56
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|