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
 problem with select
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

eugz
Posting Yak Master

195 Posts

Posted - 12/01/2012 :  23:42:57  Show Profile  Reply with Quote
Hi All.

I have select it works fine. But I would like to modify it to display Active field from dbo.Payments table if one of record with the same Request_Id has Actuve='True'. Now it display only for latest PaidDate. If is it possible how it to do? My code is:

select 
r.Request_Id
,Customer_No
,LName
,FName
,s.Status
,t.EffDate
,t.Active
,Amount
from dbo.Request r
LEFT JOIN
(
SELECT p.*
FROM dbo.Payments p
INNER JOIN (SELECT Request_Id,MAX(PaidDate) AS EffDate
	  FROM dbo.Payments
	  GROUP BY Request_ID) x
ON  p.Request_Id = x.Request_Id
AND p.PaidDate = x.EffDate
)t
ON t.Request_ID = r.Request_ID
Left join dbo.v_Status s
on t.Status_Id = s.Status_Id


Thanks

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 12/02/2012 :  01:52:29  Show Profile  Reply with Quote

elect 
r.Request_Id
,Customer_No
,LName
,FName
,s.Status
,t.EffDate
,t.Active
,Amount
from dbo.Request r
LEFT JOIN
(
SELECT p.*
FROM dbo.Payments p
INNER JOIN (SELECT Request_Id
	  FROM dbo.Payments
	  GROUP BY Request_ID
          HAVING SUM(CASE WHEN Active = 'true' THEN 1 ELSE 0 END) > 0) x
ON  p.Request_Id = x.Request_Id
)t
ON t.Request_ID = r.Request_ID
Left join dbo.v_Status s
on t.Status_Id = s.Status_Id


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

sodeep
Flowing Fount of Yak Knowledge

USA
7174 Posts

Posted - 12/02/2012 :  10:28:23  Show Profile  Reply with Quote

select 
r.Request_Id
,Customer_No
,LName
,FName
,s.Status
,t.PaidDate
,t.Active
,Amount
from dbo.Request r
LEFT JOIN
(
SELECT p.*,ROW_NUMBER() OVER(PARTITION BY p.RequestId Order by Case WHEN Active = 'true' THEN 1 ELSE 0 END) as Seq
FROM dbo.Payments p
)x ON  p.Request_Id = r.Request_Id and Seq = 1
Left join dbo.v_Status s
on t.Status_Id = s.Status_Id
Go to Top of Page

eugz
Posting Yak Master

195 Posts

Posted - 12/02/2012 :  12:59:56  Show Profile  Reply with Quote
Thanks for replays.

I would like to have select with latest PaidDate and display Active field from dbo.Payments table if one of record with the same Request_Id has Actuve='True'. Is that possible to get? If yes how it to do?

Thanks.
Go to Top of Page

sodeep
Flowing Fount of Yak Knowledge

USA
7174 Posts

Posted - 12/02/2012 :  19:28:30  Show Profile  Reply with Quote
select 
r.Request_Id
,Customer_No
,LName
,FName
,s.Status
,t.PaidDate
,t.Active
,Amount
from dbo.Request r
LEFT JOIN
(
SELECT p.*,ROW_NUMBER() OVER(PARTITION BY p.RequestId Order by t.PaidDate desc,Case WHEN Active = 'true' THEN 1 ELSE 0 END) as Seq
FROM dbo.Payments p
)x ON  p.Request_Id = r.Request_Id and Seq = 1
Left join dbo.v_Status s
on t.Status_Id = s.Status_Id
Go to Top of Page

eugz
Posting Yak Master

195 Posts

Posted - 12/02/2012 :  22:26:39  Show Profile  Reply with Quote
Hi sodeep. Thanks for replay.

Your code return the same result like I had just for latest PaidDate without Active = 'True'. For instance, I have
Request_Id PaidDate Active
=========================================
3 6/3/2011 FALSE
3 9/20/2011 TRUE
3 12/1/2012 FALSE

As the resalt I would like to get
Request_Id PaidDate Active
=========================================
3 12/1/2012 TRUE

Thanks

Edited by - eugz on 12/02/2012 22:27:46
Go to Top of Page

sodeep
Flowing Fount of Yak Knowledge

USA
7174 Posts

Posted - 12/02/2012 :  23:55:08  Show Profile  Reply with Quote
quote:
Originally posted by eugz

Hi sodeep. Thanks for replay.

Your code return the same result like I had just for latest PaidDate without Active = 'True'. For instance, I have
Request_Id PaidDate Active
=========================================
3 6/3/2011 FALSE
3 9/20/2011 TRUE
3 12/1/2012 FALSE

As the resalt I would like to get
Request_Id PaidDate Active
=========================================
3 12/1/2012 TRUE

Thanks





But Active is True for 9/20/2011 ?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 12/03/2012 :  07:41:28  Show Profile  Reply with Quote
sound like this then

Select 
r.Request_Id
,Customer_No
,LName
,FName
,s.Status
,t.EffDate
,CASE WHEN ActiveCnt > 0 THEN 'True' ELSE t.Active END
,Amount
from dbo.Request r
LEFT JOIN
(
SELECT p.*
FROM dbo.Payments p
INNER JOIN (SELECT Request_Id,MAX(PaidDate) AS EffDate,
          SUM(CASE WHEN Active = 'true' THEN 1 ELSE 0 END) AS ActiveCnt
	  FROM dbo.Payments
	  GROUP BY Request_ID) x
ON  p.Request_Id = x.Request_Id
AND p.PaidDate = x.EffDate
)t
ON t.Request_ID = r.Request_ID
Left join dbo.v_Status s
on t.Status_Id = s.Status_Id


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

sodeep
Flowing Fount of Yak Knowledge

USA
7174 Posts

Posted - 12/03/2012 :  16:39:01  Show Profile  Reply with Quote
quote:
Originally posted by visakh16

sound like this then

Select 
r.Request_Id
,Customer_No
,LName
,FName
,s.Status
,t.EffDate
,CASE WHEN ActiveCnt > 0 THEN 'True' ELSE 'False' END
,Amount
from dbo.Request r
LEFT JOIN
(
SELECT p.*
FROM dbo.Payments p
INNER JOIN (SELECT Request_Id,MAX(PaidDate) AS EffDate,
          SUM(CASE WHEN Active = 'true' THEN 1 ELSE 0 END) AS ActiveCnt
	  FROM dbo.Payments
	  GROUP BY Request_ID) x
ON  p.Request_Id = x.Request_Id
AND p.PaidDate = x.EffDate
)t
ON t.Request_ID = r.Request_ID
Left join dbo.v_Status s
on t.Status_Id = s.Status_Id


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





Shouldn't it be this one?
Go to Top of Page

eugz
Posting Yak Master

195 Posts

Posted - 12/03/2012 :  17:13:04  Show Profile  Reply with Quote
Hi visakh16. Thanks for replays.

I tried your code. In line

CASE WHEN ActiveCnt > 0 THEN 'True' ELSE 'False' END

ActiveCnt indicate like invalid column name. I tried modify like t.ActiveCtn nothing help. When I run INNER JOIN SELECT itself result looks ok but when I run whole code result not like expected.

Thanks.

Edited by - eugz on 12/03/2012 17:35:49
Go to Top of Page

sodeep
Flowing Fount of Yak Knowledge

USA
7174 Posts

Posted - 12/03/2012 :  23:06:27  Show Profile  Reply with Quote
You need to explain with proper example and expected output. The output you want is confusing.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 12/04/2012 :  00:58:52  Show Profile  Reply with Quote
quote:
Originally posted by eugz

Hi visakh16. Thanks for replays.

I tried your code. In line

CASE WHEN ActiveCnt > 0 THEN 'True' ELSE 'False' END

ActiveCnt indicate like invalid column name. I tried modify like t.ActiveCtn nothing help. When I run INNER JOIN SELECT itself result looks ok but when I run whole code result not like expected.

Thanks.


then i'm sure you've some other part too which you're not explaining us. Please post you exact scenario with sample data if you need more help

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

eugz
Posting Yak Master

195 Posts

Posted - 12/04/2012 :  17:15:46  Show Profile  Reply with Quote
Hi visakh16. Thanks for continue help.

I solved the problem by modified the line
SELECT p.*
to
SELECT p.*,EffDate,ActiveCnt

Thanks for help.

Edited by - eugz on 12/04/2012 22:18:31
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.14 seconds. Powered By: Snitz Forums 2000