Solved

# Aggregated Function from Access (First)

Posted on 2000-03-23
136 Views
Hi

I need to know what the same function in SQL server 7 is for (First) the Aggregated function for Access.

e.g.

SELECT dtYear,dtMonth,
Sum(dtSales.dtBulkValue) AS dtBulkValue,
Sum(dtSales.dtBulkVolume) AS dtBulkVolume,
First(dtSales.dtRepCode) AS dtRepCode,
FROM tblCustomers INNER JOIN dtSales
0
Question by:Rab

LVL 28

Expert Comment

ID: 2652256
you may be able to do it with something like this:
SELECT TOP 1 dtYear,dtMonth,Sum(dtSales.dtBulkValue) AS dtBulkValue,
Sum(dtSales.dtBulkVolume) AS dtBulkVolume
FROM tblCustomers
INNER JOIN dtSales
ORDER BY (whichever field you want the top value for)

0

LVL 142

Accepted Solution

Guy Hengel [angelIII / a3] earned 20 total points
ID: 2652367
azrasound: using TOP 1 like this will produce 0 or 1 rows at all, you need to modify (see below)
Rab: you need to use GROUP BY:

SELECT S.dtYear,S.dtMonth,
Sum(S.dtBulkValue) AS dtBulkValue,
Sum(S.dtBulkVolume) AS dtBulkVolume,
( SELECT TOP 1 SI.dtRepCode FROM dtSales SI WHERE SI.dtYear = S.dtYear AND SI.dtMonth = S.dtMonth) AS dtRepCode,
( SELECT TOP 1 dtTradeChannel FROM dtSales SI WHERE SI.dtYear = S.dtYear AND SI.dtMonth = S.dtMonth) AS dtTradeChannel
FROM tblCustomers C INNER JOIN dtSales S
GROUP BY S.dtYear, S.dtMonth

0

Author Comment

ID: 2652906
Hi angelIII
If I use the query as above the SQL query returns two more records that the
Access query. Why would this be.

query e.g.

SELECT dtYear,dtMonth,
Sum(dtSales.dtSingleValue) AS dtSingleValue,
Sum(dtSales.dtSingleVolume) AS dtSingleVolume,
Sum(dtSales.dtMultiValue) AS dtMultiValue,
Sum(dtSales.dtMultiVolume) AS dtMultiVolume,
Sum(dtSales.dtBulkValue) AS dtBulkValue,
Sum(dtSales.dtBulkVolume) AS dtBulkVolume,
( SELECT TOP 1 dtSales.dtRepCode FROM dtSales SI WHERE SI.dtYear = dtSales.dtYear AND dtSales.dtMonth = dtSales.dtMonth) AS dtRepCode,
( SELECT TOP 1 dtTradeChannel FROM dtSales SI WHERE SI.dtYear = dtSales.dtYear AND dtSales.dtMonth = dtSales.dtMonth) AS dtTradeChannel

FROM tblCustomers  INNER JOIN dtSales
ON tblCustomers.CustomerID=dtSales.dtCustomerID
WHERE CNumber = '1000022120' Or CNumber = '1000022130'
0

## Featured Post

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

#### Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!