Click here to Skip to main content
15,900,724 members
Home / Discussions / Database
   

Database

 
AnswerRe: SQL Remote Connections Pin
Richard Deeming18-Mar-19 9:35
mveRichard Deeming18-Mar-19 9:35 
GeneralRe: SQL Remote Connections Pin
Kevin Marois18-Mar-19 10:02
professionalKevin Marois18-Mar-19 10:02 
GeneralRe: SQL Remote Connections Pin
Richard Deeming19-Mar-19 1:57
mveRichard Deeming19-Mar-19 1:57 
GeneralRe: SQL Remote Connections Pin
Kevin Marois18-Apr-19 8:16
professionalKevin Marois18-Apr-19 8:16 
AnswerRe: SQL Remote Connections Pin
jschell23-Mar-19 6:01
jschell23-Mar-19 6:01 
QuestionHelp with simple database Pin
Member 1418292514-Mar-19 13:14
Member 1418292514-Mar-19 13:14 
AnswerRe: Help with simple database Pin
Richard MacCutchan14-Mar-19 23:06
mveRichard MacCutchan14-Mar-19 23:06 
AnswerRe: Help with simple database Pin
Eddy Vluggen14-Mar-19 23:17
professionalEddy Vluggen14-Mar-19 23:17 
AnswerRe: Help with simple database Pin
Mycroft Holmes15-Mar-19 12:49
professionalMycroft Holmes15-Mar-19 12:49 
PraiseRe: Help with simple database Pin
Peter_in_278015-Mar-19 14:20
professionalPeter_in_278015-Mar-19 14:20 
GeneralRe: Help with simple database Pin
Mycroft Holmes15-Mar-19 14:57
professionalMycroft Holmes15-Mar-19 14:57 
QuestionOracle database USER and SCHEMA [SOLVED] Pin
Valentinor11-Mar-19 9:16
Valentinor11-Mar-19 9:16 
AnswerRe: Oracle database USER and SCHEMA Pin
CHill6012-Mar-19 22:45
mveCHill6012-Mar-19 22:45 
GeneralRe: Oracle database USER and SCHEMA Pin
Valentinor13-Mar-19 1:30
Valentinor13-Mar-19 1:30 
GeneralRe: Oracle database USER and SCHEMA Pin
CHill6013-Mar-19 2:23
mveCHill6013-Mar-19 2:23 
GeneralRe: Oracle database USER and SCHEMA Pin
Valentinor13-Mar-19 3:04
Valentinor13-Mar-19 3:04 
AnswerRe: Oracle database USER and SCHEMA Pin
Eddy Vluggen13-Mar-19 0:59
professionalEddy Vluggen13-Mar-19 0:59 
QuestionDisplaying multiple rows into single row as columns based on id - Sql Server Pin
Member 111541886-Mar-19 20:18
Member 111541886-Mar-19 20:18 
SuggestionRe: Displaying multiple rows into single row as columns based on id - Sql Server Pin
CHill606-Mar-19 21:47
mveCHill606-Mar-19 21:47 
AnswerRe: Displaying multiple rows into single row as columns based on id - Sql Server Pin
Victor Nijegorodov6-Mar-19 21:52
Victor Nijegorodov6-Mar-19 21:52 
GeneralRe: Displaying multiple rows into single row as columns based on id - Sql Server Pin
Member 111541886-Mar-19 23:24
Member 111541886-Mar-19 23:24 
AnswerRe: Displaying multiple rows into single row as columns based on id - Sql Server Pin
Richard Deeming6-Mar-19 22:08
mveRichard Deeming6-Mar-19 22:08 
For SQL Server 2012 or later:
SQL
WITH cte As
(
    SELECT
        ROW_NUMBER() OVER (PARTITION BY EmpID ORDER BY RequestDate DESC) As RN,
        EmpID,
        RequestNo As CurrentReqNo,
        RequestDate As CurrentReqDate,
        RequestType As CurrentReqType,
        RequestStatus As CurrentReqStatus,
        LEAD(RequestNo) OVER (PARTITION BY EmpID ORDER BY RequestDate DESC) As PreviousReqNo,
        LEAD(RequestDate) OVER (PARTITION BY EmpID ORDER BY RequestDate DESC) As PreviousReqDate,
        LEAD(RequestType) OVER (PARTITION BY EmpID ORDER BY RequestDate DESC) As PreviousReqType,
        LEAD(RequestStatus) OVER (PARTITION BY EmpID ORDER BY RequestDate DESC) As PreviousReqStatus
    FROM
        YourTable
)
SELECT
    EmpID,
    CurrentReqNo,
    CurrentReqDate,
    CurrentReqType,
    CurrentReqStatus,
    PreviousReqNo,
    PreviousReqDate,
    PreviousReqType,
    PreviousReqStatus
FROM
    cte
WHERE
    RN = 1
;

LEAD (Transact-SQL) - SQL Server | Microsoft Docs[^]

For SQL Server 2008 or 2008 R2:
SQL
WITH cte As
(
    SELECT
        ROW_NUMBER() OVER (PARTITION BY EmpID ORDER BY RequestDate DESC) As RN,
        EmpID,
        RequestNo,
        RequestDate,
        RequestType,
        RequestStatus
    FROM
        YourTable
)
SELECT
    C.EmpID,
    C.RequestNo As CurrentReqNo,
    C.RequestDate As CurrentReqDate,
    C.RequestType As CurrentReqType,
    C.RequestStatus As CurrentReqStatus,
    P.RequestNo As PreviousReqNo,
    P.RequestDate As PreviousReqDate,
    P.RequestType As PreviousReqType,
    P.RequestStatus As PreviousReqStatus
FROM
    cte As C
    LEFT JOIN cte As P
    ON P.EmpID = C.EmpID
    And P.RN = C.RN + 1
WHERE
    C.RN = 1
;




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

GeneralRe: Displaying multiple rows into single row as columns based on id - Sql Server Pin
Member 111541886-Mar-19 23:21
Member 111541886-Mar-19 23:21 
GeneralRe: Displaying multiple rows into single row as columns based on id - Sql Server Pin
Mycroft Holmes7-Mar-19 10:50
professionalMycroft Holmes7-Mar-19 10:50 
QuestionHelp me with caculate debit balace and incredit by SQL server 2012 Pin
Member 141737536-Mar-19 19:08
Member 141737536-Mar-19 19:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.