Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Show records based on prior records

Posted on 2014-02-03
11
Medium Priority
?
201 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 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
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
I have a large data set and a SSIS package. How can I load this file in multi threading?
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.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

885 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