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
 General SQL Server Forums
 New to SQL Server Programming
 problem with select

Author  Topic 

eugz
Posting Yak Master

210 Posts

Posted - 2012-12-01 : 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

52326 Posts

Posted - 2012-12-02 : 01:52:29
[code]
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
[/code]

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

Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2012-12-02 : 10:28:23
[code]
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[/code]
Go to Top of Page

eugz
Posting Yak Master

210 Posts

Posted - 2012-12-02 : 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.
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2012-12-02 : 19:28:30
[code]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[/code]
Go to Top of Page

eugz
Posting Yak Master

210 Posts

Posted - 2012-12-02 : 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
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2012-12-02 : 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 ?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-12-03 : 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/

Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2012-12-03 : 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?
Go to Top of Page

eugz
Posting Yak Master

210 Posts

Posted - 2012-12-03 : 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.
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2012-12-03 : 23:06:27
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

52326 Posts

Posted - 2012-12-04 : 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/

Go to Top of Page

eugz
Posting Yak Master

210 Posts

Posted - 2012-12-04 : 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.
Go to Top of Page
   

- Advertisement -