Solved

Show records based on prior records

Posted on 2014-02-03
11
198 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 40

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 35

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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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
 
LVL 32

Expert Comment

by:Ephraim Wangoya
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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

631 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