SQL - 对表的列和的操作

Posted

技术标签:

【中文标题】SQL - 对表的列和的操作【英文标题】:SQL - operation on the Column sum of a Table 【发布时间】:2018-10-09 18:30:41 【问题描述】:

假设我在 SQL Server 中有一个包含 10 列的表,标题为 Col-1, Col-2, ... Col-10。现在我想编写一个 SQL 查询,它首先 分别计算所有 10 列的列总和 - 从而生成长度为 10 的行,然后 计算值的中位数生成的行数

要添加更多详细信息,假设我有下表。我需要 SQL 来计算 Col-sum 值的中值,如下图所示。

    Col - 1 Col - 2 Col - 3 Col - 4 Col - 5 Col - 6 Col - 7 Col - 8 Col - 9 Col - 10
    0.4763  0.9746  0.5082  0.8707  0.3608  0.6984  0.9326  0.9983  0.1441  0.6882
    0.9396  0.9358  0.6548  0.8046  0.3274  0.3072  0.1275  0.8273  0.9785  0.9618
    0.6656  0.7000  0.1664  0.0341  0.9804  0.4973  0.2023  0.4619  0.9759  0.0456
    0.9707  0.3495  0.8282  0.6389  0.8845  0.8833  0.8424  0.6087  0.4165  0.6986
    0.5992  0.8121  0.0324  0.9134  0.9613  0.6163  0.1110  0.8911  0.7429  0.4397
    0.1069  0.6702  0.6478  0.9055  0.3594  0.0060  0.0556  0.4216  0.0578  0.2796
Col-sum 3.7583  4.4421  2.8378  4.1672  3.8737  3.0085  2.2714  4.2088  3.3157  3.1135
**Median    3.537024951**                                   

任何如何在 SQL 中实现这样的指针都将受到高度赞赏。谢谢,

【问题讨论】:

没有足够的信息来理解您的要求,更不用说提供任何帮助了。尝试this 作为起点。 您需要阅读 transact-sql/sql-server 文档中的“聚合函数” 阅读本文。 ***.com/help/how-to-ask 样本,预期结果,到目前为止你尝试了什么,提供更多细节? 在原始线程中添加了此类表的示例 【参考方案1】:

让我们从临时表中的示例数据开始:

CREATE TABLE #temp
(
    Col1 DECIMAL(18, 4)
    ,Col2 DECIMAL(18, 4)
    ,Col3 DECIMAL(18, 4)
    ,Col4 DECIMAL(18, 4)
    ,Col5 DECIMAL(18, 4)
    ,Col6 DECIMAL(18, 4)
    ,Col7 DECIMAL(18, 4)
    ,Col8 DECIMAL(18, 4)
    ,Col9 DECIMAL(18, 4)
    ,Col10 DECIMAL(18, 4)
)

INSERT INTO #temp
(
    Col1
    ,Col2
    ,Col3
    ,Col4
    ,Col5
    ,Col6
    ,Col7
    ,Col8
    ,Col9
    ,Col10
)
VALUES
(0.4763, 0.9746, 0.5082, 0.8707, 0.3608, 0.6984, 0.9326, 0.9983, 0.1441, 0.6882)
,(0.9396, 0.9358, 0.6548, 0.8046, 0.3274, 0.3072, 0.1275, 0.8273, 0.9785, 0.9618)
,(0.6656, 0.7000, 0.1664, 0.0341, 0.9804, 0.4973, 0.2023, 0.4619, 0.9759, 0.0456)
,(0.9707, 0.3495, 0.8282, 0.6389, 0.8845, 0.8833, 0.8424, 0.6087, 0.4165, 0.6986)
,(0.5992, 0.8121, 0.0324, 0.9134, 0.9613, 0.6163, 0.1110, 0.8911, 0.7429, 0.4397)
,(0.1069, 0.6702, 0.6478, 0.9055, 0.3594, 0.0060, 0.0556, 0.4216, 0.0578, 0.2796)

接下来,我们将使用几个 CTE 将数据转换为我们需要计算中位数的形式。顺便说一句,我在这里使用答案中发布的中值算法:Function to Calculate Median in SQL Server

第一个 CTE 获取所有列的 SUM。第二个 CTE 将列“透视”为行。然后主查询找到中位数。

;WITH Sums AS
(
    SELECT Col1 = SUM(col1)
          ,Col2 = SUM(col2)
          ,Col3 = SUM(col3)
          ,Col4 = SUM(col4)
          ,Col5 = SUM(col5)
          ,Col6 = SUM(col6)
          ,Col7 = SUM(col7)
          ,Col8 = SUM(col8)
          ,Col9 = SUM(col9)
          ,Col10  = SUM(col10)
    FROM #temp
)
,SumVals AS
(
    SELECT SumVal = Col1 
    FROM Sums
    UNION ALL
    SELECT SumVal = Col2 
    FROM Sums
    UNION ALL
    SELECT SumVal = Col3 
    FROM Sums
    UNION ALL
    SELECT SumVal = Col4 
    FROM Sums
    UNION ALL
    SELECT SumVal = Col5 
    FROM Sums
    UNION ALL
    SELECT SumVal = Col6 
    FROM Sums
    UNION ALL
    SELECT SumVal = Col7 
    FROM Sums
    UNION ALL
    SELECT SumVal = Col8 
    FROM Sums
    UNION ALL
    SELECT SumVal = Col9 
    FROM Sums
    UNION ALL
    SELECT SumVal = Col10 
    FROM Sums
)
SELECT
   Median = AVG(SumVal)
FROM
(
   SELECT
      SumVal,
      ROW_NUMBER() OVER (ORDER BY SumVal ASC) AS RowAsc,
      ROW_NUMBER() OVER (ORDER BY SumVal DESC) AS RowDesc
   FROM SumVals sv
) x
WHERE
   RowAsc IN (RowDesc, RowDesc - 1, RowDesc + 1)


DROP TABLE #temp

结果:

Median
3.537000

【讨论】:

【参考方案2】:

也许你需要这个?

DECLARE 
    @t  TABLE (
        col1    DECIMAL(18,4)
    ,   col2    DECIMAL(18,4)
    ,   col3    DECIMAL(18,4)
    ,   col4    DECIMAL(18,4)
    ,   col5    DECIMAL(18,4)
    ,   col6    DECIMAL(18,4)
    ,   col7    DECIMAL(18,4)
    ,   col8    DECIMAL(18,4)
    ,   col9    DECIMAL(18,4)
    ,   col10   DECIMAL(18,4)
    )

INSERT INTO @t VALUES 
(0.4763,0.9746,0.5082,0.8707,0.3608,0.6984,0.9326,0.9983,0.1441,0.6882),
(0.9396,0.9358,0.6548,0.8046,0.3274,0.3072,0.1275,0.8273,0.9785,0.9618),
(0.6656,0.7000,0.1664,0.0341,0.9804,0.4973,0.2023,0.4619,0.9759,0.0456),
(0.9707,0.3495,0.8282,0.6389,0.8845,0.8833,0.8424,0.6087,0.4165,0.6986),
(0.5992,0.8121,0.0324,0.9134,0.9613,0.6163,0.1110,0.8911,0.7429,0.4397),
(0.1069,0.6702,0.6478,0.9055,0.3594,0.0060,0.0556,0.4216,0.0578,0.2796)


SELECT
    (col1 + col2 + col3 + col4 + col5 + col6 + col7 + col8 + col9 + col10) / 10 Median
FROM (
SELECT 
    SUM(col1) col1
,   SUM(col2) col2
,   SUM(col3) col3
,   SUM(col4) col4
,   SUM(col5) col5
,   SUM(col6) col6
,   SUM(col7) col7
,   SUM(col8) col8
,   SUM(col9) col9
,   SUM(col10) col10
FROM @t 
) D

【讨论】:

【参考方案3】:

这个查询也可以:

        Select sum(sumOfColumns)/2 as medianOfColumns from (select * from (
        (
        Select ROW_NUMBER() over (ORDER BY sumOfColumns ASC) as rnum, sumOfColumns from
     (
        Select sum(col1) as sumOfColumns from temp_table group by col1
        UNION ALL
        Select sum(col2) as sumOfColumns from temp_table group by col2
        UNION ALL
        Select sum(col3) as sumOfColumns from temp_table group by col3
        UNION ALL
        Select sum(col4) as sumOfColumns from temp_table group by col4
        UNION ALL
        Select sum(col5) as sumOfColumns from temp_table group by col5
        UNION ALL
        Select sum(col6) as sumOfColumns from temp_table group by col6
        UNION ALL
        Select sum(col7) as sumOfColumns from temp_table group by col7
        UNION ALL
        Select sum(col8) as sumOfColumns from temp_table group by col8
        UNION ALL
        Select sum(col9) as sumOfColumns from temp_table group by col9
        UNION ALL
        Select sum(col10) as sumOfColumns from temp_table group by col10
        ) 
        ) 
)where rnum BETWEEN 5 and 6
);

【讨论】:

以上是关于SQL - 对表的列和的操作的主要内容,如果未能解决你的问题,请参考以下文章

使用 ASP.Net 在 SQL 中对表的列值求和时出现异常

sql对表的操作

如何在 PL/SQL 中设置特定日期对表的操作执行?

使用sql更改表的列的数据类型和添加新列和约束

Oracle:SQL语句--对表的操作——修改表名

Oracle:对表的CREATEALTERINSERTRENAMEDELETE操作练习以及主外键约束