Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何对子查询执行GROUPBY函数?_Sql_Oracle_Select_Join_Group By - Fatal编程技术网

Sql 如何对子查询执行GROUPBY函数?

Sql 如何对子查询执行GROUPBY函数?,sql,oracle,select,join,group-by,Sql,Oracle,Select,Join,Group By,我正在使用一个包含联系人、消息、收件人和对话的数据库 我使用用户ID作为标准在联系人和收件人之间执行了完全外部联接。这将给出一个包含这些值的表格;用户ID、FNAME、LNAME、单元格、城市、国家、MSGID、用户ID、读取时间。由于两个表都有此值,因此user_ID有重复的值。我的子查询在自己完成时给出正确的结果: SELECT * FROM Contacts FULL JOIN Recipients ON Contacts.user_ID=Recipients.user_ID; 在我这样

我正在使用一个包含联系人、消息、收件人和对话的数据库

我使用用户ID作为标准在联系人和收件人之间执行了完全外部联接。这将给出一个包含这些值的表格;用户ID、FNAME、LNAME、单元格、城市、国家、MSGID、用户ID、读取时间。由于两个表都有此值,因此user_ID有重复的值。我的子查询在自己完成时给出正确的结果:

SELECT *
FROM Contacts
FULL JOIN Recipients
ON Contacts.user_ID=Recipients.user_ID;
在我这样做之后,我想通过计算每个用户ID的每个msgID来预成型一个组,以给出用户ID的最终结果和每个用户获得的消息数

我似乎不能得到正确的语法,因为我一直得到一个错误

SQL> SELECT user_ID, count(msgID)
FROM (  SELECT *
    FROM Contacts
    FULL JOIN Recipients
    ON Contacts.user_ID=Recipients.user_ID)
Group by Contacts.user_ID; 

Group by Contacts.user_ID
         *
ERROR at line 6:
ORA-00904: "CONTACTS"."USER_ID": invalid identifier
我尝试过“Contacts.user\u ID”和简单的“user\u ID”,但它不喜欢这两种变体


谢谢。

我猜你想要这个:

SELECT COALESCE(c.user_ID, r.user_ID), COUNT(*)
FROM Contacts c FULL JOIN
     Recipients r
     ON c.user_ID = r.user_ID
GROUP BY COALESCE(c.user_ID, r.user_ID);

子查询不是必需的。

我猜您希望这样:

SELECT COALESCE(c.user_ID, r.user_ID), COUNT(*)
FROM Contacts c FULL JOIN
     Recipients r
     ON c.user_ID = r.user_ID
GROUP BY COALESCE(c.user_ID, r.user_ID);

子查询不是必需的。

谢谢!这正是我想要的!非常感谢。这正是我想要的!