472,371 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

How to assign a value to a rank - Access 2010

30
Hello, I've been trawling the web trying to find how to build the expression to rank how many machines an individual has sold by date and to assign points for the ranks - so he who sold the most 6points, 2nd 4 points, 3rd 3 points, 4th 2 points 5th 1 point.

This is how my query is set out



Sales are stored in the table [Weekly Machines Sales Data]



the people are Vendeurs



When i have query

Rank: (select [Date], [fkVendeurid], Sum([Machines Sold]) as Sold
from [Weekly Machines Sales data]
Group by [Date], [fkVendeurid])

I'm getting an error

You have written a subquery that can return more than onefield without using the EXISTS reserverd word in the main query's FROM clause. Revise the SELECT statement of the subquery to request only one field

(oo this is giving me a headache!) - thanks so much for the help

I have uploaded a zip with the db file in it here http://cid-4490afdc094900ec.skydrive...DC094900EC!368

I'm aiming towards having a ranking with value assigned for most machines sold and largest margin for each seller and for each team(equipe) for any given date period




Thank you
Jan 11 '11 #1
29 8875
Rabbit
12,516 Expert Mod 8TB
It's just as the error says. You can only return one field if you're going to use a subquery as a field. Also, when using subqueries as field, you must return only one row. Since you're doing a ranking field, I would think you would use a count and tie the subquery back to the outer query. But you don't seem to be doing this.

Expand|Select|Wrap|Line Numbers
  1. SELECT Salesperson,
  2.      ((SELECT Count(*) FROM tblSales WHERE NumOfSales > x.NumOfSales) + 1) AS Rank
  3. FROM tblSales AS x
Jan 11 '11 #2
Lisa B
30
thanks for the quick reply - the sales are not totalled by seller in the weekly sales table, that is done in the query - also i see no reference to date in your sugested code - are you able to use the field names as per the images i provided?
Jan 11 '11 #3
Rabbit
12,516 Expert Mod 8TB
My code was just an example of how you would set up a ranking field. It has the basics of what is needed, you would have to modify it to your needs though.

I am unable to see the images. I think it is because of the browser I use at work.
Jan 11 '11 #4
Lisa B
30
Does your code work for a query as it looks like it's based on table data - are you able to view the pictures at a later time?
Jan 11 '11 #5
Rabbit
12,516 Expert Mod 8TB
I may be able to view the pictures later but it would have to be much later.

Why don't you try adapting my query to fit your needs? My query was not based on your tables but fake tables I created.
Jan 11 '11 #6
Lisa B
30
I am unsure as to what the last x is after the final AS

so far i have written this

Rank: (Select [VendeurID], ((Select count(*) From [Weekly Machines Sales Data]
WHERE [Machines Sold] > [Weekly Machines Sales data].[Machines Sold])+1)
AS Rank
FROM [Weekly Machines Sales data])
AS
Jan 15 '11 #7
Lisa B
30
I have uploaded a copy of my dbase to skydrive http://cid-4490afdc094900ec.skydrive...DC094900EC!368
Jan 15 '11 #8
Rabbit
12,516 Expert Mod 8TB
That doesn't match what I posted...

What you want is more like this
Expand|Select|Wrap|Line Numbers
  1. SELECT [VendeurID],
  2.      ((SELECT COUNT(*) FROM [Weekly Machines Sales Data] WHERE [Machines Sold] > x.[Machines Sold])+1) AS Rank
  3. FROM [Weekly Machines Sales data] AS x
Jan 17 '11 #9
Lisa B
30
I am unsure as to what the last x should be

all working perfectly accept i can't seem to sum the machines sold per vendeur per date

So i tried to adapt it

I have created two queries

Rqrysales

This sums the number of mahcines sold by each vendeur

I then use this query to create a new query called RankingMachines based on the Rqrysales query - created an alias of SumMach1 for the Rqrysales field list

To handle ties i created another query called Distinct where i chose the same two fields, summed the [Machines Sold] field, hid the [FKVendeurID] field and changed the properties of the query so that Unique Records were YES

In my RankingMachines Query i used the expression

Rank: (Select count(*) from [Distinct] Where [SumOfMachines Sold] > [SumMach1].[SumOfMachines Sold])+1

The only thing it's not doing is summing for the date

and it would seem now, not starting the ranking at 1
Jan 18 '11 #10
Rabbit
12,516 Expert Mod 8TB
The x is an alias. It is needed for the subquery to be able to reference the outer query.

You can't do an aggregate query and ranking query together. You can rank the aggregate results after you aggregate them. But you can't rank the pre-aggregate and then sum them without losing rank.
Jan 18 '11 #11
Lisa B
30
I have managed to work out the problem with the summing for the dates, but the ranking is not starting at 1 if there is a higher sales value before that date
Jan 18 '11 #12
Rabbit
12,516 Expert Mod 8TB
If you need a ranking grouped by date, you just need to account for that in the subquery's WHERE clause.
Jan 18 '11 #13
Lisa B
30
The grouping by date is fine

BUT the ranking is not starting at 1 if there is a sale of higher value earlier than the date chosen

i.e.

Sale Date 12/1/2011 - Value 12
Sale Date 14/1/2011 - Value 7
Sale Date 15/1/2011 - Value 4
Salte Date 16/1/2011 - Value 6

Ranking for date Range 13/1/2011 - 16/1/2011

Sale Date 14/1/2011 - Rank 2
Sale Date 15/1/2011 - Rank 4
Salte Date 16/1/2011 - Rank 3


If you select
Jan 18 '11 #14
Rabbit
12,516 Expert Mod 8TB
I know. Account for it in the subquery's WHERE clause.
Jan 18 '11 #15
Lisa B
30
HOW?!

RqrySales

Weekly Machines Sales Data (table)
[Date de Vente]
[Machines Sold] Summed and Descending
[FKVendeurID]

Distinct - Properties set to unique value
Weekly Machines Sales Data (table)

[Machines Sold] Summed Descending
[Date de Vente] - not shown
[FKVendeurID] - not shown

RankingMachines Qry -
using Rqrysales - aliased new name SumMach1

[Date de Vente] - Criteria Between [Forms]![RankMachines]![StartDate] And [Forms]![RankMachines]![EndDate]

[SumofMachines Sold]
Rank:Rank: (Select count(*) from [Distinct]
Where [SumOfMachines Sold] > [SumMach1].[SumOfMachines Sold])+1
Jan 18 '11 #16
Rabbit
12,516 Expert Mod 8TB
Put this extra condition in your WHERE clause.
Expand|Select|Wrap|Line Numbers
  1. DateField BETWEEN StartDate AND EndDate
Jan 18 '11 #17
Lisa B
30
have added another duplicate field in the query that has the ranking (as when you select where from the total row it un-shows it)

[Date de Vente] selected where for the total row and put the expression Between [Forms]![RankMachines]![StartDate] And [Forms]![RankMachines]![EndDate]


for a query of same startdate and endate i'm getting one result which is fine but rank 4!
Jan 18 '11 #18
Lisa B
30
mispost can' delete
Jan 18 '11 #19
Rabbit
12,516 Expert Mod 8TB
I have no idea what you just said.
Jan 18 '11 #20
Lisa B
30
when i put your WHERE clause in the select statment for the rnaking

Rank:Rank: (Select count(*) from [Distinct]
Where [SumOfMachines Sold] > [SumMach1].[SumOfMachines Sold])+1


it doesn't work, so i put it in the criterira for the Date field in the query
Jan 18 '11 #21
Rabbit
12,516 Expert Mod 8TB
It didn't work in the subquery most likely because your [Distinct] query doesn't return the date field as one of it's columns.
Jan 18 '11 #22
Lisa B
30
It is Summing Dates fine, however i now have the tie problem again :(

Rqrysales
Expand|Select|Wrap|Line Numbers
  1. SELECT Sum([Weekly Machines Sales data].[Machines Sold]) AS [SumOfMachines Sold], [Weekly Machines Sales data].FKVendeurID, [Weekly Machines Sales data].[Date de Vente]
  2. FROM [Weekly Machines Sales data]
  3. GROUP BY [Weekly Machines Sales data].FKVendeurID, [Weekly Machines Sales data].[Date de Vente]
  4. HAVING ((([Weekly Machines Sales data].[Date de Vente]) Between [forms]![RankMachines]![StartDate] And [Forms]![RankMachines]![EndDate]))
  5. ORDER BY Sum([Weekly Machines Sales data].[Machines Sold]) DESC;
  6.  
RankingMachines

Expand|Select|Wrap|Line Numbers
  1. SELECT SumMach1.[SumOfMachines Sold], (Select Count(*) from [Distinct] Where [SumOfMachines Sold] > SumMach1.[sumofmachines sold])+1 AS Rank, IIf([rank]=1,6,IIf([rank]=2,4,IIf([rank]=3,3,IIf([rank]=4,2,IIf([rank]=5,1,0))))) AS Points, SumMach1.FKVendeurID
  2. FROM Rqrysales AS SumMach1
  3. WHERE (((SumMach1.[Date de Vente]) Between [forms]![RankMachines]![StartDate] And [Forms]![RankMachines]![EndDate]))
  4. GROUP BY SumMach1.[SumOfMachines Sold], SumMach1.FKVendeurID;
  5.  
Distinct
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT [Weekly Machines Sales data].FKVendeurID
  2. FROM [Weekly Machines Sales data]
  3. WHERE ((([Weekly Machines Sales data].[Date de Vente]) Between [forms]![RankMachines]![StartDate] And [forms]![RankMachines]![EndDate]))
  4. GROUP BY [Weekly Machines Sales data].FKVendeurID;
  5.  
Jan 19 '11 #23
Rabbit
12,516 Expert Mod 8TB
What tie problem? You never mentioned a tie problem before this. If you're talking about records having the same rank because they have the same sum, then they should have the same rank. If you want to somehow force a different rank even though they're the same, you will have to resort to VBA coding.
Jan 19 '11 #24
Lisa B
30
SOrry thought I had mentioned it

Tie problem is when it is not adding the next rank number for tie, i.e

1
2
2
4
5
5
7

rather than
1
2
2
3
4
5
5
6
7
Jan 19 '11 #25
Rabbit
12,516 Expert Mod 8TB
You'll need to return just the distinct dollar amounts for the ranking subquery to remove the duplication.
Jan 19 '11 #26
Lisa B
30
I need to Sum the number of machines sold per Vendeur for a date range - and rank who has sold the most within that date range

Currently Vendeurs now appearing more than once and not being ranked correctly


http://i305.photobucket.com/albums/n...machineres.png
DO i need two distinct queries?

Distinct
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT [Weekly Machines Sales data].FKVendeurID, Sum([Weekly Machines Sales data].[Machines Sold]) AS [SumOfMachines Sold]
  2. FROM [Weekly Machines Sales data]
  3. WHERE ((([Weekly Machines Sales data].[Date de Vente]) Between [forms]![RankMachines]![StartDate] And [forms]![RankMachines]![EndDate]))
  4. GROUP BY [Weekly Machines Sales data].FKVendeurID;
  5.  
RqrySales
Expand|Select|Wrap|Line Numbers
  1. SELECT Sum([Weekly Machines Sales data].[Machines Sold]) AS [SumOfMachines Sold], [Weekly Machines Sales data].FKVendeurID, [Weekly Machines Sales data].[Date de Vente]
  2. FROM [Weekly Machines Sales data]
  3. GROUP BY [Weekly Machines Sales data].FKVendeurID, [Weekly Machines Sales data].[Date de Vente]
  4. HAVING ((([Weekly Machines Sales data].[Date de Vente]) Between [forms]![RankMachines]![StartDate] And [Forms]![RankMachines]![EndDate]))
  5. ORDER BY Sum([Weekly Machines Sales data].[Machines Sold]) DESC;
  6.  
RankingMachines
Expand|Select|Wrap|Line Numbers
  1. SELECT SumMach1.[SumOfMachines Sold], (Select Count(*) from [Distinct] Where [SumOfMachines Sold] > SumMach1.[sumofmachines sold])+1 AS Rank, IIf([rank]=1,6,IIf([rank]=2,4,IIf([rank]=3,3,IIf([rank]=4,2,IIf([rank]=5,1,0))))) AS Points, SumMach1.FKVendeurID
  2. FROM Rqrysales AS SumMach1
  3. WHERE (((SumMach1.[Date de Vente]) Between [forms]![RankMachines]![StartDate] And [Forms]![RankMachines]![EndDate]))
  4. GROUP BY SumMach1.[SumOfMachines Sold], SumMach1.FKVendeurID;
  5.  
Jan 19 '11 #27
Rabbit
12,516 Expert Mod 8TB
Wow, your SQL is a mess, I'm having trouble understanding all of it.

Try this:

qrySumOfSales
Expand|Select|Wrap|Line Numbers
  1. SELECT [Weekly Machines Sales data].FKVendeurID, Sum([Weekly Machines Sales data].[Machines Sold]) AS [SumOfMachines Sold]
  2. FROM [Weekly Machines Sales data]
  3. WHERE ((([Weekly Machines Sales data].[Date de Vente]) Between [forms]![RankMachines]![StartDate] And [forms]![RankMachines]![EndDate]))
  4. GROUP BY [Weekly Machines Sales data].FKVendeurID;
qryDistinctSales
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT qrySumOfSales.[SumOfMachines Sold]
  2. FROM qryDistinctSales
qryRankSales
Expand|Select|Wrap|Line Numbers
  1. SELECT qrySumOfSales.FKVendeurID, qrySumOfSales.[SumOfMachines Sold],
  2. ((SELECT Count(*) FROM qryDistinctSales WHERE [SumOfMachines Sold] > x.[SumOfMachines Sold]]) + 1) AS Rank
  3. FROM qrySumOfSales AS x
Jan 20 '11 #28
Lisa B
30
It didn't like the qryRankSales, I think because I had to alias the qrySumOfSales table (and a spurious bracket(

this is what i've ended up with - which HOORAY i 'think' is working
Expand|Select|Wrap|Line Numbers
  1. SELECT AliasSum.FKVendeurID, AliasSum.[SumOfMachines Sold], 
  2. ((Select Count(*) FROM qryDistinctSales Where [SumOfMachines Sold]>AliasSum.[SumOfMachines Sold])+1) AS Rank
  3. FROM qrySumofSales AS AliasSum;
  4.  
Can't thank you enough for your unbelieveable patience7
Jan 20 '11 #29
Rabbit
12,516 Expert Mod 8TB
Oops, you're right, it was because of the extra bracket and the alias. Good luck with everything else.
Jan 20 '11 #30

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

Similar topics

8
by: Sergei | last post by:
Hi, I am displaying modal dialog and passing values from the main form to the modal dialog and back. It works fine but if I used the following syntax on Page_Load(just for testing) in VB to...
0
by: JJ_377 | last post by:
The following doesn't assign value to the dropdownlist - WHY? ___________________________________________________________________ In a user control (ascx named USACustomer) : Public Property...
0
by: gladiator | last post by:
Hello Here I wrote an example by which an const variable can be assigned witha value but I can not understand how can it assign value. #include<stdio.h> int main(void) { const int i=10;...
1
by: harpreet1433 | last post by:
how to assign value to a hidden field of stuts form what i actually want to do that assign it through javascript document.all.fieldname=value works well under IE and Netscape but in...
1
by: No_Spam | last post by:
I have an MS Access form attached to a table. I have a text box which has its control source set to a field in the table... However, at run time if certain things change, I want to the change the...
7
vikas251074
by: vikas251074 | last post by:
Can I assign value to cookies immediately after selecting a value from list? <select name="vlan_name" style="width:150px "> <%set rs = conn.execute("select vlan_name from vlan_master order by...
1
by: phill86 | last post by:
Hi, I have an access 2010 database that I want to convert to an SQL Server database and I need to know if the data macros in access will still work in the SQL database or will i have to create...
5
by: jaad | last post by:
Hello, I have a database that was written in access 2007 on my pc. I wanted to work off site with the database so I uploaded it onto my laptop which is loaded with access 2010 beta. When I...
2
by: sierra7 | last post by:
It seems Access 2010 is associating an 'input mask' or field type with a combo box when a form is opened, even though there is no Format setting on the control. I have a form which has been...
2
by: dougancil | last post by:
I have a user who had deleted some records from a database today using Access 2010. They have an ID field that's autonumbered. They have No Duplicates allowed. When they created a new record today,...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

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.