0

SELECT RANK()over(Partition by ...)でPIVOT関数を使用して、軍ごとに最も多くの勝利を収めているウォーゲームの友達を示すテーブル「マトリックス」を返しています。軍隊ごとに上位3位のメンバーのみを表示するようにマトリックスを制限したい(たとえば、下の画像の「HE」列で、ランク「4」の強調表示されたレコードを除外したい)

メンバーによる陸軍の勝利

WHERE句またはTOP句を含める必要があると思いますが、その場所を特定できません。このサイトとグーグルを見てみましたが、答えが見つかりません。これが学習者の質問である場合は申し訳ありませんが、私はまだSQLServerにかなり慣れていません。

[Armies]と[Battles]の2つのテーブル(スプレッドシート形式)は次のとおりです。- https ://docs.google.com/spreadsheet/ccc?key = 0Ana40VqkvVtRdDAwc1BRWnhsWEdaaTQzcFprQmlyeVE

これが私のコードです:-

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;

あなたが与えることができるどんな助けにも感謝します。

CREATE TABLEスクリプト:-

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

)[プライマリ]で

GO
4

3 に答える 3

1

WHERE簡単な句を適用できるように、サブクエリのレベルを追加しました

SELECT *
FROM
    (SELECT * FROM ( --Subquery start
    SELECT
        RANK() over(Partition by ArmyMnemonic Order by COUNT(WDL) desc, Member) as ranks, ArmyMnemonic, Army, Member,
         COUNT(WDL) as Wins
    FROM
        Battles

    INNER JOIN Armies on Army1 = Armies.ArmyNum

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

    ) t where t.ranks <= 3 --New WHERE clause

    ) 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;
于 2012-11-09T13:17:21.693 に答える
1

ranksThe は関数で使用されているため、WHERE句では使用できませんPIVOTranksしたがって、フィルタリングするために 2 番目の列を作成できます。内部選択に 2 番目の列を作成しました。

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

WHEREこれを句で使用できます。

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;

WHEREまたは、 の前にサブクエリでを適用できます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;
于 2012-11-09T13:20:49.363 に答える
0

すべてを別のselectステートメントで囲みます。

SELECT TOP 3 * 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
    ) 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

于 2012-11-09T13:13:03.867 に答える