MySQL Lead function not working. I have tried but its working as lag function. Kindly help me
I am trying to implement the lead function functionality in select query from yesterday onwards but its not working for me.
SELECT inbp_ingangsdatum,
previous
FROM (SELECT ibp.inbp_ingangsdatum,
@prev previous,
@prev := inbp_ingangsdatum AS prev
FROM base ibp,
(SELECT @prev := '') r
ORDER BY inbp_ingangsdatum) AS t1;
Current Output
val previous
20090101
20120401 20090101
20120402 20120401
20120403 20120402
20120404 20120403
Expected Output
val previous
20090101 20120401
20120401 20120402
20120402 20120403
20120403 20120404
20120404 20120405
mysql database lead
add a comment |
I am trying to implement the lead function functionality in select query from yesterday onwards but its not working for me.
SELECT inbp_ingangsdatum,
previous
FROM (SELECT ibp.inbp_ingangsdatum,
@prev previous,
@prev := inbp_ingangsdatum AS prev
FROM base ibp,
(SELECT @prev := '') r
ORDER BY inbp_ingangsdatum) AS t1;
Current Output
val previous
20090101
20120401 20090101
20120402 20120401
20120403 20120402
20120404 20120403
Expected Output
val previous
20090101 20120401
20120401 20120402
20120402 20120403
20120403 20120404
20120404 20120405
mysql database lead
Does it work if you order byDESC
inside the numbering sub query?
– Salman A
Nov 22 '18 at 10:26
no its not working .
– Naren P
Nov 22 '18 at 10:56
@NarenP in recent versions of MySQL; an explicit ordering needs to be done first in a Derived table, and then in outer subquery, session variables are utilized. Otherwise the order is not guaranteed
– Madhur Bhaiya
Nov 22 '18 at 10:59
@Madhur I am Using MYSQL 5.6
– Naren P
Nov 22 '18 at 11:01
@NarenP Please see: How to accept an answer for closure. You get points for it as well. Thanks :)
– Madhur Bhaiya
Nov 26 '18 at 10:54
add a comment |
I am trying to implement the lead function functionality in select query from yesterday onwards but its not working for me.
SELECT inbp_ingangsdatum,
previous
FROM (SELECT ibp.inbp_ingangsdatum,
@prev previous,
@prev := inbp_ingangsdatum AS prev
FROM base ibp,
(SELECT @prev := '') r
ORDER BY inbp_ingangsdatum) AS t1;
Current Output
val previous
20090101
20120401 20090101
20120402 20120401
20120403 20120402
20120404 20120403
Expected Output
val previous
20090101 20120401
20120401 20120402
20120402 20120403
20120403 20120404
20120404 20120405
mysql database lead
I am trying to implement the lead function functionality in select query from yesterday onwards but its not working for me.
SELECT inbp_ingangsdatum,
previous
FROM (SELECT ibp.inbp_ingangsdatum,
@prev previous,
@prev := inbp_ingangsdatum AS prev
FROM base ibp,
(SELECT @prev := '') r
ORDER BY inbp_ingangsdatum) AS t1;
Current Output
val previous
20090101
20120401 20090101
20120402 20120401
20120403 20120402
20120404 20120403
Expected Output
val previous
20090101 20120401
20120401 20120402
20120402 20120403
20120403 20120404
20120404 20120405
mysql database lead
mysql database lead
edited Nov 22 '18 at 11:03
Madhur Bhaiya
19.5k62236
19.5k62236
asked Nov 22 '18 at 10:22
Naren PNaren P
1818
1818
Does it work if you order byDESC
inside the numbering sub query?
– Salman A
Nov 22 '18 at 10:26
no its not working .
– Naren P
Nov 22 '18 at 10:56
@NarenP in recent versions of MySQL; an explicit ordering needs to be done first in a Derived table, and then in outer subquery, session variables are utilized. Otherwise the order is not guaranteed
– Madhur Bhaiya
Nov 22 '18 at 10:59
@Madhur I am Using MYSQL 5.6
– Naren P
Nov 22 '18 at 11:01
@NarenP Please see: How to accept an answer for closure. You get points for it as well. Thanks :)
– Madhur Bhaiya
Nov 26 '18 at 10:54
add a comment |
Does it work if you order byDESC
inside the numbering sub query?
– Salman A
Nov 22 '18 at 10:26
no its not working .
– Naren P
Nov 22 '18 at 10:56
@NarenP in recent versions of MySQL; an explicit ordering needs to be done first in a Derived table, and then in outer subquery, session variables are utilized. Otherwise the order is not guaranteed
– Madhur Bhaiya
Nov 22 '18 at 10:59
@Madhur I am Using MYSQL 5.6
– Naren P
Nov 22 '18 at 11:01
@NarenP Please see: How to accept an answer for closure. You get points for it as well. Thanks :)
– Madhur Bhaiya
Nov 26 '18 at 10:54
Does it work if you order by
DESC
inside the numbering sub query?– Salman A
Nov 22 '18 at 10:26
Does it work if you order by
DESC
inside the numbering sub query?– Salman A
Nov 22 '18 at 10:26
no its not working .
– Naren P
Nov 22 '18 at 10:56
no its not working .
– Naren P
Nov 22 '18 at 10:56
@NarenP in recent versions of MySQL; an explicit ordering needs to be done first in a Derived table, and then in outer subquery, session variables are utilized. Otherwise the order is not guaranteed
– Madhur Bhaiya
Nov 22 '18 at 10:59
@NarenP in recent versions of MySQL; an explicit ordering needs to be done first in a Derived table, and then in outer subquery, session variables are utilized. Otherwise the order is not guaranteed
– Madhur Bhaiya
Nov 22 '18 at 10:59
@Madhur I am Using MYSQL 5.6
– Naren P
Nov 22 '18 at 11:01
@Madhur I am Using MYSQL 5.6
– Naren P
Nov 22 '18 at 11:01
@NarenP Please see: How to accept an answer for closure. You get points for it as well. Thanks :)
– Madhur Bhaiya
Nov 26 '18 at 10:54
@NarenP Please see: How to accept an answer for closure. You get points for it as well. Thanks :)
– Madhur Bhaiya
Nov 26 '18 at 10:54
add a comment |
2 Answers
2
active
oldest
votes
LEAD()
function fetches the "next row" value. I don't know why are you calling it as prev
. I am using the alias next
instead for clarity.
While using user-defined variables, we can access previous row's value in SELECT
clause, but getting value of the next (upcoming) row is tricky. We will have to explicitly order by inbp_ingangsdatum
in Descending order (opposite to our final sorting requirement).
Now, we will use this result-set to get the "next" row. Due to reversed order, previous value is basically the next value.
We will finally reorder the result-set in outermost query.
Schema (MySQL v5.6)
Create table base (inbp_ingangsdatum int);
insert into base
values
(20090101),
(20120401),
(20120402),
(20120403),
(20120404),
(20120405);
Query #1
SELECT
dt2.val, dt2.next
FROM
(
SELECT
@nxt AS next,
@nxt := dt.inbp_ingangsdatum AS val
FROM
(
SELECT
ibp.inbp_ingangsdatum
FROM base AS ibp
ORDER BY ibp.inbp_ingangsdatum DESC
) AS dt
CROSS JOIN (SELECT @nxt := '') AS user_init_vars
) AS dt2
ORDER BY dt2.val;
Result:
| val | next |
| -------- | -------- |
| 20090101 | 20120401 |
| 20120401 | 20120402 |
| 20120402 | 20120403 |
| 20120403 | 20120404 |
| 20120404 | 20120405 |
| 20120405 | |
View on DB Fiddle
add a comment |
I would use a correlated sub query and pay extra attention to ties:
SELECT INSD_ID, inbp_ingangsdatum, (
SELECT inbp_ingangsdatum
FROM base AS x
WHERE (inbp_ingangsdatum = base.inbp_ingangsdatum AND INSD_ID > base.INSD_ID)
OR (inbp_ingangsdatum > base.inbp_ingangsdatum)
ORDER BY inbp_ingangsdatum
LIMIT 1
) AS next_date
FROM base
ORDER BY inbp_ingangsdatum, INSD_ID
Thanks.but,i got this answer. output: 20090101 20120401 20120401 20121001 20120401 20121001 20120401 20121001 20120401 20121001 20120401 20121001
– Naren P
Nov 22 '18 at 10:53
Seems like you have "ties". You need a tie breaker column such as ID/primary key. Do you have one?
– Salman A
Nov 22 '18 at 11:01
Yes INSD_ID colums contains primary key
– Naren P
Nov 22 '18 at 11:06
@NarenP see revised answer. You can use it if you have problems getting correct results with user variables.
– Salman A
Nov 22 '18 at 11:41
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%2f53428751%2fmysql-lead-function-not-working-i-have-tried-but-its-working-as-lag-function-k%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
LEAD()
function fetches the "next row" value. I don't know why are you calling it as prev
. I am using the alias next
instead for clarity.
While using user-defined variables, we can access previous row's value in SELECT
clause, but getting value of the next (upcoming) row is tricky. We will have to explicitly order by inbp_ingangsdatum
in Descending order (opposite to our final sorting requirement).
Now, we will use this result-set to get the "next" row. Due to reversed order, previous value is basically the next value.
We will finally reorder the result-set in outermost query.
Schema (MySQL v5.6)
Create table base (inbp_ingangsdatum int);
insert into base
values
(20090101),
(20120401),
(20120402),
(20120403),
(20120404),
(20120405);
Query #1
SELECT
dt2.val, dt2.next
FROM
(
SELECT
@nxt AS next,
@nxt := dt.inbp_ingangsdatum AS val
FROM
(
SELECT
ibp.inbp_ingangsdatum
FROM base AS ibp
ORDER BY ibp.inbp_ingangsdatum DESC
) AS dt
CROSS JOIN (SELECT @nxt := '') AS user_init_vars
) AS dt2
ORDER BY dt2.val;
Result:
| val | next |
| -------- | -------- |
| 20090101 | 20120401 |
| 20120401 | 20120402 |
| 20120402 | 20120403 |
| 20120403 | 20120404 |
| 20120404 | 20120405 |
| 20120405 | |
View on DB Fiddle
add a comment |
LEAD()
function fetches the "next row" value. I don't know why are you calling it as prev
. I am using the alias next
instead for clarity.
While using user-defined variables, we can access previous row's value in SELECT
clause, but getting value of the next (upcoming) row is tricky. We will have to explicitly order by inbp_ingangsdatum
in Descending order (opposite to our final sorting requirement).
Now, we will use this result-set to get the "next" row. Due to reversed order, previous value is basically the next value.
We will finally reorder the result-set in outermost query.
Schema (MySQL v5.6)
Create table base (inbp_ingangsdatum int);
insert into base
values
(20090101),
(20120401),
(20120402),
(20120403),
(20120404),
(20120405);
Query #1
SELECT
dt2.val, dt2.next
FROM
(
SELECT
@nxt AS next,
@nxt := dt.inbp_ingangsdatum AS val
FROM
(
SELECT
ibp.inbp_ingangsdatum
FROM base AS ibp
ORDER BY ibp.inbp_ingangsdatum DESC
) AS dt
CROSS JOIN (SELECT @nxt := '') AS user_init_vars
) AS dt2
ORDER BY dt2.val;
Result:
| val | next |
| -------- | -------- |
| 20090101 | 20120401 |
| 20120401 | 20120402 |
| 20120402 | 20120403 |
| 20120403 | 20120404 |
| 20120404 | 20120405 |
| 20120405 | |
View on DB Fiddle
add a comment |
LEAD()
function fetches the "next row" value. I don't know why are you calling it as prev
. I am using the alias next
instead for clarity.
While using user-defined variables, we can access previous row's value in SELECT
clause, but getting value of the next (upcoming) row is tricky. We will have to explicitly order by inbp_ingangsdatum
in Descending order (opposite to our final sorting requirement).
Now, we will use this result-set to get the "next" row. Due to reversed order, previous value is basically the next value.
We will finally reorder the result-set in outermost query.
Schema (MySQL v5.6)
Create table base (inbp_ingangsdatum int);
insert into base
values
(20090101),
(20120401),
(20120402),
(20120403),
(20120404),
(20120405);
Query #1
SELECT
dt2.val, dt2.next
FROM
(
SELECT
@nxt AS next,
@nxt := dt.inbp_ingangsdatum AS val
FROM
(
SELECT
ibp.inbp_ingangsdatum
FROM base AS ibp
ORDER BY ibp.inbp_ingangsdatum DESC
) AS dt
CROSS JOIN (SELECT @nxt := '') AS user_init_vars
) AS dt2
ORDER BY dt2.val;
Result:
| val | next |
| -------- | -------- |
| 20090101 | 20120401 |
| 20120401 | 20120402 |
| 20120402 | 20120403 |
| 20120403 | 20120404 |
| 20120404 | 20120405 |
| 20120405 | |
View on DB Fiddle
LEAD()
function fetches the "next row" value. I don't know why are you calling it as prev
. I am using the alias next
instead for clarity.
While using user-defined variables, we can access previous row's value in SELECT
clause, but getting value of the next (upcoming) row is tricky. We will have to explicitly order by inbp_ingangsdatum
in Descending order (opposite to our final sorting requirement).
Now, we will use this result-set to get the "next" row. Due to reversed order, previous value is basically the next value.
We will finally reorder the result-set in outermost query.
Schema (MySQL v5.6)
Create table base (inbp_ingangsdatum int);
insert into base
values
(20090101),
(20120401),
(20120402),
(20120403),
(20120404),
(20120405);
Query #1
SELECT
dt2.val, dt2.next
FROM
(
SELECT
@nxt AS next,
@nxt := dt.inbp_ingangsdatum AS val
FROM
(
SELECT
ibp.inbp_ingangsdatum
FROM base AS ibp
ORDER BY ibp.inbp_ingangsdatum DESC
) AS dt
CROSS JOIN (SELECT @nxt := '') AS user_init_vars
) AS dt2
ORDER BY dt2.val;
Result:
| val | next |
| -------- | -------- |
| 20090101 | 20120401 |
| 20120401 | 20120402 |
| 20120402 | 20120403 |
| 20120403 | 20120404 |
| 20120404 | 20120405 |
| 20120405 | |
View on DB Fiddle
edited Nov 22 '18 at 11:45
answered Nov 22 '18 at 11:11
Madhur BhaiyaMadhur Bhaiya
19.5k62236
19.5k62236
add a comment |
add a comment |
I would use a correlated sub query and pay extra attention to ties:
SELECT INSD_ID, inbp_ingangsdatum, (
SELECT inbp_ingangsdatum
FROM base AS x
WHERE (inbp_ingangsdatum = base.inbp_ingangsdatum AND INSD_ID > base.INSD_ID)
OR (inbp_ingangsdatum > base.inbp_ingangsdatum)
ORDER BY inbp_ingangsdatum
LIMIT 1
) AS next_date
FROM base
ORDER BY inbp_ingangsdatum, INSD_ID
Thanks.but,i got this answer. output: 20090101 20120401 20120401 20121001 20120401 20121001 20120401 20121001 20120401 20121001 20120401 20121001
– Naren P
Nov 22 '18 at 10:53
Seems like you have "ties". You need a tie breaker column such as ID/primary key. Do you have one?
– Salman A
Nov 22 '18 at 11:01
Yes INSD_ID colums contains primary key
– Naren P
Nov 22 '18 at 11:06
@NarenP see revised answer. You can use it if you have problems getting correct results with user variables.
– Salman A
Nov 22 '18 at 11:41
add a comment |
I would use a correlated sub query and pay extra attention to ties:
SELECT INSD_ID, inbp_ingangsdatum, (
SELECT inbp_ingangsdatum
FROM base AS x
WHERE (inbp_ingangsdatum = base.inbp_ingangsdatum AND INSD_ID > base.INSD_ID)
OR (inbp_ingangsdatum > base.inbp_ingangsdatum)
ORDER BY inbp_ingangsdatum
LIMIT 1
) AS next_date
FROM base
ORDER BY inbp_ingangsdatum, INSD_ID
Thanks.but,i got this answer. output: 20090101 20120401 20120401 20121001 20120401 20121001 20120401 20121001 20120401 20121001 20120401 20121001
– Naren P
Nov 22 '18 at 10:53
Seems like you have "ties". You need a tie breaker column such as ID/primary key. Do you have one?
– Salman A
Nov 22 '18 at 11:01
Yes INSD_ID colums contains primary key
– Naren P
Nov 22 '18 at 11:06
@NarenP see revised answer. You can use it if you have problems getting correct results with user variables.
– Salman A
Nov 22 '18 at 11:41
add a comment |
I would use a correlated sub query and pay extra attention to ties:
SELECT INSD_ID, inbp_ingangsdatum, (
SELECT inbp_ingangsdatum
FROM base AS x
WHERE (inbp_ingangsdatum = base.inbp_ingangsdatum AND INSD_ID > base.INSD_ID)
OR (inbp_ingangsdatum > base.inbp_ingangsdatum)
ORDER BY inbp_ingangsdatum
LIMIT 1
) AS next_date
FROM base
ORDER BY inbp_ingangsdatum, INSD_ID
I would use a correlated sub query and pay extra attention to ties:
SELECT INSD_ID, inbp_ingangsdatum, (
SELECT inbp_ingangsdatum
FROM base AS x
WHERE (inbp_ingangsdatum = base.inbp_ingangsdatum AND INSD_ID > base.INSD_ID)
OR (inbp_ingangsdatum > base.inbp_ingangsdatum)
ORDER BY inbp_ingangsdatum
LIMIT 1
) AS next_date
FROM base
ORDER BY inbp_ingangsdatum, INSD_ID
edited Nov 22 '18 at 13:16
answered Nov 22 '18 at 10:29
Salman ASalman A
177k66337426
177k66337426
Thanks.but,i got this answer. output: 20090101 20120401 20120401 20121001 20120401 20121001 20120401 20121001 20120401 20121001 20120401 20121001
– Naren P
Nov 22 '18 at 10:53
Seems like you have "ties". You need a tie breaker column such as ID/primary key. Do you have one?
– Salman A
Nov 22 '18 at 11:01
Yes INSD_ID colums contains primary key
– Naren P
Nov 22 '18 at 11:06
@NarenP see revised answer. You can use it if you have problems getting correct results with user variables.
– Salman A
Nov 22 '18 at 11:41
add a comment |
Thanks.but,i got this answer. output: 20090101 20120401 20120401 20121001 20120401 20121001 20120401 20121001 20120401 20121001 20120401 20121001
– Naren P
Nov 22 '18 at 10:53
Seems like you have "ties". You need a tie breaker column such as ID/primary key. Do you have one?
– Salman A
Nov 22 '18 at 11:01
Yes INSD_ID colums contains primary key
– Naren P
Nov 22 '18 at 11:06
@NarenP see revised answer. You can use it if you have problems getting correct results with user variables.
– Salman A
Nov 22 '18 at 11:41
Thanks.but,i got this answer. output: 20090101 20120401 20120401 20121001 20120401 20121001 20120401 20121001 20120401 20121001 20120401 20121001
– Naren P
Nov 22 '18 at 10:53
Thanks.but,i got this answer. output: 20090101 20120401 20120401 20121001 20120401 20121001 20120401 20121001 20120401 20121001 20120401 20121001
– Naren P
Nov 22 '18 at 10:53
Seems like you have "ties". You need a tie breaker column such as ID/primary key. Do you have one?
– Salman A
Nov 22 '18 at 11:01
Seems like you have "ties". You need a tie breaker column such as ID/primary key. Do you have one?
– Salman A
Nov 22 '18 at 11:01
Yes INSD_ID colums contains primary key
– Naren P
Nov 22 '18 at 11:06
Yes INSD_ID colums contains primary key
– Naren P
Nov 22 '18 at 11:06
@NarenP see revised answer. You can use it if you have problems getting correct results with user variables.
– Salman A
Nov 22 '18 at 11:41
@NarenP see revised answer. You can use it if you have problems getting correct results with user variables.
– Salman A
Nov 22 '18 at 11:41
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.
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%2f53428751%2fmysql-lead-function-not-working-i-have-tried-but-its-working-as-lag-function-k%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
Does it work if you order by
DESC
inside the numbering sub query?– Salman A
Nov 22 '18 at 10:26
no its not working .
– Naren P
Nov 22 '18 at 10:56
@NarenP in recent versions of MySQL; an explicit ordering needs to be done first in a Derived table, and then in outer subquery, session variables are utilized. Otherwise the order is not guaranteed
– Madhur Bhaiya
Nov 22 '18 at 10:59
@Madhur I am Using MYSQL 5.6
– Naren P
Nov 22 '18 at 11:01
@NarenP Please see: How to accept an answer for closure. You get points for it as well. Thanks :)
– Madhur Bhaiya
Nov 26 '18 at 10:54