Solved

Show records based on prior records

Posted on 2014-02-03
11
191 Views
Last Modified: 2014-02-05
Hello:

I have this table

PersonName        DateA             DateB
Bill              01-09-2014        01-29-2014
Bill              01-09-2014        01-16-2014
Bill              01-09-2014        02-24-2014
John            01-09-2014        02-16-2014
John            01-09-2014        01-29-2014
John            01-09-2014        03-08-2014

What I want to do is select all records by a date like the SQL statement below

SELECT PersonName, DateA, DateB where DateB = '01-29-2014'

However, if there is a record with an earlier date, I do not want that record to be shown. So using the table above only this record should show.

John            01-09-2014        01-29-2014
0
Comment
Question by:RecipeDan
11 Comments
 
LVL 39

Expert Comment

by:lcohan
ID: 39830958
you will need to use a MAX or TOP 1 and GROUP BY structure in your SQL statement.

{edit ValentinoV: links to competing site removed}
0
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39830974
you can write it with a not exists clause as shown below

SELECT PersonName, DateA, DateB
FROM <Your Table> as T1 
where DateB = '01-29-2014'
WHERE NOT EXISTS ( SELECT 1 FROM <your Table> as T2 where T2.DateB < T1.DateB) 

Open in new window


In the above code replace<your Table> with your table Name.
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39830980
I don't get it.

According to what you wrote, that query should give you 2 results:

Bill              01-09-2014        01-29-2014
John            01-09-2014        01-29-2014

Why is the "Bill" row not an acceptable result?
0
 
LVL 1

Author Comment

by:RecipeDan
ID: 39831201
Here is a code I came with. Are there any downsides or potential errors?
SELECT RowNumber, PersonName, DateA, DateB
FROM
(
SELECT ROW_NUMBER() OVER(PARTITION BY PersonName ORDER BY DateB ASC) RowNumber, PersonName, DateA, DateB
FROM            DataTest
)
table1
where DateB='01-29-2014' and RowNumber='1'

Open in new window

0
 
LVL 3

Expert Comment

by:smilieface
ID: 39831527
Try this. You'll need to substitute your table name for #PersonTable

WITH CTE_PersonTable AS (
   SELECT 
         PersonName
      FROM #PersonTable
      WHERE DateB <  CONVERT(DATE, '01-29-2014', 101)
      GROUP BY PersonName
   )
SELECT 
   *
   FROM #PersonTable
   LEFT OUTER JOIN CTE_PersonTable
      ON CTE_PersonTable.PersonName = #PersonTable.PersonName
   WHERE DateB = CONVERT(DATE, '01-29-2014', 101)
   AND   CTE_PersonTable.PersonName IS NULL

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 32

Expert Comment

by:ewangoya
ID: 39831840
I suppose its just the way you phrased your question that was a bit confusing but your query should work ok
0
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39831950
did you give this a try
SELECT PersonName, DateA, DateB
FROM <Your Table> as T1 
where DateB = '01-29-2014'
WHERE NOT EXISTS ( SELECT 1 FROM <your Table> as T2 where T2.DateB < T1.DateB) 

Open in new window


as described here http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/Q_28355426.html#a39830974
0
 
LVL 1

Author Comment

by:RecipeDan
ID: 39833040
Hi Surendra:

Yes I did try it. But it does not show any results.
SELECT PersonName, DateA, DateB
FROM DataTest as T1 
WHERE NOT EXISTS ( SELECT 1 FROM DataTest as T2 where T2.DateB < T1.DateB) AND DateB='2014-01-29'

Open in new window

0
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 500 total points
ID: 39833125
Oops there is one join condition that is missing over there corrected the same check the below one

declare @t table 
(
 PersonName VARCHAR(20), dateA DATETIME, DateB DATETIME
)
INSERT INTO @T VALUES ('Bill',              '01-09-2014',        '01-29-2014')
INSERT INTO @T VALUES ('Bill',              '01-09-2014',        '01-16-2014')
INSERT INTO @T VALUES ('Bill',              '01-09-2014',        '02-24-2014')

INSERT INTO @T VALUES ('John',              '01-09-2014',        '02-16-2014')
INSERT INTO @T VALUES ('John',              '01-09-2014',        '01-29-2014')
INSERT INTO @T VALUES ('John',              '01-09-2014',        '03-08-2014')

SELECT T1.*
FROM @T as T1 
where T1.DateB = '01-29-2014'
AND EXISTS ( SELECT 1 FROM @T as T2 where T2.DateB < T1.DateB and T1.PersonName = T2.PersonName) 

Open in new window


the @T table is created to check the validation of the statement, replace it with your own table.
0
 
LVL 1

Author Comment

by:RecipeDan
ID: 39833853
Yes it works. However, I had to change T2.DateB < T1.DateB to T2.DateB > T1.DateB. Thank you for your assistance.
0
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39833858
oops my mistake... actually it should be some thing like below

declare @t table 
(
 PersonName VARCHAR(20), dateA DATETIME, DateB DATETIME
)
INSERT INTO @T VALUES ('Bill',              '01-09-2014',        '01-29-2014')
INSERT INTO @T VALUES ('Bill',              '01-09-2014',        '01-16-2014')
INSERT INTO @T VALUES ('Bill',              '01-09-2014',        '02-24-2014')

INSERT INTO @T VALUES ('John',              '01-09-2014',        '02-16-2014')
INSERT INTO @T VALUES ('John',              '01-09-2014',        '01-29-2014')
INSERT INTO @T VALUES ('John',              '01-09-2014',        '03-08-2014')

SELECT T1.*
FROM @T as T1 
where T1.DateB = '01-29-2014'
AND NOT EXISTS ( SELECT 1 FROM @T as T2 where T2.DateB < T1.DateB and T1.PersonName = T2.PersonName) 

Open in new window

0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now