MYSQL Cannot count the occurrence of a particular value in a date rangeHow do you set a default value for a MySQL Datetime column?Get record counts for all tables in MySQL databaseWhich MySQL data type to use for storing boolean valuesFinding duplicate values in MySQLMySQL: Count the occurrences of DISTINCT valuesFind n occurrences within a date range that is within a larger date rangeLoop through records, updating status based on balanceSQL: Count of rows since certain value first occurred: keep countingHow to sum and count occurrences of certain values from groups of rows sharing same valueget data for a record in the past
Modeling an IP Address
Today is the Center
What's the point of deactivating Num Lock on login screens?
What's that red-plus icon near a text?
meaning of に in 本当に?
Is it possible to do 50 km distance without any previous training?
Why is Minecraft giving an OpenGL error?
Is it possible to run Internet Explorer on OS X El Capitan?
Accidentally leaked the solution to an assignment, what to do now? (I'm the prof)
Why do I get two different answers for this counting problem?
DC-DC converter from low voltage at high current, to high voltage at low current
Does detail obscure or enhance action?
How old can references or sources in a thesis be?
Can I ask the recruiters in my resume to put the reason why I am rejected?
What is the word for reserving something for yourself before others do?
Codimension of non-flat locus
Watching something be written to a file live with tail
Roll the carpet
"You are your self first supporter", a more proper way to say it
What's the output of a record needle playing an out-of-speed record
Can I make popcorn with any corn?
Java Casting: Java 11 throws LambdaConversionException while 1.8 does not
What is a clear way to write a bar that has an extra beat?
Arrow those variables!
MYSQL Cannot count the occurrence of a particular value in a date range
How do you set a default value for a MySQL Datetime column?Get record counts for all tables in MySQL databaseWhich MySQL data type to use for storing boolean valuesFinding duplicate values in MySQLMySQL: Count the occurrences of DISTINCT valuesFind n occurrences within a date range that is within a larger date rangeLoop through records, updating status based on balanceSQL: Count of rows since certain value first occurred: keep countingHow to sum and count occurrences of certain values from groups of rows sharing same valueget data for a record in the past
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
So, there is an account number and we have daily information about their payments. Suppose we have information of 1 year leading up to today which is 08/March/2019, I would want to calculate the number of times he/she overpaid in last 1 week. I have used mysql window function but for some reason it does not seem to work
@GMB A sample data would look like this:Suppose for this account we have info from last march 2018. I just want the number of times paid_status = overpaid from the last date that I have on my file which is of today - 08/March/2019 and previous 7 days, 14 days, 1 month or any duration of my choosing. Your query will hardcode it only for 7 days.
ACCOUNT_ID paid_status amt dte
-----------------------
1234 overpaid 100 01/March/2018
.
.
.
1234 overpaid 120 01/March/2019
1234 not paid 0 02/March/2019
1234 overpaid 110 03/March/2019
1234 overpaid 120 04/March/2019
1234 overpaid 130 05/March/2019
1234 overpaid 120 06/March/2019
1234 overpaid 120 07/March/2019
1234 overpaid 121 08/March/2019
Query:
,COUNT(CASE WHEN paid_status = 'OVERPAID' THEN 1 END)
over (PARTITION BY ACCOUNT_ID
ORDER BY DTE ROWS BETWEEN 7 PRECEDING AND UNBOUNDED FOLLOWING
) AS num_times_overpaid_week1
The output should be like this(not including today's info):
account_id num_times_overpaid_week1
1234 6
While I am getting multiple rows for the same account_id and it is not exactly calulating the field correctly
mysql sql
|
show 3 more comments
So, there is an account number and we have daily information about their payments. Suppose we have information of 1 year leading up to today which is 08/March/2019, I would want to calculate the number of times he/she overpaid in last 1 week. I have used mysql window function but for some reason it does not seem to work
@GMB A sample data would look like this:Suppose for this account we have info from last march 2018. I just want the number of times paid_status = overpaid from the last date that I have on my file which is of today - 08/March/2019 and previous 7 days, 14 days, 1 month or any duration of my choosing. Your query will hardcode it only for 7 days.
ACCOUNT_ID paid_status amt dte
-----------------------
1234 overpaid 100 01/March/2018
.
.
.
1234 overpaid 120 01/March/2019
1234 not paid 0 02/March/2019
1234 overpaid 110 03/March/2019
1234 overpaid 120 04/March/2019
1234 overpaid 130 05/March/2019
1234 overpaid 120 06/March/2019
1234 overpaid 120 07/March/2019
1234 overpaid 121 08/March/2019
Query:
,COUNT(CASE WHEN paid_status = 'OVERPAID' THEN 1 END)
over (PARTITION BY ACCOUNT_ID
ORDER BY DTE ROWS BETWEEN 7 PRECEDING AND UNBOUNDED FOLLOWING
) AS num_times_overpaid_week1
The output should be like this(not including today's info):
account_id num_times_overpaid_week1
1234 6
While I am getting multiple rows for the same account_id and it is not exactly calulating the field correctly
mysql sql
1
it does not seem to work: what issue do you meet exactly? Are you getting an error message, or wrong results? Also, it would be good if you could show us the entire query, sample data, current and expected results...
– GMB
Mar 8 at 2:09
Please note thatROWS BETWEEN 7 PRECEDING AND UNBOUNDED FOLLOWINGdefines a window that contains the last 7 records, the current record and all following records. Maybe you just want the last 7 records and the current one? Then just:ROWS 7 PRECEDING.
– GMB
Mar 8 at 2:18
@GMB I have added the sample data
– Rohan
Mar 8 at 2:24
Can you please show your entire query?
– GMB
Mar 8 at 2:26
So in the resultset you just want one record for each account, with the number of overpaid in the last 7 days?
– GMB
Mar 8 at 2:27
|
show 3 more comments
So, there is an account number and we have daily information about their payments. Suppose we have information of 1 year leading up to today which is 08/March/2019, I would want to calculate the number of times he/she overpaid in last 1 week. I have used mysql window function but for some reason it does not seem to work
@GMB A sample data would look like this:Suppose for this account we have info from last march 2018. I just want the number of times paid_status = overpaid from the last date that I have on my file which is of today - 08/March/2019 and previous 7 days, 14 days, 1 month or any duration of my choosing. Your query will hardcode it only for 7 days.
ACCOUNT_ID paid_status amt dte
-----------------------
1234 overpaid 100 01/March/2018
.
.
.
1234 overpaid 120 01/March/2019
1234 not paid 0 02/March/2019
1234 overpaid 110 03/March/2019
1234 overpaid 120 04/March/2019
1234 overpaid 130 05/March/2019
1234 overpaid 120 06/March/2019
1234 overpaid 120 07/March/2019
1234 overpaid 121 08/March/2019
Query:
,COUNT(CASE WHEN paid_status = 'OVERPAID' THEN 1 END)
over (PARTITION BY ACCOUNT_ID
ORDER BY DTE ROWS BETWEEN 7 PRECEDING AND UNBOUNDED FOLLOWING
) AS num_times_overpaid_week1
The output should be like this(not including today's info):
account_id num_times_overpaid_week1
1234 6
While I am getting multiple rows for the same account_id and it is not exactly calulating the field correctly
mysql sql
So, there is an account number and we have daily information about their payments. Suppose we have information of 1 year leading up to today which is 08/March/2019, I would want to calculate the number of times he/she overpaid in last 1 week. I have used mysql window function but for some reason it does not seem to work
@GMB A sample data would look like this:Suppose for this account we have info from last march 2018. I just want the number of times paid_status = overpaid from the last date that I have on my file which is of today - 08/March/2019 and previous 7 days, 14 days, 1 month or any duration of my choosing. Your query will hardcode it only for 7 days.
ACCOUNT_ID paid_status amt dte
-----------------------
1234 overpaid 100 01/March/2018
.
.
.
1234 overpaid 120 01/March/2019
1234 not paid 0 02/March/2019
1234 overpaid 110 03/March/2019
1234 overpaid 120 04/March/2019
1234 overpaid 130 05/March/2019
1234 overpaid 120 06/March/2019
1234 overpaid 120 07/March/2019
1234 overpaid 121 08/March/2019
Query:
,COUNT(CASE WHEN paid_status = 'OVERPAID' THEN 1 END)
over (PARTITION BY ACCOUNT_ID
ORDER BY DTE ROWS BETWEEN 7 PRECEDING AND UNBOUNDED FOLLOWING
) AS num_times_overpaid_week1
The output should be like this(not including today's info):
account_id num_times_overpaid_week1
1234 6
While I am getting multiple rows for the same account_id and it is not exactly calulating the field correctly
mysql sql
mysql sql
edited Mar 8 at 2:57
Rohan
asked Mar 8 at 2:01
RohanRohan
287
287
1
it does not seem to work: what issue do you meet exactly? Are you getting an error message, or wrong results? Also, it would be good if you could show us the entire query, sample data, current and expected results...
– GMB
Mar 8 at 2:09
Please note thatROWS BETWEEN 7 PRECEDING AND UNBOUNDED FOLLOWINGdefines a window that contains the last 7 records, the current record and all following records. Maybe you just want the last 7 records and the current one? Then just:ROWS 7 PRECEDING.
– GMB
Mar 8 at 2:18
@GMB I have added the sample data
– Rohan
Mar 8 at 2:24
Can you please show your entire query?
– GMB
Mar 8 at 2:26
So in the resultset you just want one record for each account, with the number of overpaid in the last 7 days?
– GMB
Mar 8 at 2:27
|
show 3 more comments
1
it does not seem to work: what issue do you meet exactly? Are you getting an error message, or wrong results? Also, it would be good if you could show us the entire query, sample data, current and expected results...
– GMB
Mar 8 at 2:09
Please note thatROWS BETWEEN 7 PRECEDING AND UNBOUNDED FOLLOWINGdefines a window that contains the last 7 records, the current record and all following records. Maybe you just want the last 7 records and the current one? Then just:ROWS 7 PRECEDING.
– GMB
Mar 8 at 2:18
@GMB I have added the sample data
– Rohan
Mar 8 at 2:24
Can you please show your entire query?
– GMB
Mar 8 at 2:26
So in the resultset you just want one record for each account, with the number of overpaid in the last 7 days?
– GMB
Mar 8 at 2:27
1
1
it does not seem to work: what issue do you meet exactly? Are you getting an error message, or wrong results? Also, it would be good if you could show us the entire query, sample data, current and expected results...
– GMB
Mar 8 at 2:09
it does not seem to work: what issue do you meet exactly? Are you getting an error message, or wrong results? Also, it would be good if you could show us the entire query, sample data, current and expected results...
– GMB
Mar 8 at 2:09
Please note that
ROWS BETWEEN 7 PRECEDING AND UNBOUNDED FOLLOWING defines a window that contains the last 7 records, the current record and all following records. Maybe you just want the last 7 records and the current one? Then just: ROWS 7 PRECEDING.– GMB
Mar 8 at 2:18
Please note that
ROWS BETWEEN 7 PRECEDING AND UNBOUNDED FOLLOWING defines a window that contains the last 7 records, the current record and all following records. Maybe you just want the last 7 records and the current one? Then just: ROWS 7 PRECEDING.– GMB
Mar 8 at 2:18
@GMB I have added the sample data
– Rohan
Mar 8 at 2:24
@GMB I have added the sample data
– Rohan
Mar 8 at 2:24
Can you please show your entire query?
– GMB
Mar 8 at 2:26
Can you please show your entire query?
– GMB
Mar 8 at 2:26
So in the resultset you just want one record for each account, with the number of overpaid in the last 7 days?
– GMB
Mar 8 at 2:27
So in the resultset you just want one record for each account, with the number of overpaid in the last 7 days?
– GMB
Mar 8 at 2:27
|
show 3 more comments
1 Answer
1
active
oldest
votes
From your sample data it seems like you are looking for a simple aggregated query (no need for window functions):
SELECT account_id, SUM(paid_status = 'OVERPAID') AS num_times_overpaid_week1
FROM mytable
WHERE dte >= CURRENT_DATE - INTERVAL 7 DAY
GROUP BY account_id
Expression SUM(paid_status = 'OVERPAID') uses a nice MySQL feature where conditions return 1 when satisfied and 0 when not.
NB: if, for some reason, you do want to use window functions (maybe to perform other computation), then you would need to use ROW_NUMBER() to rank records by date, and the filter out only the most recent record per account in an outer query. I think that the definition of the window can be largely simplified:
SELECT *
FROM (
SELECT
account_id,
SUM(paid_status = 'OVERPAID') OVER(PARTITION BY account_id) AS num_times_overpaid_week1,
-- possibly other columns
ROW_NUMBER() OVER(PARTITION BY account_id ORDER BY dte DESC) rn
FROM mytable
WHERE dte >= CURRENT_DATE - INTERVAL 7 DAY
) x WHERE rn = 1
Imagine there are 10s and 20s of such statuses and hence the need for window function? Can it not be done using window function
– Rohan
Mar 8 at 2:32
@Rohan: given your sample data, conditional aggregation, as shown in my answer, is the simplest way to go. Are you able to actually test the query against your data to see if it produces the expected output?
– GMB
Mar 8 at 2:36
while your approach works perfect, the reason I want to use windows function is I can calculate lifetime sum, max, avg of multiple columns(in this case I added amt as a field). Using windows functions helps Just for this overpaid thing I can write two sql queries and then join but I wanted to do it in one go. Appreciate your help, for example if I wanted to know the lifetime that this account holder overpaid - ,COUNT(CASE WHEN DPD_BUCKET = '0.OVERPAID' THEN 1 END) over (PARTITION BY LOAN_ACCOUNT_ID) AS num_times_overpaid if I have it in one go, including last week(recency) it will hel
– Rohan
Mar 8 at 2:48
Added some more data and code above on what I am trying to achieve
– Rohan
Mar 8 at 2:50
1
Why not simply ask for greater clarity at the outset?
– Strawberry
Mar 8 at 4:07
|
show 7 more comments
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%2f55055699%2fmysql-cannot-count-the-occurrence-of-a-particular-value-in-a-date-range%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
From your sample data it seems like you are looking for a simple aggregated query (no need for window functions):
SELECT account_id, SUM(paid_status = 'OVERPAID') AS num_times_overpaid_week1
FROM mytable
WHERE dte >= CURRENT_DATE - INTERVAL 7 DAY
GROUP BY account_id
Expression SUM(paid_status = 'OVERPAID') uses a nice MySQL feature where conditions return 1 when satisfied and 0 when not.
NB: if, for some reason, you do want to use window functions (maybe to perform other computation), then you would need to use ROW_NUMBER() to rank records by date, and the filter out only the most recent record per account in an outer query. I think that the definition of the window can be largely simplified:
SELECT *
FROM (
SELECT
account_id,
SUM(paid_status = 'OVERPAID') OVER(PARTITION BY account_id) AS num_times_overpaid_week1,
-- possibly other columns
ROW_NUMBER() OVER(PARTITION BY account_id ORDER BY dte DESC) rn
FROM mytable
WHERE dte >= CURRENT_DATE - INTERVAL 7 DAY
) x WHERE rn = 1
Imagine there are 10s and 20s of such statuses and hence the need for window function? Can it not be done using window function
– Rohan
Mar 8 at 2:32
@Rohan: given your sample data, conditional aggregation, as shown in my answer, is the simplest way to go. Are you able to actually test the query against your data to see if it produces the expected output?
– GMB
Mar 8 at 2:36
while your approach works perfect, the reason I want to use windows function is I can calculate lifetime sum, max, avg of multiple columns(in this case I added amt as a field). Using windows functions helps Just for this overpaid thing I can write two sql queries and then join but I wanted to do it in one go. Appreciate your help, for example if I wanted to know the lifetime that this account holder overpaid - ,COUNT(CASE WHEN DPD_BUCKET = '0.OVERPAID' THEN 1 END) over (PARTITION BY LOAN_ACCOUNT_ID) AS num_times_overpaid if I have it in one go, including last week(recency) it will hel
– Rohan
Mar 8 at 2:48
Added some more data and code above on what I am trying to achieve
– Rohan
Mar 8 at 2:50
1
Why not simply ask for greater clarity at the outset?
– Strawberry
Mar 8 at 4:07
|
show 7 more comments
From your sample data it seems like you are looking for a simple aggregated query (no need for window functions):
SELECT account_id, SUM(paid_status = 'OVERPAID') AS num_times_overpaid_week1
FROM mytable
WHERE dte >= CURRENT_DATE - INTERVAL 7 DAY
GROUP BY account_id
Expression SUM(paid_status = 'OVERPAID') uses a nice MySQL feature where conditions return 1 when satisfied and 0 when not.
NB: if, for some reason, you do want to use window functions (maybe to perform other computation), then you would need to use ROW_NUMBER() to rank records by date, and the filter out only the most recent record per account in an outer query. I think that the definition of the window can be largely simplified:
SELECT *
FROM (
SELECT
account_id,
SUM(paid_status = 'OVERPAID') OVER(PARTITION BY account_id) AS num_times_overpaid_week1,
-- possibly other columns
ROW_NUMBER() OVER(PARTITION BY account_id ORDER BY dte DESC) rn
FROM mytable
WHERE dte >= CURRENT_DATE - INTERVAL 7 DAY
) x WHERE rn = 1
Imagine there are 10s and 20s of such statuses and hence the need for window function? Can it not be done using window function
– Rohan
Mar 8 at 2:32
@Rohan: given your sample data, conditional aggregation, as shown in my answer, is the simplest way to go. Are you able to actually test the query against your data to see if it produces the expected output?
– GMB
Mar 8 at 2:36
while your approach works perfect, the reason I want to use windows function is I can calculate lifetime sum, max, avg of multiple columns(in this case I added amt as a field). Using windows functions helps Just for this overpaid thing I can write two sql queries and then join but I wanted to do it in one go. Appreciate your help, for example if I wanted to know the lifetime that this account holder overpaid - ,COUNT(CASE WHEN DPD_BUCKET = '0.OVERPAID' THEN 1 END) over (PARTITION BY LOAN_ACCOUNT_ID) AS num_times_overpaid if I have it in one go, including last week(recency) it will hel
– Rohan
Mar 8 at 2:48
Added some more data and code above on what I am trying to achieve
– Rohan
Mar 8 at 2:50
1
Why not simply ask for greater clarity at the outset?
– Strawberry
Mar 8 at 4:07
|
show 7 more comments
From your sample data it seems like you are looking for a simple aggregated query (no need for window functions):
SELECT account_id, SUM(paid_status = 'OVERPAID') AS num_times_overpaid_week1
FROM mytable
WHERE dte >= CURRENT_DATE - INTERVAL 7 DAY
GROUP BY account_id
Expression SUM(paid_status = 'OVERPAID') uses a nice MySQL feature where conditions return 1 when satisfied and 0 when not.
NB: if, for some reason, you do want to use window functions (maybe to perform other computation), then you would need to use ROW_NUMBER() to rank records by date, and the filter out only the most recent record per account in an outer query. I think that the definition of the window can be largely simplified:
SELECT *
FROM (
SELECT
account_id,
SUM(paid_status = 'OVERPAID') OVER(PARTITION BY account_id) AS num_times_overpaid_week1,
-- possibly other columns
ROW_NUMBER() OVER(PARTITION BY account_id ORDER BY dte DESC) rn
FROM mytable
WHERE dte >= CURRENT_DATE - INTERVAL 7 DAY
) x WHERE rn = 1
From your sample data it seems like you are looking for a simple aggregated query (no need for window functions):
SELECT account_id, SUM(paid_status = 'OVERPAID') AS num_times_overpaid_week1
FROM mytable
WHERE dte >= CURRENT_DATE - INTERVAL 7 DAY
GROUP BY account_id
Expression SUM(paid_status = 'OVERPAID') uses a nice MySQL feature where conditions return 1 when satisfied and 0 when not.
NB: if, for some reason, you do want to use window functions (maybe to perform other computation), then you would need to use ROW_NUMBER() to rank records by date, and the filter out only the most recent record per account in an outer query. I think that the definition of the window can be largely simplified:
SELECT *
FROM (
SELECT
account_id,
SUM(paid_status = 'OVERPAID') OVER(PARTITION BY account_id) AS num_times_overpaid_week1,
-- possibly other columns
ROW_NUMBER() OVER(PARTITION BY account_id ORDER BY dte DESC) rn
FROM mytable
WHERE dte >= CURRENT_DATE - INTERVAL 7 DAY
) x WHERE rn = 1
edited Mar 8 at 2:56
answered Mar 8 at 2:30
GMBGMB
21.4k51028
21.4k51028
Imagine there are 10s and 20s of such statuses and hence the need for window function? Can it not be done using window function
– Rohan
Mar 8 at 2:32
@Rohan: given your sample data, conditional aggregation, as shown in my answer, is the simplest way to go. Are you able to actually test the query against your data to see if it produces the expected output?
– GMB
Mar 8 at 2:36
while your approach works perfect, the reason I want to use windows function is I can calculate lifetime sum, max, avg of multiple columns(in this case I added amt as a field). Using windows functions helps Just for this overpaid thing I can write two sql queries and then join but I wanted to do it in one go. Appreciate your help, for example if I wanted to know the lifetime that this account holder overpaid - ,COUNT(CASE WHEN DPD_BUCKET = '0.OVERPAID' THEN 1 END) over (PARTITION BY LOAN_ACCOUNT_ID) AS num_times_overpaid if I have it in one go, including last week(recency) it will hel
– Rohan
Mar 8 at 2:48
Added some more data and code above on what I am trying to achieve
– Rohan
Mar 8 at 2:50
1
Why not simply ask for greater clarity at the outset?
– Strawberry
Mar 8 at 4:07
|
show 7 more comments
Imagine there are 10s and 20s of such statuses and hence the need for window function? Can it not be done using window function
– Rohan
Mar 8 at 2:32
@Rohan: given your sample data, conditional aggregation, as shown in my answer, is the simplest way to go. Are you able to actually test the query against your data to see if it produces the expected output?
– GMB
Mar 8 at 2:36
while your approach works perfect, the reason I want to use windows function is I can calculate lifetime sum, max, avg of multiple columns(in this case I added amt as a field). Using windows functions helps Just for this overpaid thing I can write two sql queries and then join but I wanted to do it in one go. Appreciate your help, for example if I wanted to know the lifetime that this account holder overpaid - ,COUNT(CASE WHEN DPD_BUCKET = '0.OVERPAID' THEN 1 END) over (PARTITION BY LOAN_ACCOUNT_ID) AS num_times_overpaid if I have it in one go, including last week(recency) it will hel
– Rohan
Mar 8 at 2:48
Added some more data and code above on what I am trying to achieve
– Rohan
Mar 8 at 2:50
1
Why not simply ask for greater clarity at the outset?
– Strawberry
Mar 8 at 4:07
Imagine there are 10s and 20s of such statuses and hence the need for window function? Can it not be done using window function
– Rohan
Mar 8 at 2:32
Imagine there are 10s and 20s of such statuses and hence the need for window function? Can it not be done using window function
– Rohan
Mar 8 at 2:32
@Rohan: given your sample data, conditional aggregation, as shown in my answer, is the simplest way to go. Are you able to actually test the query against your data to see if it produces the expected output?
– GMB
Mar 8 at 2:36
@Rohan: given your sample data, conditional aggregation, as shown in my answer, is the simplest way to go. Are you able to actually test the query against your data to see if it produces the expected output?
– GMB
Mar 8 at 2:36
while your approach works perfect, the reason I want to use windows function is I can calculate lifetime sum, max, avg of multiple columns(in this case I added amt as a field). Using windows functions helps Just for this overpaid thing I can write two sql queries and then join but I wanted to do it in one go. Appreciate your help, for example if I wanted to know the lifetime that this account holder overpaid - ,COUNT(CASE WHEN DPD_BUCKET = '0.OVERPAID' THEN 1 END) over (PARTITION BY LOAN_ACCOUNT_ID) AS num_times_overpaid if I have it in one go, including last week(recency) it will hel
– Rohan
Mar 8 at 2:48
while your approach works perfect, the reason I want to use windows function is I can calculate lifetime sum, max, avg of multiple columns(in this case I added amt as a field). Using windows functions helps Just for this overpaid thing I can write two sql queries and then join but I wanted to do it in one go. Appreciate your help, for example if I wanted to know the lifetime that this account holder overpaid - ,COUNT(CASE WHEN DPD_BUCKET = '0.OVERPAID' THEN 1 END) over (PARTITION BY LOAN_ACCOUNT_ID) AS num_times_overpaid if I have it in one go, including last week(recency) it will hel
– Rohan
Mar 8 at 2:48
Added some more data and code above on what I am trying to achieve
– Rohan
Mar 8 at 2:50
Added some more data and code above on what I am trying to achieve
– Rohan
Mar 8 at 2:50
1
1
Why not simply ask for greater clarity at the outset?
– Strawberry
Mar 8 at 4:07
Why not simply ask for greater clarity at the outset?
– Strawberry
Mar 8 at 4:07
|
show 7 more comments
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%2f55055699%2fmysql-cannot-count-the-occurrence-of-a-particular-value-in-a-date-range%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
1
it does not seem to work: what issue do you meet exactly? Are you getting an error message, or wrong results? Also, it would be good if you could show us the entire query, sample data, current and expected results...
– GMB
Mar 8 at 2:09
Please note that
ROWS BETWEEN 7 PRECEDING AND UNBOUNDED FOLLOWINGdefines a window that contains the last 7 records, the current record and all following records. Maybe you just want the last 7 records and the current one? Then just:ROWS 7 PRECEDING.– GMB
Mar 8 at 2:18
@GMB I have added the sample data
– Rohan
Mar 8 at 2:24
Can you please show your entire query?
– GMB
Mar 8 at 2:26
So in the resultset you just want one record for each account, with the number of overpaid in the last 7 days?
– GMB
Mar 8 at 2:27