A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Running & Setting Up Queries
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Ageing by Date to 30, 60,90 etc



 
 
Thread Tools Display Modes
  #1  
Old April 20th, 2007, 12:28 PM posted to microsoft.public.access.queries
WendyUK
external usenet poster
 
Posts: 11
Default Ageing by Date to 30, 60,90 etc

I am trying to age transactions into 30 days, 60 days etc and have put
together the following script but is giving a syntax error on "AS" on the 2nd
SUM line, can anyone please help me.

SELECT

PLSupplierAccountID, GoodsValueInAccountCurrency, TransactionDate,
DATEDIFF(DAY, TransactionDate, GETDATE()) AS no_of_days_since,

SUM(GoodsValueInAccountCurrency) AS THIRTY WHERE (no_of_days_since BETWEEN 1
AND 30)
AND
SUM(GoodsValueInAccountCurrency) AS SIXTY WHERE (no_of_days_since between 31
and 60)
AND
SUM(GoodsValueInAccountCurrency) AS NINETY WHERE (no_of_days_since between
61 and 90)
and
SUM(GoodsValueInAccountCurrency) AS MORE WHERE (no_of_days_since 90)

FROM PLPostedSupplierTran
GROUP by PLSupplierAccountID, TransactionDate, GoodsValueInAccountCurrency

  #2  
Old April 20th, 2007, 12:42 PM posted to microsoft.public.access.queries
Ofer Cohen
external usenet poster
 
Posts: 1,683
Default Ageing by Date to 30, 60,90 etc

You can have only one Where in a query, unless you are using SubQueries
(which is not the case), instead use IIf for the criteria.

Soething like:
SELECT
PLSupplierAccountID, GoodsValueInAccountCurrency, TransactionDate,
DATEDIFF(DAY, TransactionDate, GETDATE()) AS no_of_days_since,
SUM(IIf(no_of_days_since BETWEEN 1 AND 30, GoodsValueInAccountCurrency, 0))
AS THIRTY ,
SUM(IIf(no_of_days_since between 31 and 60 , GoodsValueInAccountCurrency,
0)) AS SIXTY,
SUM(IIf(no_of_days_since between 61 and 90 , GoodsValueInAccountCurrency, 0))
AS NINTY ,
SUM(IIf(no_of_days_since 90 , GoodsValueInAccountCurrency, 0))
As GreaterThenNinty

FROM PLPostedSupplierTran
GROUP by PLSupplierAccountID, TransactionDate, GoodsValueInAccountCurrency
--
Good Luck
BS"D


"WendyUK" wrote:

I am trying to age transactions into 30 days, 60 days etc and have put
together the following script but is giving a syntax error on "AS" on the 2nd
SUM line, can anyone please help me.

SELECT

PLSupplierAccountID, GoodsValueInAccountCurrency, TransactionDate,
DATEDIFF(DAY, TransactionDate, GETDATE()) AS no_of_days_since,

SUM(GoodsValueInAccountCurrency) AS THIRTY WHERE (no_of_days_since BETWEEN 1
AND 30)
AND
SUM(GoodsValueInAccountCurrency) AS SIXTY WHERE (no_of_days_since between 31
and 60)
AND
SUM(GoodsValueInAccountCurrency) AS NINETY WHERE (no_of_days_since between
61 and 90)
and
SUM(GoodsValueInAccountCurrency) AS MORE WHERE (no_of_days_since 90)

FROM PLPostedSupplierTran
GROUP by PLSupplierAccountID, TransactionDate, GoodsValueInAccountCurrency

  #3  
Old April 20th, 2007, 12:56 PM posted to microsoft.public.access.queries
Stefan Hoffmann
external usenet poster
 
Posts: 709
Default Ageing by Date to 30, 60,90 etc

hi Wendy,

WendyUK wrote:
SELECT
PLSupplierAccountID, GoodsValueInAccountCurrency, TransactionDate,
DATEDIFF(DAY, TransactionDate, GETDATE()) AS no_of_days_since,
SUM(GoodsValueInAccountCurrency) AS THIRTY WHERE (no_of_days_since BETWEEN 1
AND 30)
AND
SUM(GoodsValueInAccountCurrency) AS SIXTY WHERE (no_of_days_since between 31
and 60)
AND
SUM(GoodsValueInAccountCurrency) AS NINETY WHERE (no_of_days_since between
61 and 90)
and
SUM(GoodsValueInAccountCurrency) AS MORE WHERE (no_of_days_since 90)
FROM PLPostedSupplierTran
GROUP by PLSupplierAccountID, TransactionDate, GoodsValueInAccountCurrency

Interesting approach, but while it doesen't work, it also contains a
major mistake:

You won't get a real SUM() when using GROUP BY for the same field, e.g.

SELECT id, Sum(id) FROM table GROUP BY id.

I'm also not sure, whether the GROUP BY TransactionDate gives you the
correct results.


You like to aggregate your GoodsValue per month.

You need two queries like these:

1. Calculate passed days

SELECT *,
DATEDIFF(DAY, TransactionDate, GETDATE()) AS no_of_days_since
FROM PLPostedSupplierTran

Save this query as daysSince.

2. An query to generate your groups

SELECT PLSupplierAccountID, TransactionDate,
DSum("GoodsValueInAccountCurrency",
"daysSince",
"(no_of_days_since BETWEEN 1 AND 30) AND" &
"(PLSupplierAccountID = " & PLSupplierAccountID & ") AND " &
"(TransactionDate = " & TransactionDate & ")"
FROM PLPostedSupplierTran
GROUP BY PLSupplierAccountID, TransactionDate

Not tested, but it should show you the right direction.


mfG
-- stefan --

  #4  
Old April 20th, 2007, 01:54 PM posted to microsoft.public.access.queries
WendyUK
external usenet poster
 
Posts: 11
Default Ageing by Date to 30, 60,90 etc



"Ofer Cohen" wrote:

You can have only one Where in a query, unless you are using SubQueries
(which is not the case), instead use IIf for the criteria.

Soething like:
SELECT
PLSupplierAccountID, GoodsValueInAccountCurrency, TransactionDate,
DATEDIFF(DAY, TransactionDate, GETDATE()) AS no_of_days_since,
SUM(IIf(no_of_days_since BETWEEN 1 AND 30, GoodsValueInAccountCurrency, 0))
AS THIRTY ,
SUM(IIf(no_of_days_since between 31 and 60 , GoodsValueInAccountCurrency,
0)) AS SIXTY,
SUM(IIf(no_of_days_since between 61 and 90 , GoodsValueInAccountCurrency, 0))
AS NINTY ,
SUM(IIf(no_of_days_since 90 , GoodsValueInAccountCurrency, 0))
As GreaterThenNinty

FROM PLPostedSupplierTran
GROUP by PLSupplierAccountID, TransactionDate, GoodsValueInAccountCurrency
--
Good Luck
BS"D


"WendyUK" wrote:

I am trying to age transactions into 30 days, 60 days etc and have put
together the following script but is giving a syntax error on "AS" on the 2nd
SUM line, can anyone please help me.

SELECT

PLSupplierAccountID, GoodsValueInAccountCurrency, TransactionDate,
DATEDIFF(DAY, TransactionDate, GETDATE()) AS no_of_days_since,

SUM(GoodsValueInAccountCurrency) AS THIRTY WHERE (no_of_days_since BETWEEN 1
AND 30)
AND
SUM(GoodsValueInAccountCurrency) AS SIXTY WHERE (no_of_days_since between 31
and 60)
AND
SUM(GoodsValueInAccountCurrency) AS NINETY WHERE (no_of_days_since between
61 and 90)
and
SUM(GoodsValueInAccountCurrency) AS MORE WHERE (no_of_days_since 90)

FROM PLPostedSupplierTran
GROUP by PLSupplierAccountID, TransactionDate, GoodsValueInAccountCurrency

  #5  
Old April 20th, 2007, 01:54 PM posted to microsoft.public.access.queries
WendyUK
external usenet poster
 
Posts: 11
Default Ageing by Date to 30, 60,90 etc

Thank you but this one falls over on the "between"

"Ofer Cohen" wrote:

You can have only one Where in a query, unless you are using SubQueries
(which is not the case), instead use IIf for the criteria.

Soething like:
SELECT
PLSupplierAccountID, GoodsValueInAccountCurrency, TransactionDate,
DATEDIFF(DAY, TransactionDate, GETDATE()) AS no_of_days_since,
SUM(IIf(no_of_days_since BETWEEN 1 AND 30, GoodsValueInAccountCurrency, 0))
AS THIRTY ,
SUM(IIf(no_of_days_since between 31 and 60 , GoodsValueInAccountCurrency,
0)) AS SIXTY,
SUM(IIf(no_of_days_since between 61 and 90 , GoodsValueInAccountCurrency, 0))
AS NINTY ,
SUM(IIf(no_of_days_since 90 , GoodsValueInAccountCurrency, 0))
As GreaterThenNinty

FROM PLPostedSupplierTran
GROUP by PLSupplierAccountID, TransactionDate, GoodsValueInAccountCurrency
--
Good Luck
BS"D


"WendyUK" wrote:

I am trying to age transactions into 30 days, 60 days etc and have put
together the following script but is giving a syntax error on "AS" on the 2nd
SUM line, can anyone please help me.

SELECT

PLSupplierAccountID, GoodsValueInAccountCurrency, TransactionDate,
DATEDIFF(DAY, TransactionDate, GETDATE()) AS no_of_days_since,

SUM(GoodsValueInAccountCurrency) AS THIRTY WHERE (no_of_days_since BETWEEN 1
AND 30)
AND
SUM(GoodsValueInAccountCurrency) AS SIXTY WHERE (no_of_days_since between 31
and 60)
AND
SUM(GoodsValueInAccountCurrency) AS NINETY WHERE (no_of_days_since between
61 and 90)
and
SUM(GoodsValueInAccountCurrency) AS MORE WHERE (no_of_days_since 90)

FROM PLPostedSupplierTran
GROUP by PLSupplierAccountID, TransactionDate, GoodsValueInAccountCurrency

  #6  
Old April 20th, 2007, 01:56 PM posted to microsoft.public.access.queries
WendyUK
external usenet poster
 
Posts: 11
Default Ageing by Date to 30, 60,90 etc

Thank you Stefan, this seems to work except for an error at the "FROM" is
that because there is syntax immediately before the from?

"Stefan Hoffmann" wrote:

hi Wendy,

WendyUK wrote:
SELECT
PLSupplierAccountID, GoodsValueInAccountCurrency, TransactionDate,
DATEDIFF(DAY, TransactionDate, GETDATE()) AS no_of_days_since,
SUM(GoodsValueInAccountCurrency) AS THIRTY WHERE (no_of_days_since BETWEEN 1
AND 30)
AND
SUM(GoodsValueInAccountCurrency) AS SIXTY WHERE (no_of_days_since between 31
and 60)
AND
SUM(GoodsValueInAccountCurrency) AS NINETY WHERE (no_of_days_since between
61 and 90)
and
SUM(GoodsValueInAccountCurrency) AS MORE WHERE (no_of_days_since 90)
FROM PLPostedSupplierTran
GROUP by PLSupplierAccountID, TransactionDate, GoodsValueInAccountCurrency

Interesting approach, but while it doesen't work, it also contains a
major mistake:

You won't get a real SUM() when using GROUP BY for the same field, e.g.

SELECT id, Sum(id) FROM table GROUP BY id.

I'm also not sure, whether the GROUP BY TransactionDate gives you the
correct results.


You like to aggregate your GoodsValue per month.

You need two queries like these:

1. Calculate passed days

SELECT *,
DATEDIFF(DAY, TransactionDate, GETDATE()) AS no_of_days_since
FROM PLPostedSupplierTran

Save this query as daysSince.

2. An query to generate your groups

SELECT PLSupplierAccountID, TransactionDate,
DSum("GoodsValueInAccountCurrency",
"daysSince",
"(no_of_days_since BETWEEN 1 AND 30) AND" &
"(PLSupplierAccountID = " & PLSupplierAccountID & ") AND " &
"(TransactionDate = " & TransactionDate & ")"
FROM PLPostedSupplierTran
GROUP BY PLSupplierAccountID, TransactionDate

Not tested, but it should show you the right direction.


mfG
-- stefan --


  #7  
Old April 20th, 2007, 01:56 PM posted to microsoft.public.access.queries
Michael Gramelspacher
external usenet poster
 
Posts: 482
Default Ageing by Date to 30, 60,90 etc

In article ,
says...
I am trying to age transactions into 30 days, 60 days etc and have put
together the following script but is giving a syntax error on "AS" on the 2nd
SUM line, can anyone please help me.

SELECT

PLSupplierAccountID, GoodsValueInAccountCurrency, TransactionDate,
DATEDIFF(DAY, TransactionDate, GETDATE()) AS no_of_days_since,

SUM(GoodsValueInAccountCurrency) AS THIRTY WHERE (no_of_days_since BETWEEN 1
AND 30)
AND
SUM(GoodsValueInAccountCurrency) AS SIXTY WHERE (no_of_days_since between 31
and 60)
AND
SUM(GoodsValueInAccountCurrency) AS NINETY WHERE (no_of_days_since between
61 and 90)
and
SUM(GoodsValueInAccountCurrency) AS MORE WHERE (no_of_days_since 90)

FROM PLPostedSupplierTran
GROUP by PLSupplierAccountID, TransactionDate, GoodsValueInAccountCurrency


Maybe:

SELECT PLSupplierAccountID,
GoodsValueInAccountCurrency,
TransactionDate,
DATEDIFF(DAY,TransactionDate,GETDATE()) AS no_of_days_since,
SUM(IIF(DATEDIFF(DAY,TransactionDate,GETDATE()) BETWEEN 1
AND 30,
GoodsValueInAccountCurrency,0)) AS THIRTY,
SUM(IIF(DATEDIFF(DAY,TransactionDate,GETDATE()) BETWEEN 31
AND 60,
GoodsValueInAccountCurrency,0)) AS SIXTY,
SUM(IIF(DATEDIFF(DAY,TransactionDate,GETDATE()) BETWEEN 61
AND 90,
GoodsValueInAccountCurrency,0)) AS NINTY,
SUM(IIF(DATEDIFF(DAY,TransactionDate,GETDATE()) 90,
GoodsValueInAccountCurrency,0)) AS GreaterThenNinty
FROM PLPostedSupplierTran
GROUP BY PLSupplierAccountID,TransactionDate,GoodsValueInAc countCurrency
  #8  
Old April 20th, 2007, 02:16 PM posted to microsoft.public.access.queries
Stefan Hoffmann
external usenet poster
 
Posts: 709
Default Ageing by Date to 30, 60,90 etc

hi Wendy,

WendyUK wrote:
Thank you Stefan, this seems to work except for an error at the "FROM" is
that because there is syntax immediately before the from?

Yup, there is a closing ) missing for the DSum().


mfG
-- stefan --
  #9  
Old April 20th, 2007, 02:25 PM posted to microsoft.public.access.queries
Michel Walsh
external usenet poster
 
Posts: 2,404
Default Ageing by Date to 30, 60,90 etc

You can use a crosstab with PARTITION in the PIVOT clause. Something like
(untested):



TRANSFORM SUM(GoodsValuesInAccountCurrency)
SELECT PLSupplierAccountID
FROM PLPostedSupplierTran
GROUP BY PLSupplierAccountID
PIVOT PARTITION(no_of_days_since, 0, 120, 30)




Hoping it may help,
Vanderghast, Access MVP

"WendyUK" wrote in message
news
I am trying to age transactions into 30 days, 60 days etc and have put
together the following script but is giving a syntax error on "AS" on the
2nd
SUM line, can anyone please help me.

SELECT

PLSupplierAccountID, GoodsValueInAccountCurrency, TransactionDate,
DATEDIFF(DAY, TransactionDate, GETDATE()) AS no_of_days_since,

SUM(GoodsValueInAccountCurrency) AS THIRTY WHERE (no_of_days_since BETWEEN
1
AND 30)
AND
SUM(GoodsValueInAccountCurrency) AS SIXTY WHERE (no_of_days_since between
31
and 60)
AND
SUM(GoodsValueInAccountCurrency) AS NINETY WHERE (no_of_days_since between
61 and 90)
and
SUM(GoodsValueInAccountCurrency) AS MORE WHERE (no_of_days_since 90)

FROM PLPostedSupplierTran
GROUP by PLSupplierAccountID, TransactionDate, GoodsValueInAccountCurrency



  #10  
Old April 20th, 2007, 04:32 PM posted to microsoft.public.access.queries
WendyUK
external usenet poster
 
Posts: 11
Default Ageing by Date to 30, 60,90 etc

Thank you Stefan, much appreciated

"Stefan Hoffmann" wrote:

hi Wendy,

WendyUK wrote:
Thank you Stefan, this seems to work except for an error at the "FROM" is
that because there is syntax immediately before the from?

Yup, there is a closing ) missing for the DSum().


mfG
-- stefan --

 




Thread Tools
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 On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 03:01 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.