Get last previous value of a record if condition is true
I have the following table:
+ ---------+------------+--------------+-------------+-------------+----------------+
| RecordId | Client | Subscription | Status | Date | Payment Method |
+ ---------+------------+--------------+-------------+-------------+----------------+
| ABC1234 | John Doe | NetFlex | Renewed | 13 Nov 2018 | Credit Card |
| ABC1233 | Jane Smith | SplitiFy | Not Renewed | 01 Sep 2018 | NULL |
| ABC1232 | John Doe | NetFlex | Renewed | 13 Nov 2017 | PayPart |
| ABC1231 | Jane Smith | SplitiFy | Renewed | 01 Sep 2017 | Cash |
| ABC1230 | John Doe | NetFlex | Renewed | 13 Nov 2016 | Cash |
+ ---------+------------+--------------+-------------+-------------+----------------+
I would like to have a new column in which I can provide the previous payment method for a specific client, and setting it to NULL
if there was not a previous payment. In other words, I would like to have the following information
+ ---------+------------+--------------+-------------+-------------+----------------+-------------------------+
| RecordId | Client | Subscription | Status | Date | Payment Method | Previous Payment Method |
+ ---------+------------+--------------+-------------+-------------+----------------+-------------------------+
| ABC1234 | John Doe | NetFlex | Renewed | 13 Nov 2018 | Credit Card | PayPart |
| ABC1233 | Jane Smith | SplitiFy | Not Renewed | 01 Sep 2018 | None | Cash |
| ABC1232 | John Doe | NetFlex | Renewed | 13 Nov 2017 | PayPart | Cash |
| ABC1231 | Jane Smith | SplitiFy | Renewed | 01 Sep 2017 | Cash | NULL |
| ABC1230 | John Doe | NetFlex | Renewed | 13 Nov 2016 | Cash | NULL |
+ ---------+------------+--------------+-------------+-------------+----------------+-------------------------+
Does someone know how to create this latter table?
sql database tsql database-design
add a comment |
I have the following table:
+ ---------+------------+--------------+-------------+-------------+----------------+
| RecordId | Client | Subscription | Status | Date | Payment Method |
+ ---------+------------+--------------+-------------+-------------+----------------+
| ABC1234 | John Doe | NetFlex | Renewed | 13 Nov 2018 | Credit Card |
| ABC1233 | Jane Smith | SplitiFy | Not Renewed | 01 Sep 2018 | NULL |
| ABC1232 | John Doe | NetFlex | Renewed | 13 Nov 2017 | PayPart |
| ABC1231 | Jane Smith | SplitiFy | Renewed | 01 Sep 2017 | Cash |
| ABC1230 | John Doe | NetFlex | Renewed | 13 Nov 2016 | Cash |
+ ---------+------------+--------------+-------------+-------------+----------------+
I would like to have a new column in which I can provide the previous payment method for a specific client, and setting it to NULL
if there was not a previous payment. In other words, I would like to have the following information
+ ---------+------------+--------------+-------------+-------------+----------------+-------------------------+
| RecordId | Client | Subscription | Status | Date | Payment Method | Previous Payment Method |
+ ---------+------------+--------------+-------------+-------------+----------------+-------------------------+
| ABC1234 | John Doe | NetFlex | Renewed | 13 Nov 2018 | Credit Card | PayPart |
| ABC1233 | Jane Smith | SplitiFy | Not Renewed | 01 Sep 2018 | None | Cash |
| ABC1232 | John Doe | NetFlex | Renewed | 13 Nov 2017 | PayPart | Cash |
| ABC1231 | Jane Smith | SplitiFy | Renewed | 01 Sep 2017 | Cash | NULL |
| ABC1230 | John Doe | NetFlex | Renewed | 13 Nov 2016 | Cash | NULL |
+ ---------+------------+--------------+-------------+-------------+----------------+-------------------------+
Does someone know how to create this latter table?
sql database tsql database-design
Which is the version of SQL Server that you are using?
– gotqn
Nov 13 '18 at 7:55
add a comment |
I have the following table:
+ ---------+------------+--------------+-------------+-------------+----------------+
| RecordId | Client | Subscription | Status | Date | Payment Method |
+ ---------+------------+--------------+-------------+-------------+----------------+
| ABC1234 | John Doe | NetFlex | Renewed | 13 Nov 2018 | Credit Card |
| ABC1233 | Jane Smith | SplitiFy | Not Renewed | 01 Sep 2018 | NULL |
| ABC1232 | John Doe | NetFlex | Renewed | 13 Nov 2017 | PayPart |
| ABC1231 | Jane Smith | SplitiFy | Renewed | 01 Sep 2017 | Cash |
| ABC1230 | John Doe | NetFlex | Renewed | 13 Nov 2016 | Cash |
+ ---------+------------+--------------+-------------+-------------+----------------+
I would like to have a new column in which I can provide the previous payment method for a specific client, and setting it to NULL
if there was not a previous payment. In other words, I would like to have the following information
+ ---------+------------+--------------+-------------+-------------+----------------+-------------------------+
| RecordId | Client | Subscription | Status | Date | Payment Method | Previous Payment Method |
+ ---------+------------+--------------+-------------+-------------+----------------+-------------------------+
| ABC1234 | John Doe | NetFlex | Renewed | 13 Nov 2018 | Credit Card | PayPart |
| ABC1233 | Jane Smith | SplitiFy | Not Renewed | 01 Sep 2018 | None | Cash |
| ABC1232 | John Doe | NetFlex | Renewed | 13 Nov 2017 | PayPart | Cash |
| ABC1231 | Jane Smith | SplitiFy | Renewed | 01 Sep 2017 | Cash | NULL |
| ABC1230 | John Doe | NetFlex | Renewed | 13 Nov 2016 | Cash | NULL |
+ ---------+------------+--------------+-------------+-------------+----------------+-------------------------+
Does someone know how to create this latter table?
sql database tsql database-design
I have the following table:
+ ---------+------------+--------------+-------------+-------------+----------------+
| RecordId | Client | Subscription | Status | Date | Payment Method |
+ ---------+------------+--------------+-------------+-------------+----------------+
| ABC1234 | John Doe | NetFlex | Renewed | 13 Nov 2018 | Credit Card |
| ABC1233 | Jane Smith | SplitiFy | Not Renewed | 01 Sep 2018 | NULL |
| ABC1232 | John Doe | NetFlex | Renewed | 13 Nov 2017 | PayPart |
| ABC1231 | Jane Smith | SplitiFy | Renewed | 01 Sep 2017 | Cash |
| ABC1230 | John Doe | NetFlex | Renewed | 13 Nov 2016 | Cash |
+ ---------+------------+--------------+-------------+-------------+----------------+
I would like to have a new column in which I can provide the previous payment method for a specific client, and setting it to NULL
if there was not a previous payment. In other words, I would like to have the following information
+ ---------+------------+--------------+-------------+-------------+----------------+-------------------------+
| RecordId | Client | Subscription | Status | Date | Payment Method | Previous Payment Method |
+ ---------+------------+--------------+-------------+-------------+----------------+-------------------------+
| ABC1234 | John Doe | NetFlex | Renewed | 13 Nov 2018 | Credit Card | PayPart |
| ABC1233 | Jane Smith | SplitiFy | Not Renewed | 01 Sep 2018 | None | Cash |
| ABC1232 | John Doe | NetFlex | Renewed | 13 Nov 2017 | PayPart | Cash |
| ABC1231 | Jane Smith | SplitiFy | Renewed | 01 Sep 2017 | Cash | NULL |
| ABC1230 | John Doe | NetFlex | Renewed | 13 Nov 2016 | Cash | NULL |
+ ---------+------------+--------------+-------------+-------------+----------------+-------------------------+
Does someone know how to create this latter table?
sql database tsql database-design
sql database tsql database-design
edited Nov 13 '18 at 8:00
Thorsten Kettner
50.6k22542
50.6k22542
asked Nov 13 '18 at 7:43
HumbertoHumberto
133
133
Which is the version of SQL Server that you are using?
– gotqn
Nov 13 '18 at 7:55
add a comment |
Which is the version of SQL Server that you are using?
– gotqn
Nov 13 '18 at 7:55
Which is the version of SQL Server that you are using?
– gotqn
Nov 13 '18 at 7:55
Which is the version of SQL Server that you are using?
– gotqn
Nov 13 '18 at 7:55
add a comment |
1 Answer
1
active
oldest
votes
If you are using SQL Server 2012 or higher then you can make use of LAG
which will return data from a previous row in the same result set without the use of a self-join
So here we can use LAG
In combination with PARTITION BY
to get the Expected Result
SELECT RecordId,Client,Subscription,Status,Date,PaymentMethod
,LAG (PaymentMethod, 1, 0) OVER (PARTITION BY Client,Subscription ORDER BY Date)
AS PreviousPaymentMethod
FROM Table
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276105%2fget-last-previous-value-of-a-record-if-condition-is-true%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you are using SQL Server 2012 or higher then you can make use of LAG
which will return data from a previous row in the same result set without the use of a self-join
So here we can use LAG
In combination with PARTITION BY
to get the Expected Result
SELECT RecordId,Client,Subscription,Status,Date,PaymentMethod
,LAG (PaymentMethod, 1, 0) OVER (PARTITION BY Client,Subscription ORDER BY Date)
AS PreviousPaymentMethod
FROM Table
add a comment |
If you are using SQL Server 2012 or higher then you can make use of LAG
which will return data from a previous row in the same result set without the use of a self-join
So here we can use LAG
In combination with PARTITION BY
to get the Expected Result
SELECT RecordId,Client,Subscription,Status,Date,PaymentMethod
,LAG (PaymentMethod, 1, 0) OVER (PARTITION BY Client,Subscription ORDER BY Date)
AS PreviousPaymentMethod
FROM Table
add a comment |
If you are using SQL Server 2012 or higher then you can make use of LAG
which will return data from a previous row in the same result set without the use of a self-join
So here we can use LAG
In combination with PARTITION BY
to get the Expected Result
SELECT RecordId,Client,Subscription,Status,Date,PaymentMethod
,LAG (PaymentMethod, 1, 0) OVER (PARTITION BY Client,Subscription ORDER BY Date)
AS PreviousPaymentMethod
FROM Table
If you are using SQL Server 2012 or higher then you can make use of LAG
which will return data from a previous row in the same result set without the use of a self-join
So here we can use LAG
In combination with PARTITION BY
to get the Expected Result
SELECT RecordId,Client,Subscription,Status,Date,PaymentMethod
,LAG (PaymentMethod, 1, 0) OVER (PARTITION BY Client,Subscription ORDER BY Date)
AS PreviousPaymentMethod
FROM Table
edited Nov 13 '18 at 8:12
answered Nov 13 '18 at 8:03
Sanal SunnySanal Sunny
6628
6628
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276105%2fget-last-previous-value-of-a-record-if-condition-is-true%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Which is the version of SQL Server that you are using?
– gotqn
Nov 13 '18 at 7:55