我有一个包含2列的数据库,这些列是(并且需要)相同类型的客户ID。我正在尝试这样一个SQL查询,它将列出每个客户ID在表中显示的次数。到目前为止,我有以下内容,我需要将每个个人ID添加到一起,因此它只列出一次。 enter image description here
SELECT TblMatch.CustomerID1, Count(TblMatch.[Match ID]) AS [TotalMatch ID]
FROM TblMatch
GROUP BY TblMatch.CustomerID1
UNION
SELECT TblMatch.CustomerID2, Count(TblMatch.[Match ID]) AS [TotalMatch ID]
FROM TblMatch
GROUP BY TblMatch.CustomerID2;
答案 0 :(得分:1)
试试这个:
select ID, count([Match ID]) as [TotalMatch ID] from
(
SELECT TblMatch.CustomerID1 as ID, TblMatch.[Match ID]
FROM TblMatch
UNION ALL
SELECT TblMatch.CustomerID2 as ID, TblMatch.[Match ID]
FROM TblMatch
) tmp
group by ID
答案 1 :(得分:0)
这样的事情应该有效,你的工会全部聚合:
SELECT
TblMatch.CustomerID1,
Count(data.[Match ID]) AS [TotalMatch ID]
FROM
(
TblMatch,
(
TblMatch.[Match ID]
)
UNION ALL
SELECT
TblMatch.CustomerID2,
(
TblMatch.[Match ID]
)
FROM
TblMatch
)
data
GROUP BY
TblMatch.CustomerID1;
答案 2 :(得分:0)
您的查询应该是这样的:
SELECT * FROM (
SELECT CUSTOMERID1,COUNT(*)
FROM TBLMATCH
GROUP BY CUSTOMERID1
UNION ALL
SELECT CUSTOMERID2, COUNT(*)
FROM TBLMATCH
GROUP BY CUSTOMERID2
)ORDER BY CUSTOMERID1;