如何从两个相关但不同的表中获取不同的用户数
how to get a Distinct Count of users from two related but different tables
对此表示歉意,但 SQL 对我来说不是一个强项,虽然看起来与许多其他查询相似,但我无法成功地将它们转化为这种情况。
我有两个 table,如果 table 2 中存在一行,它们将通过一个公共值(id 和 Issue)关联。
我需要明确提出特定问题的用户数量。我在两个 table 中都有用户,table 2 用户如果存在则优先。
Table1中总有一个REPORTER,但是table中可能没有Name(fieldtype=1)的Stringvalue 2.如果有Stringvalue那么就是"User" 并且 Reporter 可以忽略。
Table 1
| id | Reporter| Type |
| 1 | 111111 | 1 |
| 2 | 111111 | 2 |
| 3 | 222222 | 2 |
| 4 | 333333 | 1 |
| 5 | 111111 | 1 |
| 6 | 666666 | 1 |
Table 2
|issue | Stringvalue | fieldType|
| 1 | Fred | 1 |
| 1 | bananas | 2 |
| 2 | Jack | 1 |
| 5 | Steve | 1 |
我一共有4个正确类型的问题(1,4,5,6),三个记者(111111,333333,666666)和两个Stringvalues(Fred,Steve)。
我的不同用户总数 = 4(Fred,333333,Steve,666666)
结果Table
| id| T1.Reporter | T2.Name |
| 1| Null | Fred |
| 4| 333333 | Null |
| 5| Null | Steve |
| 6| 666666 | Null |
如何在 SQL!
中得到这个结果
目前最接近的尝试:
SELECT
table1.REPORTER,
TO_CHAR(NULL) "NAME"
FROM table1
Where table1.TYPE =1
AND table1.REPORTER <> '111111'
Union
SELECT
TO_CHAR(NULL) "REPORTER",
table2.STRINGVALUE "NAME"
FROM table2,
table1
WHERE table2.ISSUE = table1.ID
AND table2.fieldtype= 1
and table1.issuetype = 1
在没有明确排除默认 table 1 报告者的情况下,即使在 table 2.
中有名称值,我的结果中也会 returned
我已经尝试 exists
并且无法获得正确的语法或正确的结果。一旦尝试任何连接 ID 和 Issue 值的连接,结果总是最终限制为匹配的行或所有值。并在 ON 中添加附加条件并没有 return 正确的结果。
我尝试了太多的排列,无法列出,从逻辑上讲,这听起来应该能够与存在的位置进行联合,或者左外连接,但我的技能不足以完成这项工作。
如果我没理解错的话,你想要一个left join
和count(distinct)
。我认为您正在寻找以下内容:
select count(distinct coalesce(stringval, reporter) )
from table1 t1 left join
table2 t2
on t1.id = t2.issue and t2.fieldtype = 1
where t1.id in (1, 4, 5, 6);
您需要学习如何使用显式 JOIN
语法。作为一个简单的规则:从不 在 FROM
子句中使用逗号。始终使用明确的 JOIN
语法。一方面,它更强大,可以轻松表达外连接。
您需要使用 LEFT JOIN
,这就是您指定 fieldtype = 1
子句的地方:
SELECT
table1.id,
CASE
WHEN table2.Stringvalue IS NOT NULL THEN table2.Stringvalue
ELSE table1.Reporter
END AS TheUser
FROM table1
LEFT JOIN table2 ON table1.id = table2.issue AND table2.fieldType = 1
WHERE table1.Type = 1
结果:
+------+---------+
| id | TheUser |
+------+---------+
| 1 | Fred |
| 4 | 333333 |
| 5 | Steve |
| 6 | 666666 |
+------+---------+
对此表示歉意,但 SQL 对我来说不是一个强项,虽然看起来与许多其他查询相似,但我无法成功地将它们转化为这种情况。
我有两个 table,如果 table 2 中存在一行,它们将通过一个公共值(id 和 Issue)关联。
我需要明确提出特定问题的用户数量。我在两个 table 中都有用户,table 2 用户如果存在则优先。
Table1中总有一个REPORTER,但是table中可能没有Name(fieldtype=1)的Stringvalue 2.如果有Stringvalue那么就是"User" 并且 Reporter 可以忽略。
Table 1
| id | Reporter| Type |
| 1 | 111111 | 1 |
| 2 | 111111 | 2 |
| 3 | 222222 | 2 |
| 4 | 333333 | 1 |
| 5 | 111111 | 1 |
| 6 | 666666 | 1 |
Table 2
|issue | Stringvalue | fieldType|
| 1 | Fred | 1 |
| 1 | bananas | 2 |
| 2 | Jack | 1 |
| 5 | Steve | 1 |
我一共有4个正确类型的问题(1,4,5,6),三个记者(111111,333333,666666)和两个Stringvalues(Fred,Steve)。 我的不同用户总数 = 4(Fred,333333,Steve,666666)
结果Table
| id| T1.Reporter | T2.Name |
| 1| Null | Fred |
| 4| 333333 | Null |
| 5| Null | Steve |
| 6| 666666 | Null |
如何在 SQL!
中得到这个结果目前最接近的尝试:
SELECT
table1.REPORTER,
TO_CHAR(NULL) "NAME"
FROM table1
Where table1.TYPE =1
AND table1.REPORTER <> '111111'
Union
SELECT
TO_CHAR(NULL) "REPORTER",
table2.STRINGVALUE "NAME"
FROM table2,
table1
WHERE table2.ISSUE = table1.ID
AND table2.fieldtype= 1
and table1.issuetype = 1
在没有明确排除默认 table 1 报告者的情况下,即使在 table 2.
中有名称值,我的结果中也会 returned我已经尝试 exists
并且无法获得正确的语法或正确的结果。一旦尝试任何连接 ID 和 Issue 值的连接,结果总是最终限制为匹配的行或所有值。并在 ON 中添加附加条件并没有 return 正确的结果。
我尝试了太多的排列,无法列出,从逻辑上讲,这听起来应该能够与存在的位置进行联合,或者左外连接,但我的技能不足以完成这项工作。
如果我没理解错的话,你想要一个left join
和count(distinct)
。我认为您正在寻找以下内容:
select count(distinct coalesce(stringval, reporter) )
from table1 t1 left join
table2 t2
on t1.id = t2.issue and t2.fieldtype = 1
where t1.id in (1, 4, 5, 6);
您需要学习如何使用显式 JOIN
语法。作为一个简单的规则:从不 在 FROM
子句中使用逗号。始终使用明确的 JOIN
语法。一方面,它更强大,可以轻松表达外连接。
您需要使用 LEFT JOIN
,这就是您指定 fieldtype = 1
子句的地方:
SELECT
table1.id,
CASE
WHEN table2.Stringvalue IS NOT NULL THEN table2.Stringvalue
ELSE table1.Reporter
END AS TheUser
FROM table1
LEFT JOIN table2 ON table1.id = table2.issue AND table2.fieldType = 1
WHERE table1.Type = 1
结果:
+------+---------+
| id | TheUser |
+------+---------+
| 1 | Fred |
| 4 | 333333 |
| 5 | Steve |
| 6 | 666666 |
+------+---------+