我使用功能和count(*)分组对表进行了操作:

SELECT TOP 10000
  [column1]
  ,[column2]
  ,[column3]
  ,count(*) as counts
FROM TableX
group by column1, column2,column3


我的结果是表看起来:

column1 | column2 | column2 | Counts
------------------------------------
   X1   |    X2   |    X3   |   X4
   X5   |    X6   |    X7   |   X8
   ...


现在如何添加一个附加列,该列将显示X3 * X4,X7 * X8等的结果?

最佳答案

如果实际上是SQL Server,而不是MySQL,则可以使用以下命令:

WITH CTE AS
(
    SELECT TOP 10000
       [column1]
      ,[column2]
      ,[column3]
      ,counts = count(*)  OVER (PARTITION BY column1, column2,column3)
      ,RN = row_number(*) OVER (PARTITION BY column1, column2,column3 Order By column1, column2,column3)
    FROM TableX
)
SELECT  [column1]
       ,[column2]
       ,[column3]
       ,counts
       ,Product1 = column3 * counts,
       ...
FROM CTE
WHERE RN = 1

关于mysql - 将两列乘以第三列,其中之一是sql中的count(*),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28476095/

10-13 05:06