Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Getting last max 2 dates in the table

Posted on 2010-11-13
11
Medium Priority
?
379 Views
Last Modified: 2012-06-21

I am writting a function in mysql.

I need to have sum of transactions from last 2 dates entered in the system

sum(EmailTransCount)+ sum(posttranscount)


My below function is considering last max date but I wanted to consider last max 2 dates in my below query

Please help me fixing my query.
BEGIN

DECLARE totalCount INT;

Declare tranDate  Date;

select max(Transactiondate) into tranDate from dailycount where countoftheday <>0;


select (sum(EmailTransCount)+ sum(posttranscount))  INTO totalCount

from dailycount

where countoftheday <>0

and transactiondate = tranDate;

RETURN totalCount;

END

Open in new window

0
Comment
Question by:tia_kamakshi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 8

Accepted Solution

by:
raulggonzalez earned 1000 total points
ID: 34127216
Hi

I think you can do something like this, I couldn't test it, but I'm sure you can get the logic.

Instead of a variable with the max(date) put 2 into a temp table and join it with yours.

Hope it helps.

Cheers
DECLARE totalCount INT;

Declare tranDate  Date;

CREATE TEMPORARY TABLE dates(tranDate Date);

INSERT INTO dates
(SELECT Transactiondate 
   from dailycount 
  where countoftheday <>0 
ORDER BY Transactiondate DESC 
LIMIT 2);


select (sum(EmailTransCount)+ sum(posttranscount))  INTO totalCount

from dailycount
INNER JOIN dates
ON dailycount.transactionDate = dates.tranDate;


RETURN totalCount;

END

Open in new window

0
 
LVL 8

Expert Comment

by:raulggonzalez
ID: 34127221
Sorry, remove


Declare tranDate  Date;


because is not necessary for my solution

good luck
0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 34127314
Yep, kinda have to agree...

but would change it slightly...

select (sum(EmailTransCount)+ sum(posttranscount))  INTO totalCount
from dailycount
where countoftheday <>0
and transactiondate IN (select Transactiondate from dailycount where countoftheday <>0 order by transactiondate desc limit 2)

Open in new window

0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 8

Expert Comment

by:raulggonzalez
ID: 34127399
Hi mark_wills,

I'm not sure about repeating

where countoftheday <>0

If we retrieve the dates in my temp table or your subquery, would it be necessary doing it again in the main query?
 I've seen the original code has it, but I've got my doubts.

Cheers
0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 34127472
Yep reckon it is... But like you I did have my doubts.

We dont know what may be included or excluded as a result of countoftheday, or, the dependancy on that condition.

Certainly if the goal is simply the two most recent dates (regardless) then it is not needed in the subquery (as it currently is).

But, if we take our clue from the original max(Transactiondate) then it was part of that condition, so, simply carried it through...
0
 
LVL 32

Expert Comment

by:awking00
ID: 34128000
See attached for alternate method.
procedure.txt
0
 
LVL 44

Expert Comment

by:GRayL
ID: 34128766
How about:

SELECT Sum(EmailTransCount) + Sum(PostTransCount) FROM (
SELECT * FROM DailyCount ORDER BY TransactionDate DESC LIMIT 2)
0
 
LVL 51

Assisted Solution

by:Mark Wills
Mark Wills earned 1000 total points
ID: 34129209
Hmmmm...

I was assuming there could be multiple instances of transactiondate within the DailyCount datasource. So, getting the top 2 rows doesnt necessarily return the top 2 distinct transactiondates. Then I realised there was an error in my previous query where we have to group by (or distinct) in order to ensure we get the top 2 distinct transactiondates.

ie:

select (sum(EmailTransCount)+ sum(posttranscount))  INTO totalCount
from dailycount
where countoftheday <>0
and transactiondate IN (select Transactiondate from dailycount where countoftheday <>0 group by transactionDate order by transactiondate desc limit 2) 

Open in new window

0
 
LVL 44

Expert Comment

by:GRayL
ID: 34129413
Do we need the top two 'different' dates?  What do we do when there are five (to choose a number) records having the same date which are max() dates.
0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 34129766
@grayl, I think that is where the SUM comes into it...

If possible, would be great to see some sample data and expected results
0
 

Author Closing Comment

by:tia_kamakshi
ID: 34193679
Many Thanks for your replies.

I am sorry for late response. I was out of station last week.
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question