| Author |
Topic |
|
learntsql
524 Posts |
Posted - 2011-08-08 : 02:51:57
|
| Hi All,DECLARE @travel table(ID int ,RaiseID int,PassengerID varchar(50),TravelFromDate datetime ,TravelToDate datetime)INSERT INTO @travelSELECT 1,120,'P1','6-Aug-2011','7-Aug-2011'union allSELECT 2,123,'P1','7-Aug-2011','7-Aug-2011'union allSELECT 3,121,'P2','6-Aug-2011','7-Aug-2011'union allSELECT 4,122,'P2','7-Aug-2011','9-Aug-2011'union allSELECT 5,125,'P3','6-Aug-2011','7-Aug-2011'union allSELECT 6,126,'P3','8-Aug-2011','9-Aug-2011'select * from @travelHere my requirement is when for same passenger prev TravelTodate and next TravelFromdate are same need to return that passengerid and date and if possible any text as status.Please guide me.TIA |
|
|
Sachin.Nand
2937 Posts |
Posted - 2011-08-08 : 03:44:57
|
| So what should be the output of the above example ?PBUH |
 |
|
|
learntsql
524 Posts |
Posted - 2011-08-08 : 03:50:31
|
| Here my output is'P1','7-Aug-2011','Mapped''P2','7-Aug-2011','Mapped' |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-08-08 : 04:49:51
|
| [code]SELECT t.PassengerID,t.TravelFromDate,'Mapped'FROM Table tCROSS APPLY (SELECT TOP 1 TravelToDate FROM Table WHERE PassengerID =t.PassengerID AND TravelToDate <= t.TravelFromDate ORDER BY TravelToDate DESC)t1WHERE t1.TravelToDate = t.TravelFromDate[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
learntsql
524 Posts |
Posted - 2011-08-08 : 04:52:51
|
| ThanQ Visakh, |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-08-08 : 04:53:16
|
| wc------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
learntsql
524 Posts |
Posted - 2011-08-11 : 05:27:40
|
| Sorry Guys ,I have one more query here plz..for following dataDECLARE @travel table(ID int ,RaiseID int,PassengerID varchar(50),TravelFromDate datetime ,TravelToDate datetime)INSERT INTO @travelSELECT 1,120,'P1','6-Aug-2011','7-Aug-2011'union allSELECT 2,123,'P1','7-Aug-2011','7-Aug-2011'union allSELECT 3,121,'P2','6-Aug-2011','7-Aug-2011'union allSELECT 4,122,'P2','7-Aug-2011','9-Aug-2011'union allSELECT 5,125,'P3','6-Aug-2011','7-Aug-2011'union allSELECT 6,126,'P3','8-Aug-2011','9-Aug-2011'union allSELECT 7,129,'P4','5-Aug-2011','5-Aug-2011'union allSELECT 8,130,'P4','5-Aug-2011','5-Aug-2011'union allSELECT 9,131,'P1','5-Aug-2011','5-Aug-2011'SELECT t.PassengerID,t.TravelFromDate,t.TravelToDate,'Mapped'FROM @travel tCROSS APPLY (SELECT TOP 1 TravelToDate FROM @travel WHERE PassengerID =t.PassengerID AND TravelToDate <= t.TravelFromDate AND RaiseID < = t.RaiseID ORDER BY TravelToDate DESC )t1WHERE t1.TravelToDate = t.TravelFromDateThat is single passenger can apply travel request two times for same day(i,e. morning and afternoon)in this case it shoud appear in final select as for "P4" passenger here.but when he applies only either for morning or afternoon then it should not appear as like for P1 on Aug-05.Please guide me.TIA. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-08-11 : 07:52:12
|
| [code]SELECT t.PassengerID,t.TravelFromDate,t.TravelToDate,'Mapped'FROM @travel tCROSS APPLY (SELECT TOP 1 TravelToDateFROM @travelWHERE PassengerID =t.PassengerID AND TravelToDate <= t.TravelFromDate AND RaiseID < t.RaiseIDORDER BY TravelToDate DESC)t1WHERE t1.TravelToDate = t.TravelFromDate[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|