473,394 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How can I use DMax function and apply it to a single customer ID?

269 256MB
I have a report named "rptHOTSTICKS_EXPIRING" based on a query named "HOSTICKS_SHIPPING_REPORT Query". The query contains these fields:
ORDER_NUM (text)
CUST_NUM (text)
Name, address, contact info, I think not important to this thread
DATE_SHIP (date)
Then I have a field
Expand|Select|Wrap|Line Numbers
  1. STICKS_EXPIRE: DateAdd("yyyy",2,[DATE_SHIP])
with criteria
Expand|Select|Wrap|Line Numbers
  1. Between [From Date] And [To Date]
where [FromDate] and [To Date] are parameters.

My report is to let me know when sticks are coming up that are 2 years old or more. However, if I have already shipped a new order (say like last week) I want to exclude it from my report. For example, I have a customer with sticks expiring 12/10/09, but I shipped him a new order 11/4/09. Currently he appears on my report but I do not want him to. (The report is to alert me of upcoming orders to fill, and his won't be one because it's already filled!) I thought I could use DMax to help me out here, but I don't know how to limit it to each customer number in the report. Right now the result gives me the max ship date in the entire table and reports that date for every result of the query. I thought I could use criteria on the DMax result, perhaps if LAST_SHIPMENT and DATE_SHIP are equal, then put them on the report.
Here is how I'm trying to use the DMax command in my query. (I'm not saying it's correct, but I tried.) (HOTSTICKS_ORDERS is a table name. I didn't learn good naming practices in time for this one.)
Expand|Select|Wrap|Line Numbers
  1. LAST_SHIPMENT: DMax("[DATE_SHIP]","HOTSTICK_ORDERS")
I hoped since the query was based on CUST_NUM it would do DMax for each customer. I also tried putting [CUST_NUM] on the end of that DMax argument but that didn't work either. Could anyone offer me any pointers?

As always, if I'm not clear, please let me know. Thanks in advance!!!!!!
Nov 18 '09 #1

✓ answered by topher23

And here's an alternative if it doesn't.

Make a second query with only the [CUST_NUM] and [DATE_SHIP] fields in Query Designer. Now, right-click and activate the Totals. Set [DATE_SHIP]'s total to Max and [CUST_NUM] to Group By.

Now, do a Join from your original query's [DATE_SHIP] to this new query's [DATE_SHIP]. Have it show all values from your original query and only those from the new one that match (I think that's a right join, but I can never remember those directions). Now, pull either of the fields from the new query into your original query and do a filter on it for Is Not Null.

EDIT: You can actually just do a join where all fields match for this (inner join), and skip the filter for Is Not Null. Sorry, I was thinking originally about filtering out the matches, not only allowing matches.

Now you will only get records in which the record listed is the newest record for that customer.

Viva la SQL! :)

21 4587
ChipR
1,287 Expert 1GB
I just can't figure out what you mean by "sticks that are coming up that are 2 years old or more". Those terms don't appear to relate to anything in your post.
From your example, why don't you just do a select from customers who haven't ordered within the last 2 years?
Nov 18 '09 #2
DanicaDear
269 256MB
ChipR, thanks for the fast reply. The reason why I don't want to just look at orders over 2 years is because (trying to make long story short) these sticks are safety devices and we actually have to ship a new order before the old order becomes two years old. The customer ships back the old order *after* receiving the new order. They can never be without sticks.

I think the real reason I have this arranged the way I do is because we test the sticks they return and put them back into inventory. To ensure we have enough sticks to fill all upcoming orders, we may want to look at *just* next month's orders, or *just* what we need the month after that, and not everything over two years old. I hope this answers your question and sheds light on my efforts.

If you still think I'm going about this the wrong way I'm willing to listen. I think what you are saying has merit and usually I have to let these things settle in for a little while before I totally get it. Thanks!!
Nov 18 '09 #3
ChipR
1,287 Expert 1GB
Can you define exactly what you want to see in the report based on the fields and information you've given?
Nov 18 '09 #4
topher23
234 Expert 100+
Danica likes to talk about her sticks, as anyone who has helped her knows. It might be helpful to know what a "hotstick" is, for those who have nothing to do with the pwer business.

A hotstick an electrically insulated pole, usually made of fiberglass, that high-voltage electrical workers use to shut off power switches from a safe distance, or to connect tools to when they have to do work without cutting off the power. This is why they are very important safety items. OSHA in the U.S. requires that they be tested and replaced if defective every two years, so businesses like Danica's ship out known good replacements every two years and take back the old ones for testing.

Hope that's helpful. My father in law's a substation tech.
Nov 18 '09 #5
DanicaDear
269 256MB
Right on, topher!!!!!!!!!!!!!!!!!!!!!
Nov 18 '09 #6
DanicaDear
269 256MB
ChipR,
If I can't, I'm in trouble. LOL. So here goes:
I want to see CUST_NUM whose STICKS_EXPIRE (as defined above) are between the two parameters defined above, *and* there are no DATE_SHIP occuring later than the one causing the STICKS_EXPIRE to be within my parameters. (note STICKS_EXPIRE is based off of DATE_SHIP).
For instance, if STICKS_EXPIRE 12/10/09, then the DATE_SHIP would be 12/10/08. If 12/10/08 is the last DATE_SHIP, I want it in the report. If that same CUST_NAME has a DATE_SHIP entry of 11/4/09, I don't want it in the report. I'm including SQL of my current query; it might help you. Be aware, I don't really understand how to work in SQL--I still consider myself a beginner.
Expand|Select|Wrap|Line Numbers
  1. PARAMETERS [From Date] DateTime, [To Date] DateTime;
  2. SELECT HOTSTICK_ORDERS.ORDER_NUM,
  3.        HOTSTICK_ORDERS.CUST_NUM,
  4.        HOTSTICK_CUSTOMERS.[First_Name] & " " & [Last_Name] & " " & [Suffix] AS Whole_Name,
  5.        HOTSTICK_CUSTOMERS.ADDR1,
  6.        HOTSTICK_CUSTOMERS.ADDR2,
  7.        HOTSTICK_CUSTOMERS.ADDR3,
  8.        [Addr1] & (Chr(13) & Chr(10)+[Addr2]) & (Chr(13) & Chr(10)+[Addr3]) & (Chr(13) & Chr(10)+[city_ST_zip]) AS Addr,
  9.        [HOTSTICK_CUSTOMERS].[CITY] & ", " & [ST] & "  " & [ZIP] AS City_ST_Zip,
  10.        HOTSTICK_CUSTOMERS.INTERCOMPANY_PHONE,
  11.        HOTSTICK_CUSTOMERS.EXTERNAL_PHONE,
  12.        HOTSTICK_CUSTOMERS.INTERCOMPANY_PHONE,
  13.        HOTSTICK_CUSTOMERS.[RADIO/LINC],
  14.        HOTSTICK_CUSTOMERS.CELL,
  15.        HOTSTICK_CUSTOMERS.COMPANY_EMAIL,
  16.        HOTSTICK_CUSTOMERS.DIV,
  17.        HOTSTICK_ORDERS.DATE_SHIP,
  18.        HOTSTICK_ORDERS.BOX_NUM,
  19.        HOTSTICK_ORDERS.DATE_RET,
  20.        HOTSTICK_ORDERS.REMARKS,
  21.        DateAdd("yyyy",2,[DATE_SHIP]) AS STICKS_EXPIRE,
  22.        DMax("[DATE_SHIP]","HOTSTICK_ORDERS") AS LAST_SHIPMENT
  23.  
  24. FROM   HOTSTICK_CUSTOMERS INNER JOIN
  25.        HOTSTICK_ORDERS
  26.   ON   HOTSTICK_CUSTOMERS.CUST_NUM=HOTSTICK_ORDERS.CUST_NUM
  27.  
  28. WHERE  (DateAdd("yyyy",2,[DATE_SHIP]) Between [From Date] And [To Date]);
Nov 18 '09 #7
DanicaDear
269 256MB
I'm sorry about that long line in the CODE. I don't know how to make it not do that.
Nov 18 '09 #8
topher23
234 Expert 100+
I see what you're trying to do now, anyway. Try something like this:

Expand|Select|Wrap|Line Numbers
  1. Your SQL statement...
  2. WHERE (((DateAdd("yyyy",2,[DATE_SHIP])) Between [From Date] And [To Date])) 
  3. AND [DATE_SHIP] = DMax("[DATE_SHIP]","HOTSTICK_ORDERS","[CUST_NUM] = " & HOTSTICK_ORDERS.CUST_NUM) ; 
That feels like it will work.
Nov 18 '09 #9
ChipR
1,287 Expert 1GB
What if the same customer has multiple shipping dates which will be expiring, and none more recent? Would you want to show them all, or only the latest?
Nov 18 '09 #10
topher23
234 Expert 100+
And here's an alternative if it doesn't.

Make a second query with only the [CUST_NUM] and [DATE_SHIP] fields in Query Designer. Now, right-click and activate the Totals. Set [DATE_SHIP]'s total to Max and [CUST_NUM] to Group By.

Now, do a Join from your original query's [DATE_SHIP] to this new query's [DATE_SHIP]. Have it show all values from your original query and only those from the new one that match (I think that's a right join, but I can never remember those directions). Now, pull either of the fields from the new query into your original query and do a filter on it for Is Not Null.

EDIT: You can actually just do a join where all fields match for this (inner join), and skip the filter for Is Not Null. Sorry, I was thinking originally about filtering out the matches, not only allowing matches.

Now you will only get records in which the record listed is the newest record for that customer.

Viva la SQL! :)
Nov 18 '09 #11
topher23
234 Expert 100+
Dangit, Chip, why do you want to ruin things with questions?

Chip has a point. Would such an occasion ever exist?
Nov 18 '09 #12
NeoPa
32,556 Expert Mod 16PB
Performance-wise, using DMax() (or any of the Domain Aggregate functions) within a SQL query is bad news. I would consider using GROUP BY in your query. Maybe as a linked sub-query.

PS. Carriage Retun/Line Feeds can be done using the predefined vbCrLf in your code (or in the SQL).
Nov 18 '09 #13
DanicaDear
269 256MB
@ChipR
ChipR, now that I've had time for this to sink in, I think you're right on. If there was a *new* order, then there should be a *new* expiration date, so it wouldn't fall in my range. To continue the same example....if the 11/4/09 order was made, then the new expiration date would be 11/4/11 and it wouldn't come up when I enter my parameter from 12/1/09 to 12/31/09. However, that's *not* what is happening in my report...it's pulling up all orders that *ever* had an expiration date during that time. I need the report to look at just the *latest* expiration date. Perhaps I could use my DMax there.

To All, I haven't read all your replies yet. You may have already provided me with the info I seek. I'm caring for a sick child tonight. Will study up on what you wrote and get this worked out tomorrow. Thanks everyone!
Nov 18 '09 #14
DanicaDear
269 256MB
topher23,
The SQL you provided didn't quite work and plus NeoPa suggested not using DMax. So I'm going to try your second approach. Why must I have 2 queries? Why can't you do the Max total on the main query? (I already know it won't work because I tried it.) :-)

ChipR,
It is possible to have a customer having two orders expiring at the same time. It's very unlikely but still possible. However, they would have separate order numbers (I haven't discussed order number in this thread...it's a subreport, so I don't think it's an issue.) I would want to see all that customer's orders expiring between the parameter dates specified.
Nov 19 '09 #15
DanicaDear
269 256MB
topher23,
It worked!!!!!!!!!!!!!!!!!! Thanks so much! You people are so slick it makes me sick. Hahahahaha.
Nov 19 '09 #16
NeoPa
32,556 Expert Mod 16PB
@DanicaDear
Generally you just need to remember two things :
  1. Use the CODE tags (You already know this).
  2. Use only spaces for formatting. Never TABs.
Nov 19 '09 #17
DanicaDear
269 256MB
@NeoPa
NeoPa,
I copied the SQL and pasted it in. I didn't use spaces or tabs. LOL. I just used the query wizard and clipboard. :-) So now what?
Nov 19 '09 #18
NeoPa
32,556 Expert Mod 16PB
I would always recommend people reformat SQL before posting. Each field on a separate line; Use the same position on the line as much as possible so they display as a column. These things make reading it much easier, and spotting problems (yourself before even posting it) much more likely.

Remember that Access creates SQL exclusively for its Jet SQL Engine. Access isn't failing in that sense, as it is quite fit for purpose. Posting on a forum however, is quite a different thing and has different requirements. I would never dream of posting SQL straight from a QueryDef into a post (although we do often ask members to do just that - anything more complicated can prove a step too far). Most of our experts can work with that, but as it gets ore complicated, so it gets exponentially more difficult to work with. On the other hand, having a post with clearly structured SQL makes it easier to answer, and therefore gives you a better chance of an appropriate reply.

Formatting the SQL in a text editor (They typically use non-proportional fonts so columns can be easily aligned correctly) prior to posting is a good idea and a technique I always use.
Nov 19 '09 #19
topher23
234 Expert 100+
@DanicaDear
With the Totals option, you really have to think about what you're trying to do. When you use totals, everything in your query has to total somehow (unless it's an expression, which is an entirely different issue) or be a Group On field. When a query isn't too complicated, you can use just one query, but as it gets more complex the variables involved when you do totals can create undesired results. So, if all you're looking for in the totals is the Max of a certain value (like the date), but you don't want to hassle with all of the other issues that come up on a query that uses totals, you do it with 2 queries.

And, as NeoPa noted, using those 2 queries is much better, performance-wise, than using DMax. With 2 queries, both queries are only run once, for a total of just 2 queries. The DMax function is sort of a query in itself, along with some extra processing, so what you end up with is a second query running for every record in the query. The total number of queries run for the set is then the number of records in the query plus one for the original query. And once you've got several thousand records, that's a lotta queries!
Nov 20 '09 #20
NeoPa
32,556 Expert Mod 16PB
As he said...

That explains perfectly why I don't recommend using Domain Aggregate functions (DMax(), DCount(), DSum(), etc) within queries.
Nov 20 '09 #21
DanicaDear
269 256MB
Great explanations. Thanks to you all.
Nov 20 '09 #22

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
1
by: David Furey | last post by:
Hi I have an XML documnet and a XSLT document as shown below THe XSLT document brings back a filtered docmument that has the VendorName that starts with a particular sub-string This works as...
4
by: lukeargent | last post by:
Hi All, I have come across some rather weird error that I can only assume is something to do with my ADP file connecting to SQL Server 2000. I'm using Access XP as my front end. In simple...
10
by: SQL Server | last post by:
I am writing a function which will take two parameters. One the field to be returned from a table and second parameter is the ID of the record to be returned. Problem is it's not returning the...
3
by: Ron | last post by:
Hi All, Okay, here's the deal: Access2000/WinXP. Have a database that's split, FE/BE with multiple users having FE and one of those users also has BE (still split though...). Have a form on...
4
by: Ed Marzan | last post by:
Greetings, I have a query that returns varying prices for the same item in the following manner. Item1 Price1 Item1 Price2 Item1 Price3 Item1 Price4
2
by: ontherun | last post by:
Hi, I have a form called Customer based on the table tblCustomer and another form called Job based on the table Job when the user enter the details for the Customer i have a button at the...
2
by: joeyrhyulz | last post by:
Hi, I'm trying to make a very simple update statement (in Oracle) in jet sql that seems much more difficult than it should be. The root of my problem is that I'm trying to update a field on a...
9
by: =?Utf-8?B?RGFya21hbg==?= | last post by:
Hi, I am wondering how you multi-dimension an array function? My declared function looks like this: Public Function GetCustomerList(ByVal Val1 As String, ByVal Val2 As Long, ByVal Val3 As...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...

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.