计数和 return 其中 2 个字段匹配

Count and return where 2 fields match

我正在尝试编写一个脚本,该脚本根据 2 个匹配的字段计算结果 - 但不是完全匹配,而是在整个 table.

中重复出现的值

例如,我想分别找到字段 A 和字段 B = x & y 的位置(并计算这些结果),但是,字段 A 并不总是 X,字段 B 也不总是 Y。另外,字段 A 和字段 B 值未知。这是我到目前为止所写的内容:

select a.fielda, b.fieldb, count (*)
from tableA a
join tableB b
on a.fieldd = b.fieldd

where a.fielda = b.fieldb --I know this is a problem, just for notes on what I'm trying to accomplish.
group by b.fieldb, a.fielda
order by b.fieldb.

我是新手,非常感谢您的帮助。提前谢谢你。

SELECT SUM(CASE WHEN a.fielda = b.fieldb THEN 1 ELSE NULL END) AS MatchCount
     , SUM(CASE WHEN a.fielda = x and b.fieldb = y THEN 1 ELSE NULL END) AS XYCount
     , COUNT(*) AS FieldDMatchCount
FROM tableA a
JOIN tableB b
  ON a.fieldd = b.fieldd

Tobsey 对我来说是正确的。我不认为通过选择这两个字段我已经找到了这两个字段一起存在的记录......我猜是脑放屁。感谢您的帮助!