| Author |
Topic  |
|
|
eugz
Posting Yak Master
186 Posts |
Posted - 12/01/2012 : 23:42:57
|
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
47189 Posts |
Posted - 12/02/2012 : 01:52:29
|
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/
|
 |
|
|
sodeep
Flowing Fount of Yak Knowledge
USA
7173 Posts |
Posted - 12/02/2012 : 10:28:23
|
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 |
 |
|
|
eugz
Posting Yak Master
186 Posts |
Posted - 12/02/2012 : 12:59:56
|
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. |
 |
|
|
sodeep
Flowing Fount of Yak Knowledge
USA
7173 Posts |
Posted - 12/02/2012 : 19:28:30
|
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 |
 |
|
|
eugz
Posting Yak Master
186 Posts |
Posted - 12/02/2012 : 22:26:39
|
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 |
 |
|
|
sodeep
Flowing Fount of Yak Knowledge
USA
7173 Posts |
Posted - 12/02/2012 : 23:55:08
|
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 ? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47189 Posts |
Posted - 12/03/2012 : 07:41:28
|
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/
|
 |
|
|
sodeep
Flowing Fount of Yak Knowledge
USA
7173 Posts |
Posted - 12/03/2012 : 16:39:01
|
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? |
 |
|
|
eugz
Posting Yak Master
186 Posts |
Posted - 12/03/2012 : 17:13:04
|
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 |
 |
|
|
sodeep
Flowing Fount of Yak Knowledge
USA
7173 Posts |
Posted - 12/03/2012 : 23:06:27
|
| You need to explain with proper example and expected output. The output you want is confusing. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47189 Posts |
Posted - 12/04/2012 : 00:58:52
|
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/
|
 |
|
|
eugz
Posting Yak Master
186 Posts |
Posted - 12/04/2012 : 17:15:46
|
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 |
 |
|
| |
Topic  |
|