473,498 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Subquery Sum by Date Range

Hi,

I'm really new to access. (Am only a beginner in SQL) I'm trying to
create subqueries similar to SQL+ for a sum using case when within date
ranges. (Or Decode by month sum)

i.e. Trying to get total shipped qty for customers based on quarters.
10-01-04 through 12-31-04, then 01-01-05 thru 03-31-05... etc

Current access table look like:

Cust# Ship Qty Trx Date Cost ... etc
A01 1000 01-01-05
A01 1500 11-01-04
A01 100 01-15-05
A01 80 02-05-05
A01 500 01-01-05
B05 60 03-16-05
B05 1560 10-23-04
B05 25 01-15-05
B05 600 02-05-05
B05 450 01-01-05

Need for it to look something like:

Cust# Q1 Qty Q2 Qty Cost ... etc
A01 1500 1680
B05 1560 1235

We can't use cross tab as there are 3 queries linked to the main query
table we would like to run this subquery on. There are also 12 columns
to the right of cost. Crosstab report won't link all that info.

If it were in SQL, then it would look something like this:
, CASE
when trx_date BETWEEN '01-OCT-2004' AND '31-DEC-2004' then sum ship_qty

when a.trx_date BETWEEN '01-JAN-2005' AND '31-MAR-2005' then sum
ship_qty
END
Or in decode:
,-1*(DECODE(TO_CHAR(A.TRX_DATE,'MON-YY'),TO_CHAR(SYSDATE,'MON-YY'),(SUM(SHIP_QTY)),0))
QTY1
,-1*(DECODE(TO_CHAR(A.TRX_DATE,'MON-YY'),TO_CHAR(SYSDATE,'MON-YY'),(SUM(SHIP_QTY)),0))
QTY1

Any help, suggestions would be greatly appreciated.

Thank you.

Cindy

ci******@yahoo.com

Nov 13 '05 #1
1 6717
ci******@yahoo.com wrote:
Hi,

I'm really new to access. (Am only a beginner in SQL) I'm trying to
create subqueries similar to SQL+ for a sum using case when within date
ranges. (Or Decode by month sum)

i.e. Trying to get total shipped qty for customers based on quarters.
10-01-04 through 12-31-04, then 01-01-05 thru 03-31-05... etc

Current access table look like:

Cust# Ship Qty Trx Date Cost ... etc
A01 1000 01-01-05
A01 1500 11-01-04
A01 100 01-15-05
A01 80 02-05-05
A01 500 01-01-05
B05 60 03-16-05
B05 1560 10-23-04
B05 25 01-15-05
B05 600 02-05-05
B05 450 01-01-05

Need for it to look something like:

Cust# Q1 Qty Q2 Qty Cost ... etc
A01 1500 1680
B05 1560 1235

We can't use cross tab as there are 3 queries linked to the main query
table we would like to run this subquery on. There are also 12 columns
to the right of cost. Crosstab report won't link all that info.

If it were in SQL, then it would look something like this:
, CASE
when trx_date BETWEEN '01-OCT-2004' AND '31-DEC-2004' then sum ship_qty

when a.trx_date BETWEEN '01-JAN-2005' AND '31-MAR-2005' then sum
ship_qty
END
Or in decode:
,-1*(DECODE(TO_CHAR(A.TRX_DATE,'MON-YY'),TO_CHAR(SYSDATE,'MON-YY'),(SUM(SHIP_QTY)),0))
QTY1
,-1*(DECODE(TO_CHAR(A.TRX_DATE,'MON-YY'),TO_CHAR(SYSDATE,'MON-YY'),(SUM(SHIP_QTY)),0))
QTY1

Any help, suggestions would be greatly appreciated.


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

In Access you'd do something like this:

Sum(IIf(a.trx_date BETWEEN #1/1/2005# AND #3/31/2005#, ship_qty)) As
SumShipQty

The IIf() (Immediate If) function has this syntax:

IIf(evaluation expression, true expression, false expression)

If the evaluation is true the true expression is returned; otherwise,
the false expression is returned. When using IIf() in Access' SQL if
the false expression is left off and the evaluation yields False, then a
NULL is returned. The SUM() will sum the ship_qty only when the
evaluation expression returns True; otherwise it will sum NULL, which
returns zero.
--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQzwMXYechKqOuFEgEQKTrACg5RlDOKQbWz6wtV+sJbupTd Gb4p8AniTD
OvyNxqQfNzrmYdjZc5PDlgy4
=eLXN
-----END PGP SIGNATURE-----
Nov 13 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5197
by: androtech | last post by:
Hello, I'm looking for a function that returns a date range for a specified week number of the year. I'm not able to find functions like this anywhere. Any pointers/help would be much...
4
1688
by: abuse | last post by:
i want do the following but am limited by my version of mysql which I am not able to upgrade. please advise. uptime_table ************ date num_days_up .... ************
0
1212
by: JT | last post by:
I'm using access 2000 and with assistance thought I had this problem licked. This query works: SELECT Q3.id, Q3.date, Max(Q3.Rank) AS Ninetieth FROM query3 AS Q3 WHERE...
18
38203
by: dfetrow410 | last post by:
Anyone have some code that will do this? Dave
2
1599
Spazasaurus
by: Spazasaurus | last post by:
I am having trouble. I am not sure if it is not possible or not, but don't know any alternatives. I am converting my site from PHP and MYSQL to ASP.NET and MSSQL. In my current site. I did a query...
0
1265
by: fubaba | last post by:
hi, i execute a store procedure got Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.'...
4
2835
Sandboxer
by: Sandboxer | last post by:
I want to be able to program Access to provide for me, by individual day, what my contract obligations are to my customers. Will Access recognize all the individual days in between a date range...
2
2608
theGeek
by: theGeek | last post by:
I always wonder which one of join or subquery should I be using to solve a particuar problem so that I get the correct result set in minimum time. It usually happens that I can write a query quickly...
7
2271
by: nsymiakakis | last post by:
Hi All, I am have some trouble with a parameter query that I am hoping someone can guide me. I have a Parameter query with a few fields that people can filter with, Such as "Department", Date...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7165
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7203
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4908
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3081
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1417
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.