473,406 Members | 2,745 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,406 software developers and data experts.

SQL First() aggregate function not working as expected

Seth Schrock
2,965 Expert 2GB
I have two queries. The first manipulates the data and sorts it. The second query is based on the first and groups one field and displays another. I'm using the First() function on the field that is displayed, but it is pulling the second one (also the last one, as there are only two in that group). Originally, I thought I just needed to sort it on that field in order to make sure that the order was correct, but that didn't fix it. I originally had several fields, but I have eliminated all but the one and it still has the problem.

Here is the SQL for the trimmed down query:
Expand|Select|Wrap|Line Numbers
  1. SELECT First(qryFixedDates.pol_idx) AS Firstpol_idx
  2. FROM qryFixedDates
  3. GROUP BY qryFixedDates.Policy
  4. ORDER BY First(qryFixedDates.pol_idx);
And here is the SQL for the whole query:
Expand|Select|Wrap|Line Numbers
  1. SELECT qryFixedDates.Policy
  2. , First(qryFixedDates.pol_idx) AS Firstpol_idx
  3. , First(qryFixedDates.brch) AS Firstbrch
  4. , Max(qryFixedDates.effective) AS Maxeffective
  5. , Max(qryFixedDates.expired) AS Maxexpired
  6. , Min(qryFixedDates.datewritten) AS Mindatewritten
  7. , Sum(qryFixedDates.prem) AS TotalPrem
  8. , First(qryFixedDates.prem) AS FirstPrem
  9. , First(qryFixedDates.bco) AS FirstOfbco
  10. , Count(qryFixedDates.Pol_idx) AS Num
  11. FROM qryFixedDates
  12. GROUP BY qryFixedDates.Policy
  13. HAVING (((Max(qryFixedDates.expired))>#12/1/2015#))
  14. ORDER BY First(qryFixedDates.pol_idx);
As you can see in the whole query, there are a few fields on which I'm using the First() function and they are all doing the same thing. I'm not sure what is wrong.
Nov 30 '15 #1
6 3736
Rabbit
12,516 Expert Mod 8TB
The First and Last functions in Access are not intuitive. It ignores all ordering, indexes, and primary keys. Instead it picks the first or last non-deleted row based on some sort of internal ordering.
Nov 30 '15 #2
Seth Schrock
2,965 Expert 2GB
So nice of Microsoft to make such handy functions, LOL. What good are they if you can't know for sure what the results will be?

Oh, well. Thanks for your answer Rabbit. I think that I'll have to use a sub-query to get the results that I want.
Dec 1 '15 #3
NeoPa
32,556 Expert Mod 16PB
Hi Seth.
Sorting by the result you are trying to determine would never get you what you want anyway. You're sorting by the value already determined to be first.
You could try sorting that which provides the data for the GROUP BY in a sub-query and see if that returns the value you want. Alternatively, if it's the first ordered by that field, then wouldn't Min() be a better function to use?

In case you need First() for some reason then what I'm talking about is :
Expand|Select|Wrap|Line Numbers
  1. SELECT   First([pol_idx]) AS [Firstpol_idx]
  2. FROM     (SELECT   [Policy]
  3.                  , [pol_idx]
  4.           FROM     [qryFixedDates]
  5.           ORDER BY [Policy]
  6.                  , [pol_idx]) AS [sQ]
  7. GROUP BY [Policy]
  8. ORDER BY [Policy]
Dec 1 '15 #4
hvsummer
215 128KB
haha, I know your feeling Seth, I've deal with that not long ago.
First and last sql completely useless if we want to get first or last of value each parent groups.

I suggest you to use ADO, use stringSQL to open recordset that have "Order by" to sort, then .Movelast or .movefirst to get the last or first record.

UDF function can be slow, but it's accuracy
just trade off.
Dec 1 '15 #5
Seth Schrock
2,965 Expert 2GB
@NeoPa I'm sorting on the value that I want to be first because the GROUP BY field changes the sort to the [Policy] field instead of the [pol_idx] field. Actually, in the case of the [pol_idx] field, I can use the min() function, but not on the [prem] field. I'm going to see if I can rethink how I'm getting my data and how each group relates to each other.

@hvsummer I'm already having to perform lots of UDFs on my query, so performance is already low and I can't afford to lower it anymore. I think that I'm going to try some other method for the moment.
Dec 1 '15 #6
mbizup
80 64KB
If your pol_idx field is an autonumber, perhaps you can use it in conjuction with the record date in a self join (subquery), to pull the other fields...

Something like this:

Expand|Select|Wrap|Line Numbers
  1. SELECT t.* 
  2. FROM YourTableOrQuery t INNER JOIN
  3. (SELECT MIN(IDField), MIN(TimeStampField) 
  4. FROM YourTableOrQuery 
  5. GROUP BY  {your grouping fields)) q
  6. ON q.IDField = t.IDField AND q.TimeStampField = t.TimeStampField
In the SQL above, t is an alias for the table; q is an alias for a subquery based on that table. The join on ID and timestamp fields allows you to pull the rest of the fields from the records selected in the subquery (you may be able to get by with only one of those fields in the subquery too).
Dec 1 '15 #7

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

Similar topics

2
by: Claudio Lapidus | last post by:
Hello I would like to know how can I define/create a new aggregate function. I need a custom function that operate on a set of text strings and return a certain string aggregate based on certain...
1
by: nfrodsham | last post by:
In Microsoft's help literature, it states: "You can filter out non-unique rows by using the DISTINCT option of an aggregate function" I am trying to do this in Access 2003 with the COUNT...
1
by: Najib Abi Fadel | last post by:
Hi i have an ordered table of dates let's say: 1/1/2004 8/1/2004 15/1/2004 29/1/2004 5/2/2004 12/2/2004
1
by: R.A.M. | last post by:
Hello, I am learning SQL Server 2005. I have (correctly) written in .NET assembly DemoSQLServer with aggregate function AvgNoMinMax in class Demo and I have added assembly to database...
0
by: BillCo | last post by:
just wasted a long time figuring out this and I figure if I post it might save someone some pain! Jet (DAO) will allow you to to use nested aggregate functions like building blocks, e.g.: ...
5
by: BillCo | last post by:
I just wasted a long time figuring out this and I figure if I post it might save someone some pain! Jet (DAO) will allow you to to use nested aggregate functions like building blocks, e.g.: ...
3
by: Aaron | last post by:
I have been searching the boards trying to find an answer to this question and no luck. I am using a query similar to this: Select count(col1) from table1 I was having a hard time accessing...
3
by: ncsthbell | last post by:
I am pulling my hair out on this! Seems like it should be easy, I just can not get it to work like I want. I know I am doing something wrong, so I hope someone can be so kind to guide me!!! I...
1
by: Sandro997 | last post by:
Ok. I have a bit of a dilemma here. First, please consider the following function: CREATE OR REPLACE FUNCTION recent_lab(text, labs, treatments) RETURNS float8 AS $BODY$select...
3
by: beancounter | last post by:
I am trying to get this query using a derived table and an aggregate function to work in Excel Query: select purordcom.* from (select purOrdDet.PurAcct, sum(PurOrdDet.ExtCost),PurOrdDet.PONbr...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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...
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...
0
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,...

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.