Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Show records based on prior records

Posted on 2014-02-03
11
Medium Priority
?
200 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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
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 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

721 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