如何获取多项选择的结果并合并为一行

Posted

技术标签:

【中文标题】如何获取多项选择的结果并合并为一行【英文标题】:How do I take the results of multiple selects and combine into one row 【发布时间】:2010-11-01 04:46:10 【问题描述】:

我有一个表格,其中包含 4 台发电机的信息。我想将四个查询的结果放在一行中。有人有建议吗?

SELECT  avg(KW) as GEN_101_AVG
FROM         genset WHERE     (GenSetName   like 'GEA3519') and GenDate >= '1 jan   2003    00:00:00' and GenDate < '1 feb 2003 00:00:00'

   SELECT  avg(KW) as GEN_201_AVG
FROM         genset WHERE     (GenSetName   like 'GEA3520') and GenDate >= '1 jan 2003 00:00:00' and GenDate < '1 feb 2003 00:00:00'

SELECT  avg(KW) as GEN_301_AVG
FROM         genset WHERE     (GenSetName   like 'GEA3521') and GenDate >= '1 jan 2003 00:00:00' and GenDate < '1 feb 2003 00:00:00'

SELECT  avg(KW) as GEN_401_AVG
FROM         genset WHERE     (GenSetName   like 'GEA3522') and GenDate >= '1 jan 2003 00:00:00' and GenDate < '1 feb 2003 00:00:00'

【问题讨论】:

请使用“sqlserver”标签而不是“mssql”。标记问题时请注意建议:名称后数字小于 10 的任何标记都可能是错误的。在这种情况下,有超过 5000 个问题标记为“sqlserver”和零标记为“msssql”。 不知道为什么我投了反对票 【参考方案1】:
SELECT  (
        SELECT  avg(KW)
        FROM    genset
        WHERE   (GenSetName   like 'GEA3519')
                and GenDate >= '1 jan   2003    00:00:00'
                and GenDate < '1 feb 2003 00:00:00'
        ) AS avg_GEA3519,
        (
        SELECT  avg(KW)
        FROM    genset
        WHERE   (GenSetName   like 'GEA3520')
                and GenDate >= '1 jan   2003    00:00:00'
                and GenDate < '1 feb 2003 00:00:00'
        ) AS avg_GEA3520,
        (
        SELECT  avg(KW)
        FROM    genset
        WHERE   (GenSetName   like 'GEA3521')
                and GenDate >= '1 jan   2003    00:00:00'
                and GenDate < '1 feb 2003 00:00:00'
        ) AS avg_GEA3521,
        (
        SELECT  avg(KW)
        FROM    genset
        WHERE   (GenSetName   like 'GEA3522')
                and GenDate >= '1 jan   2003    00:00:00'
                and GenDate < '1 feb 2003 00:00:00'
        ) AS avg_GEA3522

,或者在SQL Server 2005+,这个:

SELECT  [GEA3519], [GEA3520], [GEA3521], [GEA3522]
FROM    (
        SELECT  GenSetName, KW
        FROM    genset
        WHERE   GenDate >= '1 Jan 2003  00:00:00'
                AND GenDate < '1 Feb 2003 00:00:00'
        ) AS q
PIVOT
(
        AVG(KW)
        FOR  GenSetName IN (['GEA3519'], ['GEA3520'], ['GEA3521'], ['GEA3522']
)

【讨论】:

完全是我的想法——尽管我个人也会给这些列起别名。 嗯,这也很好 :-) 我发布变量方法有点晚了。【参考方案2】:

另一种选择:

SELECT
     AVG(GEN_101.kw) AS GEN_101_AVG,
     AVG(GEN_201.kw) AS GEN_201_AVG,
     AVG(GEN_301.kw) AS GEN_301_AVG,
     AVG(GEN_401.kw) AS GEN_401_AVG
FROM
     Genset GEN_101
INNER JOIN Genset GEN_201 ON
     GEN_201.GenSetName = 'GEA3520' AND
     GEN_201.GenDate >= '1 jan 2003 00:00:00' AND
     GEN_201.GenDate < '1 feb 2003 00:00:00'
INNER JOIN Genset GEN_101 ON
     GEN_301.GenSetName = 'GEA3521' AND
     GEN_301.GenDate >= '1 jan 2003 00:00:00' AND
     GEN_301.GenDate < '1 feb 2003 00:00:00'
INNER JOIN Genset GEN_101 ON
     GEN_401.GenSetName = 'GEA3522' AND
     GEN_401.GenDate >= '1 jan 2003 00:00:00' AND
     GEN_401.GenDate < '1 feb 2003 00:00:00'
WHERE
     GEN_101.GenSetName = 'GEA3519' AND
     GEN_101.GenDate >= '1 jan 2003 00:00:00' AND
     GEN_101.GenDate < '1 feb 2003 00:00:00'

【讨论】:

这确实适用于 AVG(并且仅适用于 AVG),因为 AVG 不依赖于表交叉连接到另一个表的次数。性能当然会很差,但一个可行且不那么明显的解决方案值得 +1。【参考方案3】:

由于选择的数量限制为四个,一种解决方案是选择变量,然后进行最终选择。像下面这样:

declare @var1 <TYPE>
declare @var2 <TYPE>
declare @var3 <TYPE>
declare @var4 <TYPE>

select @var1 = SELECT avg(...) ...
select @var2 = ...
select @var3 = ...
select @var4 = ...

select @var1 as ..., @var2 as ..., @var3 as ..., @var4 as ...

【讨论】:

您最好使用上述子查询解决方案。如果您需要在选择之前操作四个值,您可以使用我的方法。

以上是关于如何获取多项选择的结果并合并为一行的主要内容,如果未能解决你的问题,请参考以下文章

Google 表单作为多项选择测验 - 如何提供结果

如何获取所有mysql元组结果并转换为json

【OFFICE 365】Power Query 合并工作表

jQuery Chosen:如何从多项选择中获取所选选项文本的数组

使用ivx小模块制作单项和多项选择器的经验总结

使用ivx小模块制作单项和多项选择器的经验总结