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.
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 fieldsDETAIL_AMOUNT POLICY_NUMBER ACCOUNT_SET GROUP_ACTIVITY44563 TX 13107 1 R44563 TX 13107 1 B3481 TX 13107 1 R48044 TX 13107 1 B1099 TX 13107 1 RThe second one being REGISTER with the following fieldsPOLICY_NMBER DATE_TIME SETTX 13107 8/11/09 3:03 PM 1TX 13107 8/11/09 3:10 PM 1TX 13107 8/11/09 3:16 PM 1TX 13107 8/11/09 3:26 PM 1TX 13107 8/31/09 2:38 PM 1TX 13107 9/9/09 4:43 PM 1TX 13107 9/11/09 2:29 PM 1TX 13107 9/11/09 3:21 PM 1TX 13107 9/11/09 3:31 PM 1TX 13107 9/11/09 3:35 PM 1TX 13107 9/11/09 3:36 PM 1TX 13107 9/11/09 3:47 PM 1And the third table is POLICY with the following fieldsDATE_TIME POLICY_NUMBER EFF_DATE8/11/09 3:03 PM TX 13107 07/01/098/11/09 3:10 PM TX 13107 07/01/098/11/09 3:16 PM TX 13107 07/01/098/11/09 3:26 PM TX 13107 07/01/098/31/09 2:38 PM TX 13107 07/01/099/9/09 4:43 PM TX 13107 07/01/099/11/09 2:29 PM TX 13107 07/01/099/11/09 3:21 PM TX 13107 07/01/099/11/09 3:31 PM TX 13107 07/01/099/11/09 3:35 PM TX 13107 07/01/099/11/09 3:36 PM TX 13107 07/01/099/11/09 3:47 PM TX 13107 07/01/09I have written the following scriptSELECT A.POLICY_NUMBER, A.ACCOUNT_SET,SUM(A.DETAIL_AMOUNT)AS TOTAL_AMOUNT,P.POL_EFF_DATE,P.POL_EXP_DATEFROM ACCOUNTDETAIL AINNER JOIN REGISTER R ON A.ACCOUNT_SET = R.PORTFOLIO_SETINNER 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_TIMEWHERE ACTIVITY_GROUP <> 'B' AND DETAIL_AMOUNT <> 0 GROUP BY A.POLICY_NUMBER,A.ACCOUNT_SET,P.POL_EFF_DATE,P.POL_EXP_DATEORDER BY A.POLICY_NUMBER, A.ACCOUNT_SETWhat 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_TIMEthis 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? |
|
|
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.. |
|
|
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 :)CheersMIK |
|
|
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 |
|
|
|
|
|
|
|