Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.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 出现在多个配置文件中的帐号数的不同计数_Sql_Sql Server_Distinct - Fatal编程技术网

Sql 出现在多个配置文件中的帐号数的不同计数

Sql 出现在多个配置文件中的帐号数的不同计数,sql,sql-server,distinct,Sql,Sql Server,Distinct,我有一个在account_num和user profiles profile_id中包含帐号的表。对于不同的配置文件,可以有相同帐号的多个实例,也可以有具有不同帐号的配置文件的多个实例。一个配置文件不能有多个具有相同帐号的实例 我正在尝试编写一个查询,该查询将为出现在多个配置文件中的帐号数量提供一个明确的计数 我正在尝试下面的查询,尽管建议更高效的查询将不胜感激 Select Count(*) from (select account_num, count(profile_id) as num_

我有一个在account_num和user profiles profile_id中包含帐号的表。对于不同的配置文件,可以有相同帐号的多个实例,也可以有具有不同帐号的配置文件的多个实例。一个配置文件不能有多个具有相同帐号的实例

我正在尝试编写一个查询,该查询将为出现在多个配置文件中的帐号数量提供一个明确的计数

我正在尝试下面的查询,尽管建议更高效的查询将不胜感激

Select Count(*) from
(select account_num, count(profile_id) as num_users
from dbo.sample
where account_num <> ''
group by account_num
)
where num_users >1
我正在使用Microsoft SQL Server Management Studio。顺便问一下,这个查询在Oracle服务器上会有所不同吗


任何帮助都将不胜感激。

尝试为子查询添加别名

 select Count(*) from 
    (   
        select account_num, count(profile_id) as num_users 
        from dbo.sample 
        where account_num <> '' group by account_num
    ) t 
 where num_users > 1

您想要的结果也可以通过此查询显示:

SELECT COUNT(DISTINCT account_num) AS cnt
FROM dbo.sample    a                        --- no AS here to work in Oracle
WHERE account_num <> ''
  AND profile_id IS NOT NULL
  AND EXISTS
      ( SELECT *
        FROM dbo.sample    b 
        WHERE b.account_num = a.account_num
          AND profile_id IS NOT NULL
          AND b.PK <> a.PK                  --- the PRIMARY KEY of the table
      ) ;

太简单了!Oracle DB上是否需要相同的别名?MySQL和SQL Server上肯定需要别名。我猜在甲骨文上也是这样,但我不确定。看起来甲骨文上没有必要这样做。看见它适用于Oracle,如果您将DB引擎更改为其他引擎,则不适用。这就可以解释了。我正在更改我在Oracle上运行的另一个查询,但不明白它为什么不起作用!谢谢大家的帮助。按帐号计算存款总额
SELECT COUNT(DISTINCT account_num) AS cnt
FROM dbo.sample    a                        --- no AS here to work in Oracle
WHERE account_num <> ''
  AND profile_id IS NOT NULL
  AND EXISTS
      ( SELECT *
        FROM dbo.sample    b 
        WHERE b.account_num = a.account_num
          AND profile_id IS NOT NULL
          AND b.PK <> a.PK                  --- the PRIMARY KEY of the table
      ) ;