SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Returning empty cells in a query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

mchism
Starting Member

3 Posts

Posted - 12/06/2013 :  00:00:35  Show Profile  Reply with Quote
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

India
52249 Posts

Posted - 12/06/2013 :  05:17:03  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000