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
 complex join statement

Author  Topic 

divan
Posting Yak Master

153 Posts

Posted - 2013-06-20 : 11:24:23
I have three tables

the first one is called ACCOUNTDETAIL with the following fields

DETAIL_AMOUNT POLICY_NUMBER ACCOUNT_SET GROUP_ACTIVITY
44563 TX 13107 1 R
44563 TX 13107 1 B
3481 TX 13107 1 R
48044 TX 13107 1 B
1099 TX 13107 1 R

The second one being REGISTER with the following fields

POLICY_NMBER DATE_TIME SET
TX 13107 8/11/09 3:03 PM 1
TX 13107 8/11/09 3:10 PM 1
TX 13107 8/11/09 3:16 PM 1
TX 13107 8/11/09 3:26 PM 1
TX 13107 8/31/09 2:38 PM 1
TX 13107 9/9/09 4:43 PM 1
TX 13107 9/11/09 2:29 PM 1
TX 13107 9/11/09 3:21 PM 1
TX 13107 9/11/09 3:31 PM 1
TX 13107 9/11/09 3:35 PM 1
TX 13107 9/11/09 3:36 PM 1
TX 13107 9/11/09 3:47 PM 1

And the third table is POLICY with the following fields

DATE_TIME POLICY_NUMBER EFF_DATE
8/11/09 3:03 PM TX 13107 07/01/09
8/11/09 3:10 PM TX 13107 07/01/09
8/11/09 3:16 PM TX 13107 07/01/09
8/11/09 3:26 PM TX 13107 07/01/09
8/31/09 2:38 PM TX 13107 07/01/09
9/9/09 4:43 PM TX 13107 07/01/09
9/11/09 2:29 PM TX 13107 07/01/09
9/11/09 3:21 PM TX 13107 07/01/09
9/11/09 3:31 PM TX 13107 07/01/09
9/11/09 3:35 PM TX 13107 07/01/09
9/11/09 3:36 PM TX 13107 07/01/09
9/11/09 3:47 PM TX 13107 07/01/09


I have written the following script

SELECT A.POLICY_NUMBER, A.ACCOUNT_SET,SUM(A.DETAIL_AMOUNT)AS TOTAL_AMOUNT,P.POL_EFF_DATE,P.POL_EXP_DATE
FROM ACCOUNTDETAIL A
INNER JOIN REGISTER R ON A.ACCOUNT_SET = R.PORTFOLIO_SET
INNER JOIN POLICY P ON R.POLICY_DATE_TIME = (SELECT MAX (R2.POLICY_DATE_TIME) FROM REGISTER R2 WHERE R2.POLICY_NUMBER = R.POLICY_NUMBER AND R2.PORTFOLIO_SET = R.PORTFOLIO_SET) = P.POLICY_DATE_TIME
WHERE ACTIVITY_GROUP <> 'B' AND DETAIL_AMOUNT <> 0
GROUP BY A.POLICY_NUMBER,A.ACCOUNT_SET,P.POL_EFF_DATE,P.POL_EXP_DATE
ORDER BY A.POLICY_NUMBER, A.ACCOUNT_SET


What I am trying to do with the following join statement is to find the account set from the ACCOUNTDETAIL and find the set in the REGISTER table and then find the last DATE_TIME for that set and policy and then find that DATE_TIME FOR THAT POLICY_NUMBER in the POLICY table so I can get the POL_EFF_DATE.....

INNER JOIN POLICY P ON R.POLICY_DATE_TIME = (SELECT MAX (R2.POLICY_DATE_TIME) FROM REGISTER R2 WHERE R2.POLICY_NUMBER = R.POLICY_NUMBER AND R2.PORTFOLIO_SET = R.PORTFOLIO_SET) = P.POLICY_DATE_TIME

this is not working so please help...

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2013-06-20 : 13:01:05
Can you put your data in a consumable format?

Also, what do you want for output?
Go to Top of Page

divan
Posting Yak Master

153 Posts

Posted - 2013-06-20 : 13:51:41
Not sure what you meanby consumable format.. What I want for the output to be is eff_date of 7/1/09 in this example..
Go to Top of Page

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2013-06-20 : 14:01:09
Consumable means

declare @x table (col1 int)
insert into @x values (1),(2),(3)


so that we can use this script and try developing the desired query for you.

quote:

What I want for the output to be is eff_date of 7/1/09 in this example..


SELECT '2009-01-07' eff_date --this will give you even the desired ouput?????

If not, then please further explain :)

Cheers
MIK
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2013-06-20 : 14:10:49
quote:
Originally posted by divan

Not sure what you meanby consumable format.. What I want for the output to be is eff_date of 7/1/09 in this example..


Follow these links for how to put your data in a consumable format. They also have other valuable informaiton on how to ask your question so we can help you better.
http://www.sqlservercentral.com/articles/Best+Practices/61537/
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page
   

- Advertisement -