Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.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_Count - Fatal编程技术网

我如何将另一个SQL计数嵌套到其中?

我如何将另一个SQL计数嵌套到其中?,sql,count,Sql,Count,我的代码按计划运行。告诉我“来源”以及每个来源的应用程序数量 SELECT source, COUNT(1) as Cnt FROM apps WHERE monthyear = 'October2018' GROUP BY source 输出为['Store1',1],'Store2',5],'Store3',3] 在同一个表中,我有另一行名为“sell”,我想在每个存储中计算卖出的数量=1,因此我得到以下输出: 来源,应用程序,销售['Store1',1,0],'

我的代码按计划运行。告诉我“来源”以及每个来源的应用程序数量

SELECT source, COUNT(1) as Cnt
    FROM apps
    WHERE monthyear = 'October2018' 
    GROUP BY source
输出为['Store1',1],'Store2',5],'Store3',3]

在同一个表中,我有另一行名为“sell”,我想在每个存储中计算卖出的数量=1,因此我得到以下输出:

来源,应用程序,销售['Store1',1,0],'Store2',5,3],'Store3',3, 1]


如果我理解正确,那么在
apps
表中有一列名为
seld
,它可能是一个布尔值,您希望现有查询返回设置了
seld
标志的每个不同
值的记录数。在这种情况下,您需要一个条件计数,如下所示:

select
    source,
    count(1) as Cnt,
    count(case when Sold = 1 then 1 end) as CountSold
from
    apps
where
    monthyear = 'October2018' 
group by
    source;

然后您只需更新代码,将新的
countsell
值包含在您想要的任何位置。如果我误解了您的问题,请告诉我。

您还有一行名为
已售出
?或者你指的是一张桌子?那张桌子的结构是什么?您如何将数据从it连接到
应用程序
表格?完美!非常感谢。