Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8807257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:19:35+00:00 2026-06-14T02:19:35+00:00

I am using the PIVOT function on a SELECT RANK() over(Partition by…) to return

  • 0

I am using the PIVOT function on a SELECT RANK() over(Partition by…) to return a table “matrix” showing which of my war gaming friends have the most wins per army. I want to limit the matrix to only show the top 3 ranked members per army (eg. in the “HE” column in the image below I want to exclude the highlighted record with the rank of “4”)

Army Wins by Member

I presume I need to include a WHERE or TOP clause but cannot determine it’s location. I have tried looking on this site and google but cannot find an answer. Sorry if this is a learner’s question but I’m still fairly new to SQL Server.

Here are the two tables [Armies] and [Battles] (in spreadsheet format):-
https://docs.google.com/spreadsheet/ccc?key=0Ana40VqkvVtRdDAwc1BRWnhsWEdaaTQzcFprQmlyeVE

Here’s my code:-

SELECT *
FROM
    (
    SELECT
        RANK() over(Partition by ArmyMnemonic Order by COUNT(WDL) desc, Member) as ranks, ArmyMnemonic, Army, Member,
         COUNT(WDL) as Wins
    FROM
        [dbo].[Battles]

    INNER JOIN Armies on Army1 = Armies.ArmyNum

    Where
        WDL=2 and Home=1 -- represents a "Win"
    Group By
        Member, ArmyMnemonic, Army, WDL
    ) as rnk

PIVOT (sum(rnk.ranks) for ArmyMnemonic in([Be],[Br],[DoC],[DE],[Dw],[HE],[Li],[OK],[OG],[Sk],[TE],[TK],[VC],[WoC],[WE])) as pvt

ORDER BY Wins Desc;

Thanks for any help you can give.

CREATE TABLE Scripts:-

USE [WFBattlesDB]
GO

/****** Object:  Table [dbo].[Armies]    Script Date: 11/09/2012 13:24:15 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Armies](
[ArmyNum] [int] NOT NULL,
[ArmyMnemonic] [nvarchar](3) NOT NULL,
[Army] [char](30) NOT NULL,
[Official] [bit] NULL,
[Active] [bit] NULL,
 CONSTRAINT [PK_Armies] PRIMARY KEY CLUSTERED 

(
[ArmyNum] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

USE [WFBattlesDB]
GO

/****** Object:  Table [dbo].[Battles]    Script Date: 11/09/2012 13:25:35 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Battles](
[keyBattle] [int] IDENTITY(1,1) NOT NULL,
[subDate] [datetime] NOT NULL,
[Member] [nvarchar](20) NOT NULL,
[Home] [bit] NOT NULL,
[Army1] [int] NOT NULL,
[Army2] [int] NOT NULL,
[WDL] [int] NOT NULL,
[PtsVal] [int] NULL,
[MVU] [nvarchar](30) NULL,
[Fun] [int] NULL,
[Luck] [int] NULL,
[Notes] [nvarchar](1500) NULL,
[Link] [nvarchar](255) NULL

) ON [PRIMARY]

GO
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:19:36+00:00Added an answer on June 14, 2026 at 2:19 am

    You cannot use the ranks in the WHERE clause because it is used in the PIVOT function. So you can create a second ranks column, to filter on. I created a second column in the inner select:

    RANK() over(Partition by ArmyMnemonic Order by COUNT(WDL) desc, Member) as rankFilter
    

    You can then use this in the WHERE clause:

    SELECT Army, Member, Wins, [Be],[Br],[DoC],[DE],[Dw],[HE],[Li],[OK],[OG],[Sk],[TE],[TK],[VC],[WoC],[WE]
    FROM
    (
        SELECT
            RANK() over(Partition by ArmyMnemonic Order by COUNT(WDL) desc, Member) as ranks, ArmyMnemonic, Army, Member,
             COUNT(WDL) as Wins,
           RANK() over(Partition by ArmyMnemonic Order by COUNT(WDL) desc, Member) as rankFilter
        FROM
            [dbo].[Battles]
    
        INNER JOIN Armies on Army1 = Armies.ArmyNum
    
        Where
            WDL=2 and Home=1 -- represents a "Win"
        Group By
            Member, ArmyMnemonic, Army, WDL
        ) as rnk
    
    PIVOT (sum(rnk.ranks) for ArmyMnemonic in([Be],[Br],[DoC],[DE],[Dw],[HE],[Li],[OK],[OG],[Sk],[TE],[TK],[VC],[WoC],[WE])) as pvt
    where rankFilter <=3
    
    ORDER BY Wins Desc;
    

    Or you can apply the WHERE in a subquery before the PIVOT:

    SELECT Army, Member, Wins, [Be],[Br],[DoC],[DE],[Dw],[HE],[Li],[OK],[OG],[Sk],[TE],[TK],[VC],[WoC],[WE]
    FROM
    (
        select *
        from
        (
            SELECT
                RANK() over(Partition by ArmyMnemonic Order by COUNT(WDL) desc, Member) as ranks, ArmyMnemonic, Army, Member,
                 COUNT(WDL) as Wins
            FROM
                [dbo].[Battles]
    
            INNER JOIN Armies on Army1 = Armies.ArmyNum
    
            Where
                WDL=2 and Home=1 -- represents a "Win"
            Group By
                Member, ArmyMnemonic, Army, WDL
        )  rnk
        where rnk.ranks <= 3
    ) src
    PIVOT (sum(ranks) for ArmyMnemonic in([Be],[Br],[DoC],[DE],[Dw],[HE],[Li],[OK],[OG],[Sk],[TE],[TK],[VC],[WoC],[WE])) as pvt
    
    ORDER BY Wins Desc;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possibility of using cross tab and pivot table function in postgres 8.3 version I
Using the MySQL query below, I have created a pivot table which is pretty
I am using a pivot query in SQL as below: SELECT classification, [BSL] AS
I have this great function that parses a field into a pivot table consisting
I have a problem with creating a pivot table in PostgreSQL using the crosstab()
If I create a VIEW using this pivot table query, it isn't editable. The
Does anyone have an example of a pivot using a table with an XML
I'm using the SQL Server 2008 PIVOT function after I turn a CSV field
I have generated a table using PIVOT and the ouput of columns are dynamic.
I am using following procedure to retrieve total presents and absents by using pivot

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.