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

What do the dots in this tr command do: tr .............A-Z A-ZA-Z <<< "JVPQBOV" (with 13 dots)

Why doesn't Newton's third law mean a person bounces back to where they started when they hit the ground?

Is it unprofessional to ask if a job posting on GlassDoor is real?

Has there ever been an airliner design involving reducing generator load by installing solar panels?

Why is 150k or 200k jobs considered good when there's 300k+ births a month?

How to determine what difficulty is right for the game?

What are these boxed doors outside store fronts in New York?

High voltage LED indicator 40-1000 VDC without additional power supply

Are astronomers waiting to see something in an image from a gravitational lens that they've already seen in an adjacent image?

Alternative to sending password over mail?

NMaximize is not converging to a solution

Add text to same line using sed

What defenses are there against being summoned by the Gate spell?

Does detail obscure or enhance action?

Why do I get two different answers for this counting problem?

What's the point of deactivating Num Lock on login screens?

Approximately how much travel time was saved by the opening of the Suez Canal in 1869?

Accidentally leaked the solution to an assignment, what to do now? (I'm the prof)

How do I gain back my faith in my PhD degree?

How does quantile regression compare to logistic regression with the variable split at the quantile?

Character reincarnated...as a snail

Meaning of に in 本当に

Today is the Center

"You are your self first supporter", a more proper way to say it



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;








1















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










share|improve this question



















  • 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 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











  • 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















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










share|improve this question



















  • 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 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











  • 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








1








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










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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











  • 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





    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












  • @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












1 Answer
1






active

oldest

votes


















0














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





share|improve this answer

























  • 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











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
);



);













draft saved

draft discarded


















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









0














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





share|improve this answer

























  • 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















0














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





share|improve this answer

























  • 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













0












0








0







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





share|improve this answer















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






share|improve this answer














share|improve this answer



share|improve this answer








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

















  • 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



















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

1928 у кіно

Захаров Федір Захарович

Ель Греко