Solved

Aggregated Function from Access (First)

Posted on 2000-03-23
3
136 Views
Last Modified: 2010-05-02
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,
First(dtSales.dtTradeChannel) AS dtTradeChannel
FROM tblCustomers INNER JOIN dtSales
0
Comment
Question by:Rab
3 Comments
 
LVL 28

Expert Comment

by:AzraSound
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

by:
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

by:Rab
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'
GROUP BY dtYear,dtMonth,dtRepCode,dtTradeChannel
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now