473,786 Members | 2,428 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_CH AR(A.TRX_DATE,' MON-YY'),TO_CHAR(SY SDATE,'MON-YY'),(SUM(SHIP_ QTY)),0))
QTY1
,-1*(DECODE(TO_CH AR(A.TRX_DATE,' MON-YY'),TO_CHAR(SY SDATE,'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 6729
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_CH AR(A.TRX_DATE,' MON-YY'),TO_CHAR(SY SDATE,'MON-YY'),(SUM(SHIP_ QTY)),0))
QTY1
,-1*(DECODE(TO_CH AR(A.TRX_DATE,' MON-YY'),TO_CHAR(SY SDATE,'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_d ate 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:::mgf0 0 <at> earthlink <decimal-point> net
Oakland, CA (USA)

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

iQA/AwUBQzwMXYechKq OuFEgEQKTrACg5R lDOKQbWz6wtV+sJ bupTdGb4p8AniTD
OvyNxqQfNzrmYdj Zc5PDlgy4
=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
5219
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 appreciated. TIA
4
1711
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
1236
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 (((Q3.Rank)<=(0.9*(SELECT COUNT() FROM query3 as Q3A WHERE (Q3A.ID = Q3.ID) AND (Q3A. = Q3.))))) GROUP BY Q3.id, Q3.date;
18
38248
by: dfetrow410 | last post by:
Anyone have some code that will do this? Dave
2
1611
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 and then did another query on each record while looping through those results to get what I am trying to do in one query now. I would simply do that same thing but I am not sure how while using Visual Studio and I know that it was bad code, so...
0
1281
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.' message is there any one help with these codes below ....... SET @strSQL = (@strSQL + ' AND p.PropertyID NOT IN ( ' + (SELECT DISTINCT a.propertyID FROM propertyAvailability a ...
4
2853
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 (simply a "from" date and a "to" date)? Additionally, I need to delivery a specific quantity of product when the customer's inventory is within about parameter levels. EXAMPLE: "During the Date Range January 1, 2010 through April 30, 2010 when...
2
2633
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 using subqueries (I think I happen to think that way more easily than join way :)) but I see others making use of joins all the time. May be that they can think of a query more easily using joins. I am saying that because I don't usually see much of...
7
2298
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 Range, Shift... This query is meant to sum the number of "Yes" answers as a percentage from a Y/N field. The query works fine and gives me all the filtered records depending on the choices in the query. Here is my problem. I need to create a report that...
0
9647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9492
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10360
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10163
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10108
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9960
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
4064
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 we have to send another system
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.