MS-SQL 带有 NULL 的平均列

发布于 2024-08-24 03:40:02 字数 910 浏览 7 评论 0原文

所以我有 3 个不同的列(篮子 1、2 和 3)。有时这些列包含所有信息,有时其中一两个为空。我还有另一列,我将把这些值平均并保存。

即使其中一列为空,是否有一种时尚/简单的方法来获取这三列的平均值?或者我是否必须对每个空值进行特殊检查?

示例数据( ~~ 为空)

- B1 - B2 - B3 - Avg
------------------------------
- 10 - 20 - 30 - 20
- 10 - ~~ - 30 - 20
- ~~ - 20 - ~~ - 20

我将如何编写 T-SQL 来更新我的临时表?

UPDATE @MyTable
   SET Avg = ???

回答: 感谢 Aaronaught 我使用的方法。我将把我的代码放在这里,以防其他人有同样的事情。

WITH AverageView AS
(
    SELECT Results_Key AS xxx_Results_Key,
            AVG(AverageValue) AS xxx_Results_Average
    FROM @MyResults
        UNPIVOT (AverageValue FOR B IN (Results_Basket_1_Price, Results_Basket_2_Price, Results_Basket_3_Price)) AS UnpivotTable
    GROUP BY Results_Key
)   
UPDATE @MyResults
    SET Results_Baskets_Average_Price = xxx_Results_Average
    FROM AverageView
    WHERE Results_Key = xxx_Results_Key;

So I've got 3 different columns (basket 1, 2, and 3). Sometimes these columns have all the information and sometimes one or two of them are null. I have another column that I'm going to average these values into and save.

Is there a sleek/easy way to get the average of these three columns even if one of them is null? Or do I have to have a special check for each one being null?

Example data( ~~ is null)

- B1 - B2 - B3 - Avg
------------------------------
- 10 - 20 - 30 - 20
- 10 - ~~ - 30 - 20
- ~~ - 20 - ~~ - 20

How would I write the T-SQL to update my temp table?

UPDATE @MyTable
   SET Avg = ???

Answer:
Thanks to Aaronaught for the method I used. I'm going to put my code here just in case someone else has the same thing.

WITH AverageView AS
(
    SELECT Results_Key AS xxx_Results_Key,
            AVG(AverageValue) AS xxx_Results_Average
    FROM @MyResults
        UNPIVOT (AverageValue FOR B IN (Results_Basket_1_Price, Results_Basket_2_Price, Results_Basket_3_Price)) AS UnpivotTable
    GROUP BY Results_Key
)   
UPDATE @MyResults
    SET Results_Baskets_Average_Price = xxx_Results_Average
    FROM AverageView
    WHERE Results_Key = xxx_Results_Key;

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

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

发布评论

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

评论(3

那一片橙海, 2024-08-31 03:40:02

假设您有某种 ID 列,最有效的方法可能是使用 UNPIVOT,这样您就可以使用普通的基于行的 AVG 运算符(它会忽略 NULL 值):

DECLARE @Tbl TABLE
(
    ID int,
    B1 int,
    B2 int,
    B3 int
)

INSERT @Tbl (ID, B1, B2, B3) VALUES (1, 10, 20, 30)
INSERT @Tbl (ID, B1, B2, B3) VALUES (2, 10, NULL, 30)
INSERT @Tbl (ID, B1, B2, B3) VALUES (3, 10, NULL, NULL)

SELECT ID, AVG(Value) AS Average
FROM @Tbl
UNPIVOT (Value FOR B IN (B1, B2, B3)) AS u
GROUP BY ID

如果您没有 ID 列,则可以使用 ROW_NUMBER 生成代理 ID:

;WITH CTE AS
(
    SELECT
        B1, B2, B3,
        ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS ID
    FROM @Tbl
)
SELECT ID, AVG(Value)
FROM CTE
UNPIVOT (Value FOR B IN (B1, B2, B3)) AS u
GROUP BY ID

Assuming you have some sort of ID column, the most effective way is probably to use UNPIVOT so you can use the normal row-based AVG operator (which ignores NULL values):

DECLARE @Tbl TABLE
(
    ID int,
    B1 int,
    B2 int,
    B3 int
)

INSERT @Tbl (ID, B1, B2, B3) VALUES (1, 10, 20, 30)
INSERT @Tbl (ID, B1, B2, B3) VALUES (2, 10, NULL, 30)
INSERT @Tbl (ID, B1, B2, B3) VALUES (3, 10, NULL, NULL)

SELECT ID, AVG(Value) AS Average
FROM @Tbl
UNPIVOT (Value FOR B IN (B1, B2, B3)) AS u
GROUP BY ID

If you don't have the ID column, you can generate a surrogate ID using ROW_NUMBER:

;WITH CTE AS
(
    SELECT
        B1, B2, B3,
        ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS ID
    FROM @Tbl
)
SELECT ID, AVG(Value)
FROM CTE
UNPIVOT (Value FOR B IN (B1, B2, B3)) AS u
GROUP BY ID
你的呼吸 2024-08-31 03:40:02
SELECT  (
        SELECT  AVG(b)
        FROM    (
                SELECT  b1 AS b
                UNION ALL
                SELECT  b2
                UNION ALL
                SELECT  b3
                ) q
        )
FROM    mytable
SELECT  (
        SELECT  AVG(b)
        FROM    (
                SELECT  b1 AS b
                UNION ALL
                SELECT  b2
                UNION ALL
                SELECT  b3
                ) q
        )
FROM    mytable
风蛊 2024-08-31 03:40:02
SELECT (ISNULL(B1,0) + ISNULL(B2,0) + ISNULL(B3,0))
/(CASE WHEN B1 IS NULL THEN 0 ELSE 1 END
+CASE WHEN B2 IS NULL THEN 0 ELSE 1 END
+CASE WHEN B3 IS NULL THEN 0 ELSE 1 END)

并在其中添加逻辑以排除所有三个都为空的情况(如果需要)。

SELECT (ISNULL(B1,0) + ISNULL(B2,0) + ISNULL(B3,0))
/(CASE WHEN B1 IS NULL THEN 0 ELSE 1 END
+CASE WHEN B2 IS NULL THEN 0 ELSE 1 END
+CASE WHEN B3 IS NULL THEN 0 ELSE 1 END)

and put logic in there to exclude cases where all three are null if you need to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文