T-SQL 按分区分组

发布于 2024-10-10 11:44:30 字数 758 浏览 9 评论 0原文

我在 SQL Server 2008 中有下表。请帮助获得预期输出

alt text

谢谢。

CREATE TABLE [dbo].[Test]([Category] [varchar](10) NULL,[Value] [int] NULL,
[Weightage] [int] NULL,[Rn] [smallint] NULL ) ON [PRIMARY]

insert into Test values ('Cat1',310,674,1),('Cat1',783,318,2),('Cat1',310,96,3),('Cat1',109,917,4),('Cat2',441,397,1),('Cat2',637,725,2),('Cat2',460,742,3),('Cat2',542,583,4),('Cat2',601,162,5),('Cat2',45,719,6),('Cat2',46,305,7),('Cat3',477,286,1),('Cat3',702,484,2),('Cat3',797,836,3),('Cat3',541,890,4),('Cat3',750,962,5),('Cat3',254,407,6),('Cat3',136,585,7),('Cat3',198,477,8),('Cat4',375,198,1),('Cat4',528,351,2),('Cat4',845,380,3),('Cat4',716,131,4),('Cat4',781,919,5)

I have below table in SQL server 2008.Please help to get expected output

alt text

Thanks.

CREATE TABLE [dbo].[Test]([Category] [varchar](10) NULL,[Value] [int] NULL,
[Weightage] [int] NULL,[Rn] [smallint] NULL ) ON [PRIMARY]

insert into Test values ('Cat1',310,674,1),('Cat1',783,318,2),('Cat1',310,96,3),('Cat1',109,917,4),('Cat2',441,397,1),('Cat2',637,725,2),('Cat2',460,742,3),('Cat2',542,583,4),('Cat2',601,162,5),('Cat2',45,719,6),('Cat2',46,305,7),('Cat3',477,286,1),('Cat3',702,484,2),('Cat3',797,836,3),('Cat3',541,890,4),('Cat3',750,962,5),('Cat3',254,407,6),('Cat3',136,585,7),('Cat3',198,477,8),('Cat4',375,198,1),('Cat4',528,351,2),('Cat4',845,380,3),('Cat4',716,131,4),('Cat4',781,919,5)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深海夜未眠 2024-10-17 11:44:30

对于每个类别 平均权重

SELECT
   Category,
   AVG(Value),
   SUM(CASE WHEN RN<4 THEN Weightage ELSE 0 END) / (NULLIF(SUM(CASE WHEN RN<4 THEN 1 ELSE 0 END), 0))
FROM
   MyTable
GROUP BY
   Category

整个集合的平均权重

SELECT
   M.Category,
   AVG(Value),
   foo.AvgWeightage
FROM
   MyTable M
   CROSS JOIN
   (SELECT AVG(Weightage) As AvgWeightage FROM MyTable WHERE Rn < 4) foo
GROUP BY
   M.Category, foo.AvgWeightage

For per category Average Weightage

SELECT
   Category,
   AVG(Value),
   SUM(CASE WHEN RN<4 THEN Weightage ELSE 0 END) / (NULLIF(SUM(CASE WHEN RN<4 THEN 1 ELSE 0 END), 0))
FROM
   MyTable
GROUP BY
   Category

Average Weightage over the whole set

SELECT
   M.Category,
   AVG(Value),
   foo.AvgWeightage
FROM
   MyTable M
   CROSS JOIN
   (SELECT AVG(Weightage) As AvgWeightage FROM MyTable WHERE Rn < 4) foo
GROUP BY
   M.Category, foo.AvgWeightage
数理化全能战士 2024-10-17 11:44:30

简单的:)

  SELECT Category,  
        AVG(Value) AS AvgValue, 
        AVG(CASE WHEN RN< 4 THEN (Weightage) END ) AS AvgWeightage
  FROM Test
  GROUP BY Category

Simple:)

  SELECT Category,  
        AVG(Value) AS AvgValue, 
        AVG(CASE WHEN RN< 4 THEN (Weightage) END ) AS AvgWeightage
  FROM Test
  GROUP BY Category
苏辞 2024-10-17 11:44:30

试试这个

SELECT AvgValue.Category, AvgValue.AvgValue, AvgWeight.Weight
FROM(
(SELECT c.Category, 
        AVG(c.Value) AS AvgValue
    FROM Test c
    GROUP BY Category) AvgValue
INNER JOIN 
(SELECT Category, AVG(Weightage) AS Weight
    FROM Test
    WHERE Rn < 4
    GROUP BY Category) AvgWeight
ON AvgValue.Category = AvgWeight.Category)

Try this

SELECT AvgValue.Category, AvgValue.AvgValue, AvgWeight.Weight
FROM(
(SELECT c.Category, 
        AVG(c.Value) AS AvgValue
    FROM Test c
    GROUP BY Category) AvgValue
INNER JOIN 
(SELECT Category, AVG(Weightage) AS Weight
    FROM Test
    WHERE Rn < 4
    GROUP BY Category) AvgWeight
ON AvgValue.Category = AvgWeight.Category)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文