在sql中聚合

hmmo2u0o  于 2021-08-09  发布在  Java
关注(0)|答案(2)|浏览(256)

这个问题在这里已经有答案了

列“在选择列表中无效,因为它不包含在聚合函数或GROUPBY子句中”(3个答案)
10个月前关门了。
我有一张这样的table

需要像下面这样聚合

DROP TABLE IF EXISTS #tmp2
SELECT item, Quantity, Seconds
SUM(Quantity) AS total_quantity,
--SUM(Seconds) AS total_seconds,
--SUM(Seconds)/3600 AS items_per_hour
INTO #tmp2
FROM #tmp1
GROUP BY item
doinxwow

doinxwow1#

如果您想要列的sun,可以在select中直接调用同一列

SELECT item

    SUM(Quantity) AS total_quantity,
    --SUM(Seconds) AS total_seconds,
    --SUM(Seconds)/3600 AS items_per_hour
    INTO #tmp2
    FROM #tmp1
    GROUP BY item
jtjikinw

jtjikinw2#

对于聚合,您不需要 Quantity 以及 Seconds . 请尝试以下操作

SELECT 
   item,
   SUM(Quantity) AS total_quantity,
   SUM(Seconds) AS total_seconds,
   SUM(Seconds)/3600 AS items_per_hour
FROM #tmp1
GROUP BY 
  item

相关问题