我有以下数据:
Server AvgValue
Server1 1.29
Server2 0.2
Server3 1.74
Server4 0.58
Server5 2.06
Server6 2.82
我想显示具有以下内容的服务器数量:
有什么建议吗?
到目前为止,Alberto提供了最好的选择,并回答了我原来的问题。然而,我遇到了多次列出服务器的情况,我想平均AvgValue并确保服务器只在该范围内计算一次。想法?
答案 0 :(得分:0)
您可以在单个查询中执行此操作,例如
select server,
count(Server) as NUmber_Of_Server,
sum(floor(AvgValue)) as Avg_Score
from tab group by Server
在这里看一个演示小提琴
答案 1 :(得分:0)
我认为最简单的方法是使用这样的查询:
SELECT Server, COUNT(*) AS ServersCount
FROM ServersTable
WHERE AvgValue < 10
GROUP BY Server
您没有提及有关您正在使用的RDBMS的任何信息。如果您使用的是MySQL,请查看有关COUNT()。
的文档必须针对每个条件修改此查询。然后,您将必须执行五个不同的查询。
否则,您可以使用以下查询:
SELECT ServersWithAvgValueLT10 = SUM(CASE WHEN AvgValue < 10 THEN 1 ELSE 0 END),
ServersWithAvgValueGT10LT30 = SUM(CASE WHEN AvgValue > 10 AND AvgValue < 30 THEN 1 ELSE 0 END),
ServersWithAvgValueGT30LT50 = SUM(CASE WHEN AvgValue > 30 AND AvgValue < 50 THEN 1 ELSE 0 END),
ServersWithAvgValueGT50LT80 = SUM(CASE WHEN AvgValue > 50 AND AvgValue < 80 THEN 1 ELSE 0 END),
ServersWithAvgValueGT80 = SUM(CASE WHEN AvgValue > 80 THEN 1 ELSE 0 END),
FROM ServersTable
答案 2 :(得分:0)
在SQL服务器中,我们可以这样做 -
DECLARE @lowerLimit int
DECLARE @upperLimit int
SET @lowerLimit = 0
SET @upperLimit = 1
SELECT
[Server]
,[AvgValue]
FROM [Services]
WHERE ([AvgValue] < @upperLimit
AND [AvgValue] > @lowerLimit)
以下是为SQL Server创建示例表的查询 -
CREATE TABLE [dbo].[Services](
[Id] [int] NULL,
[Server] [varchar](25) NULL,
[AvgValue] [decimal](18, 2) NULL
)
INSERT [dbo].[Services] ([Id], [Server], [AvgValue])
VALUES (1, N'Server1', CAST(1.29 AS Decimal(18, 2)))
INSERT [dbo].[Services] ([Id], [Server], [AvgValue])
VALUES (2, N'Server2', CAST(0.20 AS Decimal(18, 2)))
INSERT [dbo].[Services] ([Id], [Server], [AvgValue])
VALUES (3, N'Server3', CAST(1.74 AS Decimal(18, 2)))
INSERT [dbo].[Services] ([Id], [Server], [AvgValue])
VALUES (4, N'Server4', CAST(0.58 AS Decimal(18, 2)))
INSERT [dbo].[Services] ([Id], [Server], [AvgValue])
VALUES (5, N'Server5', CAST(2.06 AS Decimal(18, 2)))
INSERT [dbo].[Services] ([Id], [Server], [AvgValue])
VALUES (6, N'Server6', CAST(2.82 AS Decimal(18, 2)))