Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.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/sqlite/3.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
Sqlite查询-具有第一列总和的列_Sql_Sqlite - Fatal编程技术网

Sqlite查询-具有第一列总和的列

Sqlite查询-具有第一列总和的列,sql,sqlite,Sql,Sqlite,假设我有这个疑问 select count(customerid) as Count_Own, (select count(customerid) from Customers) Count_ALL from Customers where EmployeeID = 1 它在第一列显示employeeid=1的客户数,在第二列显示所有员工数 现在我想编写类似的查询,而不是使用表Customers,我有一个大的子查询: select count(

假设我有这个疑问

select 
    count(customerid) as Count_Own,
    (select count(customerid) from Customers) Count_ALL 
from 
    Customers
where 
    EmployeeID = 1
它在第一列显示employeeid=1的客户数,在第二列显示所有员工数

现在我想编写类似的查询,而不是使用表
Customers
,我有一个大的子查询:

select 
    count(customerid) as Count_Own,
    (select count(customerid) from /*BIG QUERY*/) Count_ALL 
from 
    ( /*BIG QUERY*/   )
where 
    EmployeeID = 1
sqlite> with big_query as
   ...> ( select i,e from t ),
   ...> q1 as 
   ...> ( select count(*) as n, 1 as fake  from big_query ),
   ...> q2 as
   ...> ( select count(*) as n, 1 as fake from big_query where e=1)
   ...> select q2.n, q1.n
   ...> from q1 inner join q2
   ...> on q1.fake=q2.fake;
3|4

我现在对列
Count\u ALL
有一个问题,因为我想这样快速地计数,而不使用我的子查询。我想知道这是否只是一个简单的解决方案。

解决方法:创建一个假列以连接两个查询:

select 
  count(T1.customerid) as Count_Own, T2.Count_ALL
From ( Select 1 as joinField, --BIG QUERY--   ) T1
join (select count(customerid) Count_ALL, 1 as joinField  
      from customers)  T2
on T1.joinField = T2.joinField
where T1.EmployeeID = 1
已编辑到期评论

可以使用CTE避免两次编写大查询。让我们使用这个简化的模式:

dh4@GLOW:~/tmp$ sqlite3 pepe.sql
SQLite version 3.11.0 2016-02-15 17:29:24
Enter ".help" for usage hints.
sqlite> create table t ( i int , e int );
sqlite> insert into t values 
   ...> ( 1,1),
   ...> ( 2,1),
   ...> ( 3,1),
   ...> ( 1,2);
那么,您的查询是:

sqlite> with big_query as
   ...> ( select i,e from t )
   ...> select count(i), (select count(*) from big_query)
   ...> from big_query
   ...> where e=1;
3|4
此外,您还可以使用伪连接行程来避免子闸门子查询:

select 
    count(customerid) as Count_Own,
    (select count(customerid) from /*BIG QUERY*/) Count_ALL 
from 
    ( /*BIG QUERY*/   )
where 
    EmployeeID = 1
sqlite> with big_query as
   ...> ( select i,e from t ),
   ...> q1 as 
   ...> ( select count(*) as n, 1 as fake  from big_query ),
   ...> q2 as
   ...> ( select count(*) as n, 1 as fake from big_query where e=1)
   ...> select q2.n, q1.n
   ...> from q1 inner join q2
   ...> on q1.fake=q2.fake;
3|4
最后一种方法是使用
case

sqlite> select count( case when e = 1 then 1 else null end) , count(*) 
   ...> from t
   ...> ;
3|4

谢谢“假”加入是一个有趣的解决方案,但也许我写的不准确。我修正了一点例子。关键是我必须使用子查询来计算这两列。谢谢,伟大的解决方案