如何计算已执行的 SQL 查询的不同结果?
How to count distinct results from a already executed SQL Query?
select application from l3_usage
where function in ('dbms', 'web', 'app')
group by application, function
order by application
这是我执行上述查询时的结果图像。
我现在想统计应用程序出现的次数。例如:
预算 : 3
企业社会责任:3
飞行管理系统:3
设施 : 1
库存:3
等...
我试过了
select application, count(application) from l3_usage
where function in ('dbms', 'web', 'app')
group by application
order by application
但我得到的随机计数与我正在寻找的计数无关。
有人能帮忙吗?谢谢!
编辑:::::
我这样做的最终目的是只获取计数值小于 3 的应用程序。
在这种情况下,应该 return 只是 "Facilities" 和 "SCM"。
SELECT application,COUNT(CASE
WHEN application IS NOT NULL THEN 1
END)
FROM table group by application
我从 here
中找到参考
编辑
select * from(
SELECT typing,COUNT(CASE
WHEN typing IS NOT NULL THEN 1
END) as counts
FROM ss group by typing ) abc where counts<3
试试这个查询:
SELECT x.application, COUNT(*) As Frequency
FROM
(
SELECT application
FROM l3_usage
WHERE function IN ('dbms', 'web', 'app')
GROUP BY application, function
ORDER BY application
) AS x
GROUP BY x.application
HAVING COUNT(*) < 3
ORDER BY COUNT(*)
我查询了您在答案中发布的结果集,并获得了每个组的COUNT
。请注意,此 COUNT
是每个 application
组的唯一 function
值的数量。你应该得到这个结果:
+-------------+-----------+
| application | Frequency |
+-------------+-----------+
| Facilities | 1 |
| SCM | 1 |
+-------------+-----------+
更新:
一种更简单的查询方法是:
SELECT application, COUNT(DISTINCT function) As Frequency
FROM l3_usage
WHERE function IN ('dbms', 'web', 'app')
GROUP BY application
HAVING COUNT(DISTINCT function) < 3
ORDER BY application
您一开始的想法是正确的,但您需要 SELECT
每个应用程序组的不同 function
数量。
我已经使用了我连接到您的查询中的子查询。如果您想要不同的行数,请在 @count query
中使用 count(*)
前面的 distinct
declare @count nvarchar(max)= (select count(*)
from l3_usage
where function in ('dbms', 'web', 'app'))
declare @sql nvarchar(max) = 'select a,count(*),'+@count+' as count from l3_usage
where function in (''dbms'', ''web'', ''app'') '
exec(@sql)
select application from l3_usage
where function in ('dbms', 'web', 'app')
group by application, function
order by application
这是我执行上述查询时的结果图像。
我现在想统计应用程序出现的次数。例如: 预算 : 3 企业社会责任:3 飞行管理系统:3 设施 : 1 库存:3
等...
我试过了
select application, count(application) from l3_usage
where function in ('dbms', 'web', 'app')
group by application
order by application
但我得到的随机计数与我正在寻找的计数无关。
有人能帮忙吗?谢谢!
编辑:::::
我这样做的最终目的是只获取计数值小于 3 的应用程序。
在这种情况下,应该 return 只是 "Facilities" 和 "SCM"。
SELECT application,COUNT(CASE
WHEN application IS NOT NULL THEN 1
END)
FROM table group by application
我从 here
中找到参考编辑
select * from(
SELECT typing,COUNT(CASE
WHEN typing IS NOT NULL THEN 1
END) as counts
FROM ss group by typing ) abc where counts<3
试试这个查询:
SELECT x.application, COUNT(*) As Frequency
FROM
(
SELECT application
FROM l3_usage
WHERE function IN ('dbms', 'web', 'app')
GROUP BY application, function
ORDER BY application
) AS x
GROUP BY x.application
HAVING COUNT(*) < 3
ORDER BY COUNT(*)
我查询了您在答案中发布的结果集,并获得了每个组的COUNT
。请注意,此 COUNT
是每个 application
组的唯一 function
值的数量。你应该得到这个结果:
+-------------+-----------+
| application | Frequency |
+-------------+-----------+
| Facilities | 1 |
| SCM | 1 |
+-------------+-----------+
更新:
一种更简单的查询方法是:
SELECT application, COUNT(DISTINCT function) As Frequency
FROM l3_usage
WHERE function IN ('dbms', 'web', 'app')
GROUP BY application
HAVING COUNT(DISTINCT function) < 3
ORDER BY application
您一开始的想法是正确的,但您需要 SELECT
每个应用程序组的不同 function
数量。
我已经使用了我连接到您的查询中的子查询。如果您想要不同的行数,请在 @count query
中使用count(*)
前面的 distinct
declare @count nvarchar(max)= (select count(*)
from l3_usage
where function in ('dbms', 'web', 'app'))
declare @sql nvarchar(max) = 'select a,count(*),'+@count+' as count from l3_usage
where function in (''dbms'', ''web'', ''app'') '
exec(@sql)