我有一张这样的表:
ycsb=# select * from pgstatindex('usertable_pkey');
version | tree_level | index_size | root_block_no | internal_pages | leaf_pages | empty_pages | deleted_pages | avg_leaf_density | leaf_fragmentation
---------+------------+------------+---------------+----------------+------------+-------------+---------------+------------------+--------------------
2 | 3 | 5488721920 | 44337 | 4464 | 665545 | 0 | 0 | 52 | 11
(1 row)
我想选择每个名称,按ID分组并计算事务(NOT SUM)。所以我想要下表:
Name Id Amount
Name1 1 99
Name1 1 30
Name1 9 120.2
Name2 21 348
Name2 21 21
Name3 41 99
我试过这个sql:
Name Id Count
Name1 1 2
Name1 9 1
Name2 21 2
Name3 41 1
但是我收到以下错误:SELECT
[Name],
[Id]
FROM table1 A
INNER JOIN (
SELECT
[Id],
count([Amount]) as 'Count'
FROM
table1
GROUP BY [Id]
)
B ON A.[Id] = B.[Id]
我做错了什么?
答案 0 :(得分:3)
SELECT
[Name],
[Id],
count([Amount]) as 'Count'
FROM
table1
GROUP BY [Name], [Id]
答案 1 :(得分:0)
SELECT
A.[Name],
A.[Id]
FROM table1 A
INNER JOIN (
SELECT
table1.[Id],
count([Amount]) as 'Count'
FROM
table1
GROUP BY table1.[Id]
)
B ON A.[Id] = B.[Id]