Solved

finding next previous date

Posted on 2013-01-23
9
309 Views
Last Modified: 2013-02-14
Hi, I'm trying to find the previous date from the most current date. This is what I want
 
materialid      Receiveddt      unitid      nextrecevieddt
500130      4/12/2012      101896      6/30/2012
500130      6/30/2012      101126      8/31/2012
500130      8/31/2012      105261      

This is what I have so far

SELECT a.materialid, a.ReceivedDt, a.unitid,nextreceiveddt
FROM test1 AS a
LEFT JOIN (SELECT materialid, MIN(ReceivedDt) AS nextreceiveddt
            FROM test1 AS a
            WHERE materialid = '500130'
            GROUP BY vin) b
on b.materialid = a.materialid
AND b.nextreceiveddt <= a.ReceivedDt
WHERE a.vin = '500130'

materialid      Receiveddt      unitid      nextrecevieddt
500130      4/12/2012      101896      4/12/2012
500130      6/30/2012      101126      4/12/2012
500130      8/31/2012      105261      4/12/2012
0
Comment
Question by:elucero
[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
  • 4
  • 3
  • 2
9 Comments
 
LVL 27

Accepted Solution

by:
Chris Luttrell earned 325 total points
ID: 38812368
Maybe something like this:
CREATE TABLE test1 ( materialid INT , ReceivedDt DATE, unitid INT ,nextreceiveddt DATE);
INSERT INTO test1 VALUES 
(  500130,      '4/12/2012',      101896,      '6/30/2012'),
(  500130,      '6/30/2012',      101126,      '8/31/2012'),
(  500130,      '8/31/2012',      105261, NULL  )


SELECT a.materialid, a.ReceivedDt, a.unitid, a.nextreceiveddt 
FROM test1 AS a
LEFT JOIN (SELECT materialid, MIN(ReceivedDt) AS nextreceiveddt
            FROM test1 AS a
            WHERE materialid = '500130'
            GROUP BY materialid) b
on b.materialid = a.materialid
AND b.nextreceiveddt <= a.ReceivedDt
WHERE a.materialid = '500130'

;WITH CTE AS (
SELECT a.materialid, a.ReceivedDt, a.unitid, a.nextreceiveddt , ROW_NUMBER() OVER (PARTITION BY materialid ORDER BY receivedDt) rn
FROM test1 a
)
SELECT C1.materialid, C1.ReceivedDt, C1.unitid, C1.nextreceiveddt, C2.ReceivedDt AS PreviousreceivedDt
FROM CTE C1
LEFT OUTER JOIN CTE C2 ON C1.materialid = C2.materialid AND C1.rn - 1 = C2.rn


DROP TABLE test1;

Open in new window

results look like this
Results
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 38812378
Assuming you only have those first three columns in your table...

CREATE TABLE test1 ( materialid INT , ReceivedDt DATE, unitid INT);
INSERT INTO test1 VALUES 
(  500130,      '4/12/2012',      101896),
(  500130,      '6/30/2012',      101126),
(  500130,      '8/31/2012',      105261)

--get next date

SELECT a.materialid, a.Receiveddt, a.unitid, MIN(b.Receiveddt) AS NextReceiveddt
FROM test1 a LEFT JOIN
    test1 b ON a.materialid = b.materialid AND a.Receiveddt < b.Receiveddt
GROUP BY a.materialid, a.Receiveddt, a.unitid
ORDER BY a.materialid, a.Receiveddt, a.unitid

-- get previous date

SELECT a.materialid, a.Receiveddt, a.unitid, MAX(b.Receiveddt) AS PreviousReceiveddt
FROM test1 a LEFT JOIN
    test1 b ON a.materialid = b.materialid AND a.Receiveddt > b.Receiveddt
GROUP BY a.materialid, a.Receiveddt, a.unitid
ORDER BY a.materialid, a.Receiveddt, a.unitid

-- get previous date and next date

SELECT a.materialid, a.Receiveddt, a.unitid, MAX(c.Receiveddt) AS PreviousReceiveddt,
    MIN(b.Receiveddt) AS NextReceiveddt
FROM test1 a LEFT JOIN
    test1 b ON a.materialid = b.materialid AND a.Receiveddt < b.Receiveddt LEFT JOIN
    test1 c ON a.materialid = c.materialid AND a.Receiveddt > c.Receiveddt
GROUP BY a.materialid, a.Receiveddt, a.unitid
ORDER BY a.materialid, a.Receiveddt, a.unitid

DROP TABLE test1

Open in new window

0
 
LVL 27

Expert Comment

by:Chris Luttrell
ID: 38812478
oops, in my code above the first select was what you had to start, not part of the solution, just the CTE.
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

Author Comment

by:elucero
ID: 38812890
Thanks, what if there are more than 3 colums like 8?
0
 
LVL 27

Expert Comment

by:Chris Luttrell
ID: 38813046
The number of columns should not matter in the CTE example I gave. As long as they are not part of the key between rows.
0
 

Author Comment

by:elucero
ID: 38814701
thanks, what if they are part of the key?
0
 
LVL 27

Expert Comment

by:Chris Luttrell
ID: 38814715
then we just need to work them into the JOIN like I did with "ON C1.materialid = C2.materialid."  Just add them something like:
ON C1.materialid = C2.materialid AND C1.KeyColumn2 = C2.KeyColumn2 -- etc
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 38816218
elucero,

Did you try my suggestion in http:#a38812378 ?

Patrick
0
 

Author Comment

by:elucero
ID: 38833375
Hi, thanks this is what I'm looking for, but how can I make it faster. The data is being inserted into a table variable. This is what's making it so slow C1.rn - 1 = C2.rn
Thanks


;WITH CTE AS (
SELECT a.materialid, a.ReceivedDt, a.unitid, a.nextreceiveddt , ROW_NUMBER() OVER (PARTITION BY materialid ORDER BY receivedDt) rn
FROM test1 a
)
SELECT C1.materialid, C1.ReceivedDt, C1.unitid, C1.nextreceiveddt, C2.ReceivedDt AS PreviousreceivedDt
FROM CTE C1
LEFT OUTER JOIN CTE C2 ON C1.materialid = C2.materialid AND C1.rn - 1 = C2.rn
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Hi, I have heard from my friends that it’s not possible to create Label Printing report using SSRS. I am amazed after hearing this words not possible in SSRS. I googled lot and found that it is possible to some of people know about the Report Bui…
A recent questions about how to add SSRS named instances, couldn't find any that talks about SQL server 2008, anyway I decided to help by creating some screen shots. The installation is straightforward, you just pop the SQL server 2008 installati…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

617 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