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 2005 Forums
 Transact-SQL (2005)
 Returning empty cells in a query

Author  Topic 

mchism
Starting Member

3 Posts

Posted - 2013-12-06 : 00:00:35
Hi all,

I am attempting to run a query using left join, but as some cells from my first cloumn are empty, I can not populate the 2nd and 3rd colums when the 1st is empty, regardless of results for the 2nd and 3rd. Please help, thank you.

select a.*,
case when b.Trip_Rec_Type IS NULL then 0 else b. Trip_Rec_Type end Trip_Rec_Type,
case when c. Trip_Rec_Type IS NULL then 0 else c. Trip_Rec_Type end Trip_Rec_Type
from
(SELECT CAST(CONVERT(varchar(10),Date_only,110) as datetime) as "Date_Only", Hour_only, COUNT (*) Trip_Rec_Type FROM TRIPRATDETPLUS Where Trip_Rec_Type = '100%' Group by Date_only, Hour_only) a
left outer join (SELECT CAST(CONVERT(varchar(10),Date_only,110) as datetime) as "Date_Only", Hour_only, COUNT (*) Trip_Rec_Type FROM TRIPRATDETPLUS Where Trip_Rec_Type = '50%' Group by Date_only, Hour_only) b on a.Date_only = b.Date_only AND a.Hour_only =b.Hour_only
left outer Join (SELECT CAST(CONVERT(varchar(10),Date_only,110) as datetime) as "Date_Only", Hour_only, COUNT (*) Trip_Rec_Type FROM TRIPRATDETPLUS Where Trip_Rec_Type = 'TP1' Group by Date_only, Hour_only) c on a.Date_only = c.Date_only AND a.Hour_only = c.Hour_only
order by 1, 2

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-12-06 : 05:17:03
for getting 2nd and 3rd columns even when a is absent you should be using full outer join against left join

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

- Advertisement -