我使用group by function和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等的结果?
答案 0 :(得分:1)
如果它实际上是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
答案 1 :(得分:0)
使用查询结果,然后乘以:
select [column1], [column2], [column3], counts, [column3] * counts
from
(
SELECT TOP 10000
[column1]
,[column2]
,[column3]
,count(*) as counts
FROM TableX
group by column1, column2,column3
)