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
29 8875
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. - SELECT Salesperson,
-
((SELECT Count(*) FROM tblSales WHERE NumOfSales > x.NumOfSales) + 1) AS Rank
-
FROM tblSales AS x
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?
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.
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?
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.
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
That doesn't match what I posted...
What you want is more like this - SELECT [VendeurID],
-
((SELECT COUNT(*) FROM [Weekly Machines Sales Data] WHERE [Machines Sold] > x.[Machines Sold])+1) AS Rank
-
FROM [Weekly Machines Sales data] AS x
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
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.
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
If you need a ranking grouped by date, you just need to account for that in the subquery's WHERE clause.
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
I know. Account for it in the subquery's WHERE clause.
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
Put this extra condition in your WHERE clause. - DateField BETWEEN StartDate AND EndDate
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!
I have no idea what you just said.
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
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.
It is Summing Dates fine, however i now have the tie problem again :(
Rqrysales - SELECT Sum([Weekly Machines Sales data].[Machines Sold]) AS [SumOfMachines Sold], [Weekly Machines Sales data].FKVendeurID, [Weekly Machines Sales data].[Date de Vente]
-
FROM [Weekly Machines Sales data]
-
GROUP BY [Weekly Machines Sales data].FKVendeurID, [Weekly Machines Sales data].[Date de Vente]
-
HAVING ((([Weekly Machines Sales data].[Date de Vente]) Between [forms]![RankMachines]![StartDate] And [Forms]![RankMachines]![EndDate]))
-
ORDER BY Sum([Weekly Machines Sales data].[Machines Sold]) DESC;
-
RankingMachines - 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
-
FROM Rqrysales AS SumMach1
-
WHERE (((SumMach1.[Date de Vente]) Between [forms]![RankMachines]![StartDate] And [Forms]![RankMachines]![EndDate]))
-
GROUP BY SumMach1.[SumOfMachines Sold], SumMach1.FKVendeurID;
-
Distinct - SELECT DISTINCT [Weekly Machines Sales data].FKVendeurID
-
FROM [Weekly Machines Sales data]
-
WHERE ((([Weekly Machines Sales data].[Date de Vente]) Between [forms]![RankMachines]![StartDate] And [forms]![RankMachines]![EndDate]))
-
GROUP BY [Weekly Machines Sales data].FKVendeurID;
-
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.
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
You'll need to return just the distinct dollar amounts for the ranking subquery to remove the duplication.
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 - SELECT DISTINCT [Weekly Machines Sales data].FKVendeurID, Sum([Weekly Machines Sales data].[Machines Sold]) AS [SumOfMachines Sold]
-
FROM [Weekly Machines Sales data]
-
WHERE ((([Weekly Machines Sales data].[Date de Vente]) Between [forms]![RankMachines]![StartDate] And [forms]![RankMachines]![EndDate]))
-
GROUP BY [Weekly Machines Sales data].FKVendeurID;
-
RqrySales - SELECT Sum([Weekly Machines Sales data].[Machines Sold]) AS [SumOfMachines Sold], [Weekly Machines Sales data].FKVendeurID, [Weekly Machines Sales data].[Date de Vente]
-
FROM [Weekly Machines Sales data]
-
GROUP BY [Weekly Machines Sales data].FKVendeurID, [Weekly Machines Sales data].[Date de Vente]
-
HAVING ((([Weekly Machines Sales data].[Date de Vente]) Between [forms]![RankMachines]![StartDate] And [Forms]![RankMachines]![EndDate]))
-
ORDER BY Sum([Weekly Machines Sales data].[Machines Sold]) DESC;
-
RankingMachines - 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
-
FROM Rqrysales AS SumMach1
-
WHERE (((SumMach1.[Date de Vente]) Between [forms]![RankMachines]![StartDate] And [Forms]![RankMachines]![EndDate]))
-
GROUP BY SumMach1.[SumOfMachines Sold], SumMach1.FKVendeurID;
-
Wow, your SQL is a mess, I'm having trouble understanding all of it.
Try this:
qrySumOfSales - SELECT [Weekly Machines Sales data].FKVendeurID, Sum([Weekly Machines Sales data].[Machines Sold]) AS [SumOfMachines Sold]
-
FROM [Weekly Machines Sales data]
-
WHERE ((([Weekly Machines Sales data].[Date de Vente]) Between [forms]![RankMachines]![StartDate] And [forms]![RankMachines]![EndDate]))
-
GROUP BY [Weekly Machines Sales data].FKVendeurID;
qryDistinctSales - SELECT DISTINCT qrySumOfSales.[SumOfMachines Sold]
-
FROM qryDistinctSales
qryRankSales - SELECT qrySumOfSales.FKVendeurID, qrySumOfSales.[SumOfMachines Sold],
-
((SELECT Count(*) FROM qryDistinctSales WHERE [SumOfMachines Sold] > x.[SumOfMachines Sold]]) + 1) AS Rank
-
FROM qrySumOfSales AS x
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 - SELECT AliasSum.FKVendeurID, AliasSum.[SumOfMachines Sold],
-
((Select Count(*) FROM qryDistinctSales Where [SumOfMachines Sold]>AliasSum.[SumOfMachines Sold])+1) AS Rank
-
FROM qrySumofSales AS AliasSum;
-
Can't thank you enough for your unbelieveable patience7
Oops, you're right, it was because of the extra bracket and the alias. Good luck with everything else.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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;...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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++...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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.
| |