How to assign a value to a rank - Access 2010

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Lisa B
    New Member
    • Jan 2011
    • 30

    #16
    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

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #17
      Put this extra condition in your WHERE clause.
      Code:
      DateField BETWEEN StartDate AND EndDate

      Comment

      • Lisa B
        New Member
        • Jan 2011
        • 30

        #18
        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!

        Comment

        • Lisa B
          New Member
          • Jan 2011
          • 30

          #19
          mispost can' delete
          Last edited by Lisa B; Jan 18 '11, 08:37 PM. Reason: not valid

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #20
            I have no idea what you just said.

            Comment

            • Lisa B
              New Member
              • Jan 2011
              • 30

              #21
              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

              Comment

              • Rabbit
                Recognized Expert MVP
                • Jan 2007
                • 12517

                #22
                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.

                Comment

                • Lisa B
                  New Member
                  • Jan 2011
                  • 30

                  #23
                  It is Summing Dates fine, however i now have the tie problem again :(

                  Rqrysales
                  Code:
                  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

                  Code:
                  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
                  Code:
                  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;

                  Comment

                  • Rabbit
                    Recognized Expert MVP
                    • Jan 2007
                    • 12517

                    #24
                    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.

                    Comment

                    • Lisa B
                      New Member
                      • Jan 2011
                      • 30

                      #25
                      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

                      Comment

                      • Rabbit
                        Recognized Expert MVP
                        • Jan 2007
                        • 12517

                        #26
                        You'll need to return just the distinct dollar amounts for the ranking subquery to remove the duplication.

                        Comment

                        • Lisa B
                          New Member
                          • Jan 2011
                          • 30

                          #27
                          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


                          DO i need two distinct queries?

                          Distinct
                          Code:
                          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
                          Code:
                          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
                          Code:
                          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;

                          Comment

                          • Rabbit
                            Recognized Expert MVP
                            • Jan 2007
                            • 12517

                            #28
                            Wow, your SQL is a mess, I'm having trouble understanding all of it.

                            Try this:

                            qrySumOfSales
                            Code:
                            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;
                            qryDistinctSale s
                            Code:
                            SELECT DISTINCT qrySumOfSales.[SumOfMachines Sold]
                            FROM qryDistinctSales
                            qryRankSales
                            Code:
                            SELECT qrySumOfSales.FKVendeurID, qrySumOfSales.[SumOfMachines Sold],
                            ((SELECT Count(*) FROM qryDistinctSales WHERE [SumOfMachines Sold] > x.[SumOfMachines Sold]]) + 1) AS Rank
                            FROM qrySumOfSales AS x

                            Comment

                            • Lisa B
                              New Member
                              • Jan 2011
                              • 30

                              #29
                              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
                              Code:
                              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

                              Comment

                              • Rabbit
                                Recognized Expert MVP
                                • Jan 2007
                                • 12517

                                #30
                                Oops, you're right, it was because of the extra bracket and the alias. Good luck with everything else.

                                Comment

                                Working...