Need to select top 5 rows

I want to set up a select to get the sum of a value but I only want to sum up 
the first 5 rows.

Example:

select sum(x)
from tableA
where **ROWCOUNT <= 5
order by y

Is this possible in VBA/Access?? Example? Thanks
0
Utf
1/23/2008 11:22:01 PM
access 16762 articles. 3 followers. Follow

7 Replies
670 Views

Similar Articles

[PageSpeed] 47

Try this ---
select TOP 5 sum(x)
from tableA
order by y;

-- 
KARL DEWEY
Build a little - Test a little


"AJ" wrote:

> I want to set up a select to get the sum of a value but I only want to sum up 
> the first 5 rows.
> 
> Example:
> 
> select sum(x)
> from tableA
> where **ROWCOUNT <= 5
> order by y
> 
> Is this possible in VBA/Access?? Example? Thanks
0
Utf
1/23/2008 11:28:00 PM
Thanks Karl - If I do that I get the error "you tried to execute a query that 
does not include the specified expression 'Y' as part of an aggregate 
function. Any ideas?

"KARL DEWEY" wrote:

> Try this ---
> select TOP 5 sum(x)
> from tableA
> order by y;
> 
> -- 
> KARL DEWEY
> Build a little - Test a little
> 
> 
> "AJ" wrote:
> 
> > I want to set up a select to get the sum of a value but I only want to sum up 
> > the first 5 rows.
> > 
> > Example:
> > 
> > select sum(x)
> > from tableA
> > where **ROWCOUNT <= 5
> > order by y
> > 
> > Is this possible in VBA/Access?? Example? Thanks
0
Utf
1/23/2008 11:53:10 PM
Try this ---
select TOP 5 sum(x), y
from tableA
order by y;

-- 
KARL DEWEY
Build a little - Test a little


"AJ" wrote:

> Thanks Karl - If I do that I get the error "you tried to execute a query that 
> does not include the specified expression 'Y' as part of an aggregate 
> function. Any ideas?
> 
> "KARL DEWEY" wrote:
> 
> > Try this ---
> > select TOP 5 sum(x)
> > from tableA
> > order by y;
> > 
> > -- 
> > KARL DEWEY
> > Build a little - Test a little
> > 
> > 
> > "AJ" wrote:
> > 
> > > I want to set up a select to get the sum of a value but I only want to sum up 
> > > the first 5 rows.
> > > 
> > > Example:
> > > 
> > > select sum(x)
> > > from tableA
> > > where **ROWCOUNT <= 5
> > > order by y
> > > 
> > > Is this possible in VBA/Access?? Example? Thanks
0
Utf
1/24/2008 12:26:07 AM
Damn....no same error.

"KARL DEWEY" wrote:

> Try this ---
> select TOP 5 sum(x), y
> from tableA
> order by y;
> 
> -- 
> KARL DEWEY
> Build a little - Test a little
> 
> 
> "AJ" wrote:
> 
> > Thanks Karl - If I do that I get the error "you tried to execute a query that 
> > does not include the specified expression 'Y' as part of an aggregate 
> > function. Any ideas?
> > 
> > "KARL DEWEY" wrote:
> > 
> > > Try this ---
> > > select TOP 5 sum(x)
> > > from tableA
> > > order by y;
> > > 
> > > -- 
> > > KARL DEWEY
> > > Build a little - Test a little
> > > 
> > > 
> > > "AJ" wrote:
> > > 
> > > > I want to set up a select to get the sum of a value but I only want to sum up 
> > > > the first 5 rows.
> > > > 
> > > > Example:
> > > > 
> > > > select sum(x)
> > > > from tableA
> > > > where **ROWCOUNT <= 5
> > > > order by y
> > > > 
> > > > Is this possible in VBA/Access?? Example? Thanks
0
Utf
1/24/2008 12:40:00 AM
"AJ" <AJ@discussions.microsoft.com> wrote in message
news:3EE145C4-2A1C-4DD9-A113-007FE8EB3D3F@microsoft.com...
> Damn....no same error.

Sum(x) without any GROUP BY will yield only one record.
Which Y do you expect to see with that total?

GROUP BY y.
Or perhaps try First(y)

(It also helps to explicitly alias aggregated fields.)

-- 
Allen Browne - Microsoft MVP.  Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

0
Allen
1/24/2008 1:52:15 AM
Hello Allen: I understand that sum will only return one record and I want to 
only sum the first five rows, based on column Y.
Can this be done?

"Allen Browne" wrote:

> "AJ" <AJ@discussions.microsoft.com> wrote in message
> news:3EE145C4-2A1C-4DD9-A113-007FE8EB3D3F@microsoft.com...
> > Damn....no same error.
> 
> Sum(x) without any GROUP BY will yield only one record.
> Which Y do you expect to see with that total?
> 
> GROUP BY y.
> Or perhaps try First(y)
> 
> (It also helps to explicitly alias aggregated fields.)
> 
> -- 
> Allen Browne - Microsoft MVP.  Perth, Western Australia
> Tips for Access users - http://allenbrowne.com/tips.html
> Reply to group, rather than allenbrowne at mvps dot org.
> 
> 
0
Utf
1/24/2008 2:04:02 AM
Perhaps something like this:

SELECT Sum(x) AS SumOfX
FROM Table1
WHERE Table1.ID IN
  (SELECT TOP 5 ID
  FROM Table1 AS Dupe
  ORDER BY Dupe.y, Dupe.ID);

If subqueries are new, see:
    http://allenbrowne.com/subquery-01.html

-- 
Allen Browne - Microsoft MVP.  Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"AJ" <AJ@discussions.microsoft.com> wrote in message
news:378EDF24-84F8-4507-974C-32BC27843FF9@microsoft.com...
> Hello Allen: I understand that sum will only return one record and I want 
> to
> only sum the first five rows, based on column Y.
> Can this be done? 

0
Allen
1/24/2008 2:24:49 AM
Reply:

Similar Artilces:

Not sure if Excel can do what i need
Hello all, I am obviously a new user to excel. I have a psychological assessmen that needs to be computerised for ease of use in obtaining results. Here is what i would like to happen. I have 28 questions with fiv Likert scale options(0-4) for answers. Each question addresses 1 of different personality traits. So questions 1, 6, 8, 22 look at anger. Where as questions 2, 9, 28 look at happiness. What i would like is fo excel to group the answers and give me the total number for each trait. Then based on that number, give out a prewritten (which i would provide result of the assessment. ...

Match value in one column and return value in same row in next col
Hi, I have a a set of values in A1 through A100. I need to look up each value and find a match in another set of values located in C1 through C200. If a match is found then I need the formula located in column B to return the value in the same row but the next column over (D). Example: A B C D 359 375 10012 456 125 10031 125 10031 952 10052 854 444 10017 The formula located in B3 should find that the value of 125 (located in A3) matches the value in C2 and returns 10031 which is the v...

Need Min Value from Row and Heading Value that Corresponds to it.
I have a table that looks somewhat like this w/o the *'d headings: h1 h2 h3 h4 h5 h6 h7 *Min Price *Heading $5.00 $6.00 $3.00 $4.00 $9.00 $1.00 $9.00 $1.00 h6 $3.00 $6.00 $8.00 $65.00 $9.00 $2.00 $4.00 $2.00 h6 $4.00 $9.00 $5.00 $7.00 $1.00 $2.00 $8.00 $1.00 h5 $9.00 $7.00 $6.00 $3.00 $9.00 $8.00 $2.00 $2.00 h7 $7.00 $8.00 $9.00 $7.00 $9.00 $8.00 $8.00 $7.00 h1 I have several SKU's with various price contract/programs. I want to find the best price in one cell and the heading that would correspond to that cell in another. I was trying to do this with an expression ...

Replying without top posting
When I reply to a message that is in RTF format I get a blue bar along the left edge of the original text (I think it's RTF messages that do this, maybe it's HTML). This is because I want to prefix the original text. However, when I try to reply to sections of the original text, the blue bar doesn't disappear next to my writing like it did with 2003. This causes my replies to look just like original text. I've tried all sorts of settings and can't stop this behavior. Does anyone know how I can reply, in a bottom-posting fashion, without having my comments prefixe...

Excel need help
I have 2 columns a and b . a contains payment b contains balance. I have the entire col b with the formula. my ? is when the balance appearsin b it is in all the cells in that column.I only want it in the used ones, not the unused ones:( example $270.00 16,730.00 $270.00 16,460.00 $100.00 16,360.00 $300.00 16,060.00 $270.00 15,790.00 15,790.00 15,790.00 15,790.00 15,790.00 15,790.00 this 15790.00 is in col b...not a...I want no bal in unused cells -- fiftieslady ------------------------------------------------------------------------ fiftieslady's Profile: http://www.excelforum.co...

Publisher Question #5
How can you select the toolbars that appear on your workspace Isbell wrote: > How can you select the toolbars > that appear on your workspace ============================ Not sure what you mean by "select". If you're asking how to toggle them on or off...just right click and check or uncheck the one you're interested in. Also you can access the settings at... View / Toolbars. -- John Inzer MS Picture It! - Digital Image MVP Digital Image Highlights and FAQs http://tinyurl.com/aczzp Notice This is not tech support I am a voluntee...

Excel newbie needs help from math wiz (poker related)
I want to create a Excel workbook that will help in specific poker situations. Short version: I want to be able to calculate how often my opponent will need to fold for an all-in raise to show a positive expectation. Explanation: In poker you can win a hand by having the best hand at the end OR getting your opponent to fold. A common situation comes up where you have a "drawing hand" and face a bet by your opponent. In this situation you know you are currently behind in the hand, but have a chance to win if you catch some of your "outs" (cards that will give you th...

Need Help Linking Sheets
How can I link 2 pages without using the actual sheet as a reference? In other words, can I have two sheets linked by Identifying the same exact names one on each sheet? ...

Macro help needed........
How do I sort a particular column in ascending order and change the pag setup to landscape and margins to 0.25 using a macro code ? does anyone have a macro code for this ????? -- Message posted from http://www.ExcelForum.com Hi you have already received some answers in your previous threads please don't multipost! -- Regards Frank Kabel Frankfurt, Germany > How do I sort a particular column in ascending order and change the > page setup to landscape and margins to 0.25 using a macro code ? > > does anyone have a macro code for this ?????? > > > --- > Me...

Need to test a mobo
I had a power surge about 3 weeks ago - lost a drive, now making ticking noises. I had another two drive that seem to work sporadically now. So, I thought it might have been my power supply. Bought a tester and found out that all seems well. Next, I bought a new hard drive and found that it was also acting up - making noises like whirring up and down (like the other drives I thought were dead). So I am starting to wonder if it is my mobo - but how can I test this out? I am using a dual boot - Win XP and Win 7 - both drives work and don't work. Up and down. Right now my Win7 ...

Help needed
is there anyway you can see what links to other workbooks are associated with the workbook you have open. The problem i am having is when i open the workbook in question a message appears and states "The workbook you opened contains automatic links to information in another workbook. do you want to update this workbook with changes made to the other workbook" Yes....... No. if i click No the workbook opens, however if i click Yes the workbook just hangs and i have to go into task manager to shut down the system. I can only assume that there is a problem with one of the links....

Need help on this....thanks
I have an Excel worksheet which stored a survey outcome of over 1200 members. There are over 30 questions in the survey and the first column captured the member ID. The problem is 3 of the survey questions that allow multiple answers, instead the answer for those three questions are captured in seperate column, the answers are stored in a single column and it makes it very difficult to analysis those result. As a result I created addittional columns depending on the total number of the answers available for that question and assign the result manually to that column. It is very time consuming ...

select a specific ActiveCell by VBA
In any cell in column "F", a user can select a code number from a drop down list. These numbers go up to over 100, so, if the user isn't sure of which number to select, cell F6 can be rightclicked and VBA will take the user to a different sheet where all the code numbers (and their associated meanings) are listed. A command button on that sheet will take the user back to the ActiveCell on the first sheet. Even if the user was working in cell F2000, the active cell is now always F6 To get around this, how can I set the active row to the bottom most row with data i...

need help badly
How do you print ole objects.. Thanks lachanda -- Message posted via AccessMonster.com http://www.accessmonster.com/Uwe/Forums.aspx/access-forms/200710/1 On Wed, 03 Oct 2007 18:13:40 GMT, "misschanda via AccessMonster.com" <u36612@uwe> wrote: >How do you print ole objects.. >Thanks >lachanda What *kind* of OLE objects? Many of them are binary blobs (such as programs) which are inherently not printable. More details please! John W. Vinson [MVP] ...

I need Help
I have a random popup that i cant stop,when it does come up it seems to know what i was looking at,like when i was looking for registry cleaner it popped up and showed me one,any help would be great. On Jan 22, 6:57=A0am, Larry <La...@discussions.microsoft.com> wrote: > I have a random popup that i cant stop,when it does come up it seems to k= now > what i was looking at,like when i was looking for registry cleaner it pop= ped > up and showed me one,any help would be great. To eliminate questions and guessing, please provide additional information about your sys...

Missing Text #5
Hello, I have a problem with Outlook XP (2002) that I can't find a solution for. When this particular user receives a reply to an email she sent, the frist couple of lines of the reply text will appear in the auto preview (the preview in the message listing in the inbox) but when she opens the message itself by double clicking it, that reply text is missing. I've updated Outlook to SP3 and even tried reinstalling it once. Does anyone know of a fix for this? Thanks, Rod ...

Stuck in review and need to be in design.
I am using Visio 2007. I need to complete the diagram that I have been working on and I hit the review key by mistake. How do I get back to the design screen? On Wed, 4 Nov 2009 16:14:01 -0800, cameron <cameron@discussions.microsoft.com> wrote: >I am using Visio 2007. I need to complete the diagram that I have been >working on and I hit the review key by mistake. How do I get back to the >design screen? menu Tools -> Track Markup and then close the Review window. -- Regards, Paul Herber, Sandrila Ltd. DFD/SSADM for Visio http://www.visio-dfd.sa...

Need Help Despertly!
I am in a desperate situation. I had to reformat my hard drive. Prior to do this, I copied my entire User Profile to another drive. I can now access my User Profile information from Windows Explorer. Now that I installed Microsoft Outlook, I do not see any of my Contacts. I need to restore my Contacts to the new Outlook installation. Again, I did save my entire "User Profile" from my previous installation of Windows 2000 Professional. I just do not know how to access my Contact information from Outlook that included addresses and phone numbers and such. Any help would be greatly appr...

A9 (1/2 page) top fold card
Hi. I recently upgraded from 2003 to 2007. I was pleased to see it now includes half-page top fold cards. It seems to work for smaller cards but not for half page cards. It keeps putting pages 2 and 3 next to each other (a la side-fold) rather than above and below. I'd rather not go back to rotating the image myself. Am I doing something wrong or id this a bug? Thanks -- Denise - Somerset Hills Handicapped Riding Center I am having the identical problem today. I really need a top fold! Any responses yet? "Denise" wrote: > Hi. I recently upgraded from 2003 t...

Plug in needed
Looking for an Outlook plug in which, on reply to a e-mail, will:- a) allow selective quoting only, and b) not top quote. Anything around at all? Much to my horror (sorry guys) the Boss wants us to use Outofluck at work! -- On the carpool lane lane to Nirvana. John Phillips <flatulentdingo@deadspam.com> wrote: > Looking for an Outlook plug in which, on reply to a e-mail, will:- > > a) allow selective quoting only, and > > b) not top quote. > > Anything around at all? The best Outlook adjunct I've found for quote handling on replies is Outlook-Quotef...

What is the Need for XSD in Application Blocks designing
Can anyone help in clearing my doubt Hi, There is a need for me to implement Application Blocks in my project, (Basically 3 tire), the project comprises of UIL, BLL,Business Entities, DAL.. When i saw the example programs from User Interface Application Block example, which is implementing the Business Layer part, Interface for BLL and Business Entity part where by i am not able to understand the real usage of XSD's and the autogenerated C#? Can anyone explain me what is the process flow behind this fuda in a simple terms. Thanks in Advance Jagadeesh ...

Need to Hire Outlook 2003 Help
Hi I'd like to hire someone that can walk me through some specific stuff with outlook 2003 I'm using the Lotus Notes to Outlook plugin My compnay is a Notes shop - which means I get NO support for outlook --- this plugin is a god send and i want to make sure I get it set up correctly It's coming out of my pocket but I'm willing to pay if I can find the right person to help me Please post ideas here or email me Thanks Brendan ...

Selecting which Inbox to show at start-up
I am trying to sync my mail with my palm, and it will only sync the general inbox, but my mail goes to the inbox of my mail account at my school. How can I either get that mail moved to the general inbox at start-up, or just switch that to be the main inbox? If i could get outlook to check this other account at start-up it might be fine, but i have to click on it to get it to check the school mail, even though that is the only mail account i have set up What type of account and version of Outlook are we talking about here? -- Robert Sparnaaij [MVP-Outlook] www.howto-outlook.com Tips...

reflecting values in a column into a row
I am creating a chart to map a round-robin chess game. If there are 4 players, then all 4 has to play one another. if I have the names John Mike Sally Bill Then I'd like to type them into a columns and write a formula in a row to pick up the names the spreadsheet should then look like this: John Mike Sally Bill John Mike Sally Bill I think it may be achieved with the Indirect() function, but my Excel 2007 help seems broken and I can't figure it out without an example. Thanks. MikeB With names in A2:A5 Enter in B1 =INDIRECT("A"&COLUMN(B1)) Or...

Which setting do I need to adjust
I've noticed that when using my default browser IE 8 and I click on a link that is suppose to start a download, it doesn't. When I click on the same link in firefox the download starts like it should. Can anyone tell me what setting in IE 8 is preventing the download from starting? on the page it says "Downloading: 50fpct-Just4freeplanet.com.rar | 34.7 MB" but in IE 8 it isn't downloading. It also isn't asking me if I want to allow IE 8 to download the file like it sometimes does. Thanks Shep wrote: > I've noticed that when using my d...