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

x
?
Solved

Mysql query to get the first record for a set

Posted on 2010-11-24
10
Medium Priority
?
503 Views
Last Modified: 2012-06-21
Mysql query to get the first record for a set.

For each Acct# I want to get the old (first) deposit

Here is my data
Acct#  Date           Deposit
Smith  01-Jan-10   100.00
Smith  01-Feb-10   200.00
Jones  01-Mar-10   50.00
Brown 01-Jan-10   10.00
Brown 01-Apr-10   20.00


I want the result
Acct#  Date           Deposit
Smith  01-Jan-10   100.00
Jones  01-Mar-10   50.00
Brown 01-Jan-10   10.00
0
Comment
Question by:pmsguy
[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
  • 3
  • 2
  • 2
  • +2
10 Comments
 

Accepted Solution

by:
dozenmatta earned 448 total points
ID: 34209752
Try something like:

Select AcctNum, Max(TrxDate), Deposit
from Accounts
group by TrxDate

0
 

Assisted Solution

by:dozenmatta
dozenmatta earned 448 total points
ID: 34209758
Sorry, i meant

Try something like:

Select AcctNum, Max(TrxDate), Deposit
from Accounts
group by AcctNum
0
 
LVL 15

Assisted Solution

by:dirknibleck
dirknibleck earned 668 total points
ID: 34209929
Use MIN(TrxDate) to get the oldest (first) record, otherwise dozenmatta has it correct.
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

Author Comment

by:pmsguy
ID: 34209955
Let me rephrase my question.

For each Acct# I want to get the old (first) deposit and sum all the deposits in 1 sql statement.

I assume a sub query is needed to get the old (first) deposit.
0
 
LVL 15

Assisted Solution

by:dirknibleck
dirknibleck earned 668 total points
ID: 34209980
Ok. If you just want the amount of the first deposit (or deposits on the first date)...:

SELECT AcctNum, first_deposit, SUM(Deposit)

FROM Accounts, (SELECT AcctNum, MIN(TrxDate) as first_deposit FROM Accounts GROUP BY AcctNum) as agg

WHERE Accounts.AcctNum = agg.AcctNum AND Accounts.TrxDate = agg.first_deposit
0
 
LVL 15

Assisted Solution

by:dirknibleck
dirknibleck earned 668 total points
ID: 34209983
* Add *

GROUP BY AcctNum, first_deposit
0
 
LVL 15

Assisted Solution

by:danrosenthal
danrosenthal earned 440 total points
ID: 34210013
I think this would work:

SELECT d.AcctNo, d.Date, d.Deposit
FROM Data d
WHERE NOT EXISTS (
      SELECT 1 FROM Data d2 WHERE d2.AcctNo = d.AcctNo AND d2.Date < d.Date
)

0
 
LVL 15

Assisted Solution

by:danrosenthal
danrosenthal earned 440 total points
ID: 34210021
To get the old (first deposit) date and the sum of all deposits you would simply do this:

SELECT AcctNo, MIN(Date) AS FirstDeposit, SUM(Deposit) AS TotalDeposits
FROM Data
GROUP BY AcctNo
0
 
LVL 58

Assisted Solution

by:cyberkiwi
cyberkiwi earned 444 total points
ID: 34210071
In case you have 2 deposits on the same first day, this will still work

select `Acct#`, Min(`Date`) `Date`, Min(FirstDeposit) FirstDeposit, Max(SumDeposit) SumDeposit
from
(
select `Acct#`, `Date`, Deposit,
  @d:=case when @a=`Acct#` then null else Deposit end as FirstDeposit,
  @s:=case when @a=`Acct#` then @s+ifnull(Deposit,0) else Deposit end as SumDeposit,
  @a:=`Acct#` as Discard
from (select @a:=null,@d:=null,@s:=null) r, mydata
order by `Acct#`, `Date` ASC
) n
group by `Acct#`
order by `Acct#`
0
 
LVL 58

Assisted Solution

by:cyberkiwi
cyberkiwi earned 444 total points
ID: 34210075
In my previous query, the columns are to be interpreted:

`Acct#` - account
Min(`Date`) `Date`  - date of first deposit
Min(FirstDeposit) FirstDeposit   - amount of first deposit
Max(SumDeposit) SumDeposit   - total of all deposits
0

Featured Post

Docker-Compose to Simplify Multi-Container Builds

Our veteran DevOps Author takes you through how to build a multi-container environment, managed with a single utility in order to simplify your deployments.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
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…

688 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