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
 General SQL Server Forums
 New to SQL Server Programming
 specify column field by condition
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

eugz
Posting Yak Master

195 Posts

Posted - 10/12/2012 :  15:35:46  Show Profile  Reply with Quote
Hi All

I try to create select

select distinct
TestNo
,LName+', '+FName Name
,case when (p.Status_Id)=20 then (p.StatusDate) 
      when (p.Status_Id)=28 then (p.StatusDate) end Test_Date
,p.Status_Id
from dbo.Request m
join (select p1.* 
	  From dbo.Statuses p1
	  join (Select p2.Request_id, StatusDate 
		    From dbo.Statuses p2) mp
	  on p1.Request_Id = mp.Request_Id) p
on m.Request_Id = p.Request_Id

It works but I would like to have 2 column for StatusDate in that select:
if Status_Id = 20 StatusDate column should be Test1
if Status_Id = 28 StatusDate column should be Test2

How it to do?

Thanks.

Lamprey
Flowing Fount of Yak Knowledge

4614 Posts

Posted - 10/12/2012 :  16:18:49  Show Profile  Reply with Quote
I'm not really following your question. Do you want two new columns names Test1 and Test2? If so, then maybe this will help:
,case when (p.Status_Id)=20 then (p.StatusDate) else null end AS Test1
    ,case when (p.Status_Id)=28 then (p.StatusDate) else null end AS Test2

Edited by - Lamprey on 10/12/2012 16:19:40
Go to Top of Page

eugz
Posting Yak Master

195 Posts

Posted - 10/12/2012 :  16:28:09  Show Profile  Reply with Quote
Hi Lamprey. Thanks for replay.

That works. I add at the end of select where p.StatusDate is not null to eliminate all row where Test1 and Test2 is null but didn't get result. What is problem?

Thanks.

Edited by - eugz on 10/12/2012 16:35:45
Go to Top of Page

Lamprey
Flowing Fount of Yak Knowledge

4614 Posts

Posted - 10/12/2012 :  16:35:16  Show Profile  Reply with Quote
I can't say for sure, I can only assume that all the p.StatusDate values are NULL. If that is not the case, then you'll have to prodive DDl, DML and expected results:

http://www.sqlservercentral.com/articles/Best+Practices/61537/
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
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.08 seconds. Powered By: Snitz Forums 2000