Welcome to the { mindfrost82.com } forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

Go Back   { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Microsoft > MS Office > Access

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-29-2008, 06:49 PM
Johnny
 
Posts: n/a
How can point me out the best way on a query

Hi everyone

Hi have a DB were I control the transactions made by a group f people.
The DB has two tables that are related in 1 to many.A table
with people information(name, adress, and so on) and a table with
money transactions.
The transactions table has 6 fields:
- regID related with the ID of "Clients" table (not automatic
counter on transactions table)
-movReg to identify the transaction number(automatic)
-Credits
-Debits
-movDate as date of transaction
-movDescr as description of transaction (food, money deposit,
gas....)

When people join the party they use to add money to personal account.
There is also a common money found, to be used when needed.

Now the problem:
At a certain date for example, someone whose current balance was
$100,
had the need of $200, so he got $100 of is own and $100 of common
found and became $100 negative, that person made two more
transactions
of $100 each and the balance is now $300 negative.
I have a query that will give me all people than has negative balance,
with all the transactions made
since the beginning of the account.

What I need:
In order to advise advise all people that has negative balance,
besides the negative amount, I need the transaction who made the
balance became negative and all other after this one. I Don't need a
list with all transactions.
The idea is just to put in a list of people who are negative, and all
the transactions after the one that let the balance became negative
(included).
In spoken words I'll say:"Mr John you had $100 on your account and
you
pick $200 on date xx/xx/xx, $100 on date xx/xx/xx and more $100 on
date xx/xx/xx, so you are $300 negative"

The query that gives all negative balances and all transactions made:
SELECT MovimentoEuros.MovNum, MovimentoEuros.MovNReg,
MovimentoEurosTesteQuery.strName,
MovimentoEurosTesteQuery.SumOfValorMov
FROM MovimentoEurosTesteQuery INNER JOIN MovimentoEuros ON
MovimentoEurosTesteQuery.MovNReg = MovimentoEuros.MovNReg
WHERE (((MovimentoEurosTesteQuery.SumOfValorMov)<0))
GROUP BY MovimentoEuros.MovNum, MovimentoEuros.MovNReg,
MovimentoEurosTesteQuery.strName,
MovimentoEurosTesteQuery.SumOfValorMov;


On table Debits are saved as negative values and credits as positive.

Hope someone could help
Tks
Reply With Quote
  #2 (permalink)  
Old 08-29-2008, 10:20 PM
=?Utf-8?B?S2VuIFNoZXJpZGFu?=
 
Posts: n/a
RE: How can point me out the best way on a query

The following should return the latest sequence of negative balances per
regID, which I think is what you are asking for:

SELECT regID, movDate, movDescr, Credits, Debits,
(SELECT SUM(Credits)
FROM Transactions AS T2
WHERE T2.regID = T1.regID
AND T2.movDate <= T1.movDate
AND ( T2.movReg<= T1.TransactionID
OR T2.movDate <> T1.movDate)) +
(SELECT SUM(Debits)
FROM Transactions AS T2
WHERE T2.regID = T1.regID
AND T2.movDate <= T1.movDate
AND ( T2.movReg<= T1.TransactionID
OR T2.movDate <> T1.movDate)) AS Balance
FROM Transactions AS T1
WHERE
(SELECT SUM(Credits)
FROM Transactions AS T2
WHERE T2.regID = T1.regID
AND T2.movDate <= T1.movDate
AND ( T2.movReg<= T1.TransactionID
OR T2.movDate <> T1.movDate)) +
(SELECT SUM(Debits)
FROM Transactions AS T2
WHERE T2.regID = T1.regID
AND T2.movDate <= T1.movDate
AND ( T2.movReg<= T1.TransactionID
OR T2.movDate <> T1.movDate)) < 0
AND movDate >
(SELECT MAX(movDate)
FROM Transactions AS T3
WHERE
T3.regID = T1.regID
AND
(SELECT SUM(Credits)
FROM Transactions AS T2
WHERE T2.regID = T3.regID
AND T2.movDate <= T3.movDate
AND ( T2.movReg<= T3.TransactionID
OR T2.movDate <> T3.movDate)) +
(SELECT SUM(Debits)
FROM Transactions AS T2
WHERE T2.regID = T3.regID
AND T2.movDate <= T3.movDate
AND ( T2.movReg<= T3.TransactionID
OR T2.movDate <> T3.movDate)) >= 0)
ORDER BY regID, movDate DESC, movReg DESC;

Note that if the movDate values are entered as dates without a time of day
and a client executes two transactions on the same day the order of those
transaction in the query's result set will be determined by the movReg
values. As this is an autonumber it cannot be guaranteed that the rows will
be in the correct order of the actual transactions, an autonumber only
guaranteeing uniqueness not necessarily sequence. If on the other hand the
movDate values are unique by virtue of including the time of day, then there
should be no problem.

As you'll probably have guessed the above query is adapted from one of my
own using a similar table, the only material difference apart from the column
names being that both the credit and debit values are positive numbers.
Usually when positive and negative values are used a single TransactionAmount
column would be used rather than having separate credit and debit columns.
Testing it with my table does return the correct rows, so it should work as
amended with your table. You can of course join the clients table to the
Transactions table in the outer query to include the names etc of clients.

Ken Sheridan
Stafford, England

"Johnny" wrote:

> Hi everyone
>
> Hi have a DB were I control the transactions made by a group f people.
> The DB has two tables that are related in 1 to many.A table
> with people information(name, adress, and so on) and a table with
> money transactions.
> The transactions table has 6 fields:
> - regID related with the ID of "Clients" table (not automatic
> counter on transactions table)
> -movReg to identify the transaction number(automatic)
> -Credits
> -Debits
> -movDate as date of transaction
> -movDescr as description of transaction (food, money deposit,
> gas....)
>
> When people join the party they use to add money to personal account.
> There is also a common money found, to be used when needed.
>
> Now the problem:
> At a certain date for example, someone whose current balance was
> $100,
> had the need of $200, so he got $100 of is own and $100 of common
> found and became $100 negative, that person made two more
> transactions
> of $100 each and the balance is now $300 negative.
> I have a query that will give me all people than has negative balance,
> with all the transactions made
> since the beginning of the account.
>
> What I need:
> In order to advise advise all people that has negative balance,
> besides the negative amount, I need the transaction who made the
> balance became negative and all other after this one. I Don't need a
> list with all transactions.
> The idea is just to put in a list of people who are negative, and all
> the transactions after the one that let the balance became negative
> (included).
> In spoken words I'll say:"Mr John you had $100 on your account and
> you
> pick $200 on date xx/xx/xx, $100 on date xx/xx/xx and more $100 on
> date xx/xx/xx, so you are $300 negative"
>
> The query that gives all negative balances and all transactions made:
> SELECT MovimentoEuros.MovNum, MovimentoEuros.MovNReg,
> MovimentoEurosTesteQuery.strName,
> MovimentoEurosTesteQuery.SumOfValorMov
> FROM MovimentoEurosTesteQuery INNER JOIN MovimentoEuros ON
> MovimentoEurosTesteQuery.MovNReg = MovimentoEuros.MovNReg
> WHERE (((MovimentoEurosTesteQuery.SumOfValorMov)<0))
> GROUP BY MovimentoEuros.MovNum, MovimentoEuros.MovNReg,
> MovimentoEurosTesteQuery.strName,
> MovimentoEurosTesteQuery.SumOfValorMov;
>
>
> On table Debits are saved as negative values and credits as positive.
>
> Hope someone could help
> Tks
>


Reply With Quote
  #3 (permalink)  
Old 08-29-2008, 11:23 PM
Johnny
 
Posts: n/a
Re: How can point me out the best way on a query

Hello Ken

Thank you for your answer.
I need to confess my mistake. I initally said that the table has 6
fields, but it was on first version, I changed to 5 fields and credits
and debits are saved on same field, credit as positive value and debit
as negative, I think that will make a little difference on query
structure.
SO the transactions table fields are:
- regID related with the ID of "Clients" table (not automatic counter
on transactions table)
- movNum to identify the transaction number(automatic)
- movValue (Debits as negative and Credits as positive)
- movDate as date of transaction
- movDescr as description of transaction (food, money deposit,
gas....)

Regards
Johnny

Reply With Quote
  #4 (permalink)  
Old 08-30-2008, 01:24 AM
=?Utf-8?B?S2VuIFNoZXJpZGFu?=
 
Posts: n/a
Re: How can point me out the best way on a query

All you should need to do is, instead of adding the two results of the pairs
of subqueries use a single subquery in each case. You'll now sum the
movValue column rather than the debits or credits of course.

Ken Sheridan
Stafford, England

"Johnny" wrote:

> Hello Ken
>
> Thank you for your answer.
> I need to confess my mistake. I initally said that the table has 6
> fields, but it was on first version, I changed to 5 fields and credits
> and debits are saved on same field, credit as positive value and debit
> as negative, I think that will make a little difference on query
> structure.
> SO the transactions table fields are:
> - regID related with the ID of "Clients" table (not automatic counter
> on transactions table)
> - movNum to identify the transaction number(automatic)
> - movValue (Debits as negative and Credits as positive)
> - movDate as date of transaction
> - movDescr as description of transaction (food, money deposit,
> gas....)
>
> Regards
> Johnny
>
>


Reply With Quote
  #5 (permalink)  
Old 08-30-2008, 10:19 AM
Johnny
 
Posts: n/a
Re: How can point me out the best way on a query

Hi Ken

I tried the query you sent, but for some reason no records are found
and there are at least 3 people with negative balance, also the empty
query takes long time to show!

regards
Johnny

Reply With Quote
  #6 (permalink)  
Old 08-30-2008, 09:14 PM
=?Utf-8?B?S2VuIFNoZXJpZGFu?=
 
Posts: n/a
Re: How can point me out the best way on a query

I think see the problem; I hadn't taken account of the account going back
into credit after one or more consecutive debit balances. To make matters
simpler its probably best to do it in two stages, first creating a simple
balances query, qryBals:

SELECT movNum, regID, movDate, movValue, movDescr,
(SELECT SUM(movValue)
FROM Transactions AS T2
WHERE T2.regID = T1.regID
AND T2.movDate <= T1.movDate
AND ( T2.movNum <= T1.movNum
OR T2.movDate <> T1.movDate)) AS Balance
FROM Transactions AS T1;

If there can be only one transaction per customer per day you can simplify
it to:

SELECT movNum, regID, movDate, movValue, movDescr,
(SELECT SUM(movValue)
FROM Transactions AS T2
WHERE T2.regID = T1.regID
AND T2.movDate <= T1.movDate) AS Balance
FROM Transactions AS T1;

and then querying this:

SELECT *
FROM qryBals AS Q1
WHERE Balance < 0
AND (movDate =
(SELECT MAX(movDate)
FROM qryBals AS Q2
WHERE Q2.regID = Q1.regID
AND Q2.Balance < 0)
OR Transactiondate >
(SELECT MAX(movDate)
FROM qryBals AS Q3
WHERE Q3.regID = Q1.regID
AND Q3.Balance >= 0
AND Q3.movDate <
(SELECT MAX(movDate)
FROM qryBals AS Q4
WHERE Q4.regID = Q1.regID
AND Q4.Balance < 0)))
ORDER BY regID, movDate DESC, movNum DESC;

This seems to work OK with my dummy data. Its fast, but is using a table
with a restricted number of rows of course. If you are using it as the
RecordSource of a report remove the ORDER BY clause and use the report's own
internal sorting and grouping mechanism. Indexing makes a big difference to
performance of course.

Just to be clear on what it does; it returns the latest subset of
consecutive debit balance rows per customer, which I think is what you want.

Ken Sheridan
Stafford, England

"Johnny" wrote:

> Hi Ken
>
> I tried the query you sent, but for some reason no records are found
> and there are at least 3 people with negative balance, also the empty
> query takes long time to show!
>
> regards
> Johnny
>
>


Reply With Quote
  #7 (permalink)  
Old 08-30-2008, 10:32 PM
Johnny
 
Posts: n/a
Re: How can point me out the best way on a query

Hello Ken

Everything worked like you said I get the latest debits, but the
query is very, very, very slow, and computer freezes most of the
times.
Transactions table has around 1000 records, If you know a way to get
arround this, let me know please.

Thank you
Johnny

Reply With Quote
  #8 (permalink)  
Old 08-31-2008, 01:29 AM
=?Utf-8?B?S2VuIFNoZXJpZGFu?=
 
Posts: n/a
Re: How can point me out the best way on a query

Access is not good at handling correlated subqueries and these are fairly
complex ones. First thing is to make sure the relevant columns are indexed
of course, but you might be asking too much of it here.

Ken Sheridan
Stafford, England

"Johnny" wrote:

> Hello Ken
>
> Everything worked like you said I get the latest debits, but the
> query is very, very, very slow, and computer freezes most of the
> times.
> Transactions table has around 1000 records, If you know a way to get
> arround this, let me know please.
>
> Thank you
> Johnny
>
>


Reply With Quote
Reply

  { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Microsoft > MS Office > Access


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 04:12 PM.


Powered by vBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
© 1999-2008 mindfrost82.com v11.0


Sponsors:
Favoriten hotels | MPAA | Debt Consolidation | Credit Counseling | Loans



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114