473,583 Members | 3,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL query - duplicate records - different dates - how to get only latest information?

I have a SQL query I need to design to select name and email addresses
for policies that are due and not renewed in a given time period. The
problem is, the database keeps the information for every renewal in
the history of the policyholder.
The information is in 2 tables, policy and customer, which share the
custid data. The polno changes with every renewal Renewals in 2004
would be D, 2005 S, and 2006 L. polexpdates for a given customer could
be 2007-03-21, 2006-03-21, 2005-03-21, and 2004-09-21, with polno of
1234 (original policy), 1234D (renewal in 2004), 1234S (renewal in
2005), and 1235L (renewed in 2006).
The policy is identified in trantype as either 'rwl' for renewal, or
'nbs' for new business.

The policies would have poleffdates of 2004-03-21 (original 6 month
policy) 2004-09-21 (first 6 month renewal) , 2005-03-21 (2nd renewal,
1 year), 2006-03-21(3rd renewal, 1 yr).

I want ONLY THE LATEST information, and keep getting early
information.
My current query structure is:
select c.lastname, c.email, p.polno, p.polexpdate
from policy p, customer c
where p.polid = c.polid
and p.polexpdate between '2006-03-01 and 2006-03-31
and p.polno like '1234%s'
and p.trantype like 'rwl'
and c.email is not null

union

select c.lastname, c.email, p.polno, p.polexpdate
from policy p, customer c
where p.polid = c.polid
and p.polexpdate between '2006-03-01 and 2006-03-31
and p.polno like '1234%'
and p.trantype like 'nbs'
and c.email is not null

How do I make this query give me ONLY the polno 123%, or 123%S
information, and not give me the information on policies that ALSO
have 123%L policies, and/ or renewal dates after 2006-03-31?

Adding a 'and not polexpdate > 2006-03-31' does not work.

I am working with SQL SERVER 2003. Was using SQL Server 7, but found
it was too restrictive, and I had a valid 2003 licence, so I upgraded,
and still could not do it (after updating the syntax - things like
using single quotes instead of double, etc)

I keep getting those policies that were due in the stated range and
HAVE been renewed as well as those which have not. I need to get only
those which have NOT been renewed, and I cannot modify the database in
any way.
*** Free account sponsored by SecureIX.com ***
*** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com ***
Mar 17 '06 #1
24 19889
There is no such thing as SQL Server 2003....do you mean 2000 or 2005?

You have a lot of questions here, I'll take a stab at a couple of them
and then we'll go from there.

Structure and data would have helped me give you a MUCH better answer.
I'm going to use a simpler model.

CREATE TABLE Policy (
PolID int,
PolNo nvarchar(30),
PolExpDate datetime,
TranType nvarchar(30)
)

CREATE TABLE Customer (
Lastname nvarchar(30),
Email nvarchar(30),
PolID int
)

INSERT INTO Customer VALUES ('Abby', 'a@a.com', 1)
INSERT INTO Customer VALUES ('Brad', 'b@b.com.com', 2)
INSERT INTO Customer VALUES ('Chris', NULL, 3)

INSERT INTO Policy VALUES (1, '1234', '20030101', 'nbs')
INSERT INTO Policy VALUES (1, '1234D', '20040101', 'rwl')
INSERT INTO Policy VALUES (1, '1234S', '20050101', 'rwl')
INSERT INTO Policy VALUES (1, '1234L', '20060101', 'rwl')

INSERT INTO Policy VALUES (2, '1234', '20030101', 'nbs')
INSERT INTO Policy VALUES (2, '1234D', '20040101', 'rwl')
INSERT INTO Policy VALUES (2, '1234S', '20050101', 'rwl')
INSERT INTO Policy VALUES (2, '1234L', '20060101', 'rwl')

INSERT INTO Policy VALUES (3, '1234', '20030101', 'nbs')
INSERT INTO Policy VALUES (3, '1234D', '20040101', 'rwl')
INSERT INTO Policy VALUES (3, '1234S', '20050101', 'rwl')
INSERT INTO Policy VALUES (3, '1234L', '20060101', 'rwl')

SELECT * FROM Policy P
JOIN Customer C ON C.PolID = P.PolID
WHERE P.PolExpDate =
(SELECT TOP 1 P2.PolExpDate
FROM Policy P2
WHERE P2.PolID = C.PolID
ORDER BY P2.PolExpDate DESC
)
AND NOT C.Email IS NULL
AND (P.TranType IN ('nbs', 'rwl'))

Note that you probably should have a TransTypes table so that you
aren't storing strings ('rwl', 'nbs') in your policy table over and
over.

You also have no need for unions here.

If you want specific types, you can use ORs or the like.

I'm not sure what you want with the dates in the where clause. Do you
want only records that have their most recent event between two dates?

Also note, there are probably a hundred ways to do this...this is
unlikely to be the fastest. Without really knowing what you want
though...

Mar 17 '06 #2
On 17 Mar 2006 12:49:52 -0800, "figital" <mh****@gmail.c om> wrote:
There is no such thing as SQL Server 2003....do you mean 2000 or 2005?

You have a lot of questions here, I'll take a stab at a couple of them
and then we'll go from there.

Structure and data would have helped me give you a MUCH better answer.
I'm going to use a simpler model.

CREATE TABLE Policy (
PolID int,
PolNo nvarchar(30),
PolExpDate datetime,
TranType nvarchar(30)
)

CREATE TABLE Customer (
Lastname nvarchar(30),
Email nvarchar(30),
PolID int
)

INSERT INTO Customer VALUES ('Abby', 'a@a.com', 1)
INSERT INTO Customer VALUES ('Brad', 'b@b.com.com', 2)
INSERT INTO Customer VALUES ('Chris', NULL, 3)

INSERT INTO Policy VALUES (1, '1234', '20030101', 'nbs')
INSERT INTO Policy VALUES (1, '1234D', '20040101', 'rwl')
INSERT INTO Policy VALUES (1, '1234S', '20050101', 'rwl')
INSERT INTO Policy VALUES (1, '1234L', '20060101', 'rwl')

INSERT INTO Policy VALUES (2, '1234', '20030101', 'nbs')
INSERT INTO Policy VALUES (2, '1234D', '20040101', 'rwl')
INSERT INTO Policy VALUES (2, '1234S', '20050101', 'rwl')
INSERT INTO Policy VALUES (2, '1234L', '20060101', 'rwl')

INSERT INTO Policy VALUES (3, '1234', '20030101', 'nbs')
INSERT INTO Policy VALUES (3, '1234D', '20040101', 'rwl')
INSERT INTO Policy VALUES (3, '1234S', '20050101', 'rwl')
INSERT INTO Policy VALUES (3, '1234L', '20060101', 'rwl')

SELECT * FROM Policy P
JOIN Customer C ON C.PolID = P.PolID
WHERE P.PolExpDate =
(SELECT TOP 1 P2.PolExpDate
FROM Policy P2
WHERE P2.PolID = C.PolID
ORDER BY P2.PolExpDate DESC
)
AND NOT C.Email IS NULL
AND (P.TranType IN ('nbs', 'rwl'))

Note that you probably should have a TransTypes table so that you
aren't storing strings ('rwl', 'nbs') in your policy table over and
over.

You also have no need for unions here.

If you want specific types, you can use ORs or the like.

I'm not sure what you want with the dates in the where clause. Do you
want only records that have their most recent event between two dates?

Also note, there are probably a hundred ways to do this...this is
unlikely to be the fastest. Without really knowing what you want
though...

OK, here is the scoop.
The program that creates and uses the database does not allow us to
select only the policies that are due and nor yet renewed in a given
time period and return the e-mail address.

We need this functionality to email policy reminders with a link to
the e-commerce page that allows them to renew online. This will save
mailing notices to those policyholders that have e-mail and deal with
the company in that manner.

There are two tables involved - and they can NOT be modified in any
way - we are stuck with them.

The customer table has all kinds of customer information fields - over
half of which are not used at all. Among them are the lastname (which
has first and last name in it), email address,policy-id, and a lot of
stuff we don't need to know.

The policy information table has the policy-id, the policy number, the
renewal date, and the effective date, as well as the transaction type.
The policy number starts with a particular numeric pattern, which
identifies the particular policies we are interested in. The rest of
the policy number identifies the actual policy, and starts out as a
numeric string, like 3215.
When it is renewed, it is saved with a suffix letter. Might be d for
2005, s for 2005, and l for 2006 (I think that is correct - but you
get the idea). Nothing is EVER deleted from this database.

SO - John Doe has policy 12345-123 issued Mar 1 2003, due Mar 1 2004.
John Doe's policy is renewed in Feb 2004, and becomes 12345-123d, due
Mar 1, 2005.
It is renewed and becomes 12345-123s, due Mar 2006
It is renewed Mar 2006 and becomes 12345-123L, due Mar 1 2007.

Jane Smith has a policy #12345-923 issued Mar 9, 2005, due Mar 9,
2006.
IT is renewed and becomes 12345-923s, due Mar 9, 2006

I need to do a search by date and policy number to find Jane Smith,
and others like her, while not finding John Doe, and others like him,
in Feb 2006 to remind them to renew their policies.
Don't want to bother those who have already looked after theirs. Don't
want to miss the one that's going to forget without a reminder.

I WAS using the union because I was breaking it down looking for only
renewals with a S at the end of the policy number, eliminating the
1993, 1994, and 1995 renewals - and specifying poltype "rwl" so we
were only looking at renewals.
Then I was adding the policies that were new business "nbs", and had
not yet had a renewal, so had no 'S' on the end.

That was the theory behind the construct.

I know we can likely get rid of that complication IF we can figure out
how to get only the LATEST renewal date - using the capabilities of
SQL Server 2003. Have not seen any documentation on "top", so am
unaware of if and how it works.
*** Free account sponsored by SecureIX.com ***
*** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com ***
Mar 18 '06 #3
(clare at snyder.on.ca) writes:
I want ONLY THE LATEST information, and keep getting early
information.
My current query structure is:
select c.lastname, c.email, p.polno, p.polexpdate
from policy p, customer c
where p.polid = c.polid
and p.polexpdate between '2006-03-01 and 2006-03-31
and p.polno like '1234%s'
and p.trantype like 'rwl'
and c.email is not null

union

select c.lastname, c.email, p.polno, p.polexpdate
from policy p, customer c
where p.polid = c.polid
and p.polexpdate between '2006-03-01 and 2006-03-31
and p.polno like '1234%'
and p.trantype like 'nbs'
and c.email is not null

How do I make this query give me ONLY the polno 123%, or 123%S
information, and not give me the information on policies that ALSO
have 123%L policies, and/ or renewal dates after 2006-03-31?

Adding a 'and not polexpdate > 2006-03-31' does not work.
It appears that the data you have is very messy, with various pieces
of information built into the policy number. Since I don't know the
tables in detail, and only have your narrative to work from, this
is a bit of guesswork:

SELECT c.lastname, c.email, p.polno, p.polexpdate
FROM customer c
JOIN (SELECT polid, polexpdate = MAX(polexpdate)
FROM policies
WHERE polno like '1234%'
GROUP BY polid) AS p1 ON p1.polid = c.polid
JOIN policies p ON p.polid = p1.polid
AND p.polexpdate = p1.polexpdate
WHERE p.polexpdate BETWEEN '20060301' AND '20060331'
AND c.email IS NOT NULL

First, I have assumed that the polid does not change over renewals, but
that is plain guess on my part.

The inner SELECT gives the most recent expiration date for each polid,
and then I filter to see only those with expiration in March 2006.

If this does not address your problem, I would suggest that you post:

o CREATE TABLE statements for your tables. Don't forget to include
defintion of primary keys and foreign keys.
o INSERT statemetns with sample data.
o The desired result given the sample.
I am working with SQL SERVER 2003. Was using SQL Server 7, but found
it was too restrictive, and I had a valid 2003 licence, so I upgraded,
and still could not do it (after updating the syntax - things like
using single quotes instead of double, etc)
Please clarify if you are working with SQL 2000 or SQL 2005. There is
no such thing as SQL 2003.
I know we can likely get rid of that complication IF we can figure out
how to get only the LATEST renewal date - using the capabilities of
SQL Server 2003. Have not seen any documentation on "top", so am
unaware of if and how it works.


You can read about TOP in Books Online. By the way, it was available in
SQL 7 as well.

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Mar 18 '06 #4
On Sat, 18 Mar 2006 16:05:54 +0000 (UTC), Erland Sommarskog
<es****@sommars kog.se> wrote:
(clare at snyder.on.ca) writes:
I want ONLY THE LATEST information, and keep getting early
information.
My current query structure is:
select c.lastname, c.email, p.polno, p.polexpdate
from policy p, customer c
where p.polid = c.polid
and p.polexpdate between '2006-03-01 and 2006-03-31
and p.polno like '1234%s'
and p.trantype like 'rwl'
and c.email is not null

union

select c.lastname, c.email, p.polno, p.polexpdate
from policy p, customer c
where p.polid = c.polid
and p.polexpdate between '2006-03-01 and 2006-03-31
and p.polno like '1234%'
and p.trantype like 'nbs'
and c.email is not null

How do I make this query give me ONLY the polno 123%, or 123%S
information, and not give me the information on policies that ALSO
have 123%L policies, and/ or renewal dates after 2006-03-31?

Adding a 'and not polexpdate > 2006-03-31' does not work.


It appears that the data you have is very messy, with various pieces
of information built into the policy number. Since I don't know the
tables in detail, and only have your narrative to work from, this
is a bit of guesswork:

SELECT c.lastname, c.email, p.polno, p.polexpdate
FROM customer c
JOIN (SELECT polid, polexpdate = MAX(polexpdate)
FROM policies
WHERE polno like '1234%'
GROUP BY polid) AS p1 ON p1.polid = c.polid
JOIN policies p ON p.polid = p1.polid
AND p.polexpdate = p1.polexpdate
WHERE p.polexpdate BETWEEN '20060301' AND '20060331'
AND c.email IS NOT NULL

First, I have assumed that the polid does not change over renewals, but
that is plain guess on my part.

The inner SELECT gives the most recent expiration date for each polid,
and then I filter to see only those with expiration in March 2006.

If this does not address your problem, I would suggest that you post:

o CREATE TABLE statements for your tables. Don't forget to include
defintion of primary keys and foreign keys.
o INSERT statemetns with sample data.
o The desired result given the sample.
I am working with SQL SERVER 2003. Was using SQL Server 7, but found
it was too restrictive, and I had a valid 2003 licence, so I upgraded,
and still could not do it (after updating the syntax - things like
using single quotes instead of double, etc)


Please clarify if you are working with SQL 2000 or SQL 2005. There is
no such thing as SQL 2003.
I know we can likely get rid of that complication IF we can figure out
how to get only the LATEST renewal date - using the capabilities of
SQL Server 2003. Have not seen any documentation on "top", so am
unaware of if and how it works.


You can read about TOP in Books Online. By the way, it was available in
SQL 7 as well.

It is SQL 2000 - just checked.

I will try your code and see what happens.
Thanks.
*** Free account sponsored by SecureIX.com ***
*** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com ***
Mar 18 '06 #5

On Sat, 18 Mar 2006 16:05:54 +0000 (UTC), Erland Sommarskog
<es****@sommars kog.se> wrote:
I have a few syntax questions. I will insert them inline.
It appears that the data you have is very messy, with various pieces
of information built into the policy number. Since I don't know the
tables in detail, and only have your narrative to work from, this
is a bit of guesswork:

SELECT c.lastname, c.email, p.polno, p.polexpdate
FROM customer c
Don't I need 'policies p' in here too? JOIN (SELECT polid, polexpdate = MAX(polexpdate)
FROM policies
WHERE polno like '1234%'
and p.polno insted of polno? GROUP BY polid) AS p1 ON p1.polid = c.polid
And what is the 'BY polid)' and the 'AS p1 ON p1.polid' JOIN policies p ON p.polid = p1.polid
AND p.polexpdate = p1.polexpdate
WHERE p.polexpdate BETWEEN '20060301' AND '20060331'
Actual date info is " between '2006-03-01 00:00:00.000 and 2006-03-31
00:00:00.000' " AND c.email IS NOT NULL

First, I have assumed that the polid does not change over renewals, but
that is plain guess on my part.
I think your guess is correct, from what I've seen.
The inner SELECT gives the most recent expiration date for each polid,
and then I filter to see only those with expiration in March 2006.

If this does not address your problem, I would suggest that you post:
I think your methodology may work, if we get the syntax straightened
out.
o CREATE TABLE statements for your tables. Don't forget to include
defintion of primary keys and foreign keys.
o INSERT statemetns with sample data.
o The desired result given the sample.
I am working with SQL SERVER 2003. Was using SQL Server 7, but found
it was too restrictive, and I had a valid 2003 licence, so I upgraded,
and still could not do it (after updating the syntax - things like
using single quotes instead of double, etc)


Please clarify if you are working with SQL 2000 or SQL 2005. There is
no such thing as SQL 2003.
I know we can likely get rid of that complication IF we can figure out
how to get only the LATEST renewal date - using the capabilities of
SQL Server 2003. Have not seen any documentation on "top", so am
unaware of if and how it works.


You can read about TOP in Books Online. By the way, it was available in
SQL 7 as well.


*** Free account sponsored by SecureIX.com ***
*** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com ***
Mar 18 '06 #6
On Sat, 18 Mar 2006 16:05:54 +0000 (UTC), Erland Sommarskog
<es****@sommars kog.se> wrote:
(clare at snyder.on.ca) writes:
I want ONLY THE LATEST information, and keep getting early
information.
My current query structure is:
select c.lastname, c.email, p.polno, p.polexpdate
from policy p, customer c
where p.polid = c.polid
and p.polexpdate between '2006-03-01 and 2006-03-31
and p.polno like '1234%s'
and p.trantype like 'rwl'
and c.email is not null

union

select c.lastname, c.email, p.polno, p.polexpdate
from policy p, customer c
where p.polid = c.polid
and p.polexpdate between '2006-03-01 and 2006-03-31
and p.polno like '1234%'
and p.trantype like 'nbs'
and c.email is not null

How do I make this query give me ONLY the polno 123%, or 123%S
information, and not give me the information on policies that ALSO
have 123%L policies, and/ or renewal dates after 2006-03-31?

Adding a 'and not polexpdate > 2006-03-31' does not work.


It appears that the data you have is very messy, with various pieces
of information built into the policy number. Since I don't know the
tables in detail, and only have your narrative to work from, this
is a bit of guesswork:

SELECT c.lastname, c.email, p.polno, p.polexpdate
FROM customer c
JOIN (SELECT polid, polexpdate = MAX(polexpdate)
FROM policies
WHERE polno like '1234%'
GROUP BY polid) AS p1 ON p1.polid = c.polid
JOIN policies p ON p.polid = p1.polid
AND p.polexpdate = p1.polexpdate
WHERE p.polexpdate BETWEEN '20060301' AND '20060331'
AND c.email IS NOT NULL

First, I have assumed that the polid does not change over renewals, but
that is plain guess on my part.

The inner SELECT gives the most recent expiration date for each polid,
and then I filter to see only those with expiration in March 2006.

If this does not address your problem, I would suggest that you post:

o CREATE TABLE statements for your tables. Don't forget to include
defintion of primary keys and foreign keys.
o INSERT statemetns with sample data.
o The desired result given the sample.
I am working with SQL SERVER 2003. Was using SQL Server 7, but found
it was too restrictive, and I had a valid 2003 licence, so I upgraded,
and still could not do it (after updating the syntax - things like
using single quotes instead of double, etc)


Please clarify if you are working with SQL 2000 or SQL 2005. There is
no such thing as SQL 2003.
I know we can likely get rid of that complication IF we can figure out
how to get only the LATEST renewal date - using the capabilities of
SQL Server 2003. Have not seen any documentation on "top", so am
unaware of if and how it works.


You can read about TOP in Books Online. By the way, it was available in
SQL 7 as well.

Using just the inner SELECT, written as :
SELECT polid. polexpdate =MAX (polexpdate)
FROM afw_basicpolinf o p WHERE p.polno like '1932700%'
GROUP BY polid AS p1 ON p1.polid-p1 p1 polid
Join afw_basicpolinf o p on p.polid = p1.polid
AND p.polexpdate=p1 .polexpdate
WHERE p.polexpdate between '2006-04-15 00:00:00.000 and 2006-04-31
00:00:00.000'
and c.email IS NOT NULL
I get an error:

SERVER: Msg 156, Level 15, Stare 1, Line 3
Incorrect syntax near the keyword "AS".
*** Free account sponsored by SecureIX.com ***
*** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com ***
Mar 18 '06 #7
On Sat, 18 Mar 2006 16:05:54 +0000 (UTC), Erland Sommarskog
<es****@sommars kog.se> wrote:

It appears that the data you have is very messy, with various pieces
of information built into the policy number. Since I don't know the
tables in detail, and only have your narrative to work from, this
is a bit of guesswork:

SELECT c.lastname, c.email, p.polno, p.polexpdate
FROM customer c
JOIN (SELECT polid, polexpdate = MAX(polexpdate)
FROM policies
WHERE polno like '1234%'
GROUP BY polid) AS p1 ON p1.polid = c.polid
JOIN policies p ON p.polid = p1.polid
AND p.polexpdate = p1.polexpdate
WHERE p.polexpdate BETWEEN '20060301' AND '20060331'
AND c.email IS NOT NULL

First, I have assumed that the polid does not change over renewals, but
that is plain guess on my part.

The inner SELECT gives the most recent expiration date for each polid,
and then I filter to see only those with expiration in March 2006.

The "outer select" works if written:

Select c.lastname, c.email, p.polno, p.polexpdate
FROM afw_customer c, afw_basicpolinf o p

If we get the "inner select" working, the JOIN will likely work as
well.

*** Free account sponsored by SecureIX.com ***
*** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com ***
Mar 18 '06 #8
On Sat, 18 Mar 2006 16:05:54 +0000 (UTC), Erland Sommarskog
<es****@sommars kog.se> wrote:
(clare at snyder.on.ca) writes:
I want ONLY THE LATEST information, and keep getting early
information.
My current query structure is:
select c.lastname, c.email, p.polno, p.polexpdate
from policy p, customer c
where p.polid = c.polid
and p.polexpdate between '2006-03-01 and 2006-03-31
and p.polno like '1234%s'
and p.trantype like 'rwl'
and c.email is not null

union

select c.lastname, c.email, p.polno, p.polexpdate
from policy p, customer c
where p.polid = c.polid
and p.polexpdate between '2006-03-01 and 2006-03-31
and p.polno like '1234%'
and p.trantype like 'nbs'
and c.email is not null

How do I make this query give me ONLY the polno 123%, or 123%S
information, and not give me the information on policies that ALSO
have 123%L policies, and/ or renewal dates after 2006-03-31?

Adding a 'and not polexpdate > 2006-03-31' does not work.


It appears that the data you have is very messy, with various pieces
of information built into the policy number. Since I don't know the
tables in detail, and only have your narrative to work from, this
is a bit of guesswork:

SELECT c.lastname, c.email, p.polno, p.polexpdate
FROM customer c
JOIN (SELECT polid, polexpdate = MAX(polexpdate)
FROM policies
WHERE polno like '1234%'
GROUP BY polid) AS p1 ON p1.polid = c.polid
JOIN policies p ON p.polid = p1.polid
AND p.polexpdate = p1.polexpdate
WHERE p.polexpdate BETWEEN '20060301' AND '20060331'
AND c.email IS NOT NULL

First, I have assumed that the polid does not change over renewals, but
that is plain guess on my part.

The inner SELECT gives the most recent expiration date for each polid,
and then I filter to see only those with expiration in March 2006.

If this does not address your problem, I would suggest that you post:

o CREATE TABLE statements for your tables. Don't forget to include
defintion of primary keys and foreign keys.
o INSERT statemetns with sample data.
o The desired result given the sample.
I am working with SQL SERVER 2003. Was using SQL Server 7, but found
it was too restrictive, and I had a valid 2003 licence, so I upgraded,
and still could not do it (after updating the syntax - things like
using single quotes instead of double, etc)


Please clarify if you are working with SQL 2000 or SQL 2005. There is
no such thing as SQL 2003.
I know we can likely get rid of that complication IF we can figure out
how to get only the LATEST renewal date - using the capabilities of
SQL Server 2003. Have not seen any documentation on "top", so am
unaware of if and how it works.


You can read about TOP in Books Online. By the way, it was available in
SQL 7 as well.

And polid needs to be custid. I've remoted into the server today to
get the proper field names and to attempt to run the queries.
The policies table is afw_basicpolinf o, and the customers is
afw_customer.

*** Free account sponsored by SecureIX.com ***
*** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com ***
Mar 18 '06 #9
(clare at snyder.on.ca) writes:
SELECT c.lastname, c.email, p.polno, p.polexpdate
FROM customer c
Don't I need 'policies p' in here too?


It's further below. Note that I am using the newer ANSI syntax for
joins,
JOIN (SELECT polid, polexpdate = MAX(polexpdate)
FROM policies
WHERE polno like '1234%'


and p.polno insted of polno?


We're inside a derived table. A derived table is a temp table within the
query, but only conceptually. SQL Server may recast the comuptation
order, and often do. The purpose of the derived table is find the
latest expiration day for each policy holder.
GROUP BY polid) AS p1 ON p1.polid = c.polid


And what is the 'BY polid)'


The end of a GROUP BY clause.
and the 'AS p1 ON p1.polid'


AS p1 establishes an alias for the derived table. The ON clause specifies
on which columns the derived table is to be join with customers.
JOIN policies p ON p.polid = p1.polid
AND p.polexpdate = p1.polexpdate
WHERE p.polexpdate BETWEEN '20060301' AND '20060331'


Actual date info is " between '2006-03-01 00:00:00.000 and 2006-03-31
00:00:00.000' "


Always use the format YYYYMMDD for date literals, as this date format
is always interpreted the same. The format YYYY-MM-DD fails when the
current language is set to for instance German or British.
First, I have assumed that the polid does not change over renewals, but
that is plain guess on my part.


I think your guess is correct, from what I've seen.


Oh well, if you don't know what is the key in your data, no one here
will be able to help you with part.

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Mar 18 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2419
by: paii | last post by:
I have a table that stores job milestone dates. The 2 milestones I am interested in are "Ship Date" TypeID 1 and "Revised Ship Date" TypeID 18. All jobs have TypeID 1 only some jobs have TypeID 18. I need a query that will return the c date for TypeID 18 if it exist else the date for TypeID 1, for all jobs. the table structure is the...
2
2764
by: Jason | last post by:
I have a table of 650,000 records associated to contracts. Some of the contracts have multiple records but each contract only has one Active Record (there might be several inactive records). There are dates associated with each of the records (whether active or inactive). I need to compare the dates between the active and inactive contract...
1
1664
by: Mike Cooper | last post by:
Hi everyone, This is a tough one. I have a database full of solicitations, identifying a customer and recording initial call, first followup, second followup, etc. My boss want to be able to generate a report showing a list of customer who were call between two different dates that he types into a form. That, I have done. My boss...
2
4698
by: zeke | last post by:
Any assistance is much appreciated... I have a table with duplicate record information and I need to remove certain records based on values in four different fields. For example: PK field Name Date1 Date2 Date3 1 Bill 1/21/04 1/18/02 5/14/04 2 Bill 1/15/03 1/18/02 5/14/04 3 Bill 1/25/04 ...
22
31174
by: Stan | last post by:
I am working with Access 2003 on a computer running XP. I am new at using Access. I have a Db with a date field stored as mm/dd/yyyy. I need a Query that will prompt for the month, ie. 6 for June, and will return all records in that month.
16
3482
by: ARC | last post by:
Hello all, So I'm knee deep in this import utility program, and am coming up with all sorts of "gotcha's!". 1st off. On a "Find Duplicates Query", does anyone have a good solution for renaming the duplicate records? My thinking was to take the results of the duplicate query, and somehow have it number each line where there is a duplicate...
1
1892
by: bcquadrant | last post by:
Hi there I am trying to make an update query that will update information in all occurances of duplicate records based on the First record for that duplicate set. There are about 30,000 records in the table, and of them without the duplicates there is only about 4000 records.The duplicates are essential to work we do, so we cannot delete...
5
8990
by: jennwilson | last post by:
Using Access 2000 - I have a query that is suppose to return the records from table within specified time range and find matching data from another table . Table houses Clinician name, location and rate information Table houses Clinician name, location and number of units for each type of service rendered on each day. The two tables are...
0
7824
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8321
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7931
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8191
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5699
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3841
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1426
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1154
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.