0

我正在尝试编写一个 sql 查询,其中我需要按县划分的分数数 > 3,然后对于该列表上的县,我需要生成分数 < 3 的房间百分比。所以我需要三列,县名称,# of各县得分 > 3,各县得分 < 3 的房间百分比

    SELECT County = c.Description, [Score > 3] = count(s.Score),  
  ((select count(room.Name) where s.Score< 3) /( select count(room.Name) ) * 100)
    FROM Sites AS s
    inner join Profiles as p on s.Profile_Id = p.Id
    inner join Counties as c on p.County_Id = c.Id
    inner join Rooms as room on s.Id = room.Site_Id
    where s.Score > 3
    Group By c.Description
4

3 回答 3

0

利用Cast

SELECT County = c.Description, [Score > 3] = count(s.Score), 
    ( Cast(select count(room.Name) where s.Score < 3 ) as float / ( select count(room.Name) ) * 100)
    FROM Sites AS s
    inner join Profiles as p on s.Profile_Id = p.Id
    inner join Counties as c on p.County_Id = c.Id
    inner join Rooms as room on s.Id = room.Site_Id
    where s.Score > 3
    Group By c.Description
于 2013-03-26T17:36:41.663 回答
0
[Score > 3] = count(s.Score) 

...

where s.Score > 3

这两个语句都不需要。事实上,您的 Where 子句将所有操作限制为 s.score >3,当您还尝试从 score <3 中提取数据时,这并不理想。

如果您尝试计算 s.Score>3 和 <3 的情况,则需要使用 CASE 语句

SELECT SUM(CASE WHEN s.score < 3 THEN 1  ELSE 0 END) AS Hiscores,  
SUM(CASE WHEN s.score >    3 THEN 1  ELSE 0 END) /count(s.scores) AS percentLowScores

这应该这样做

  SELECT c.Description County, 
  SUM(CASE WHEN s.score < 3 THEN 1  ELSE 0 END) AS Hiscores,  
  SUM(CASE WHEN s.score > 3 THEN 1  ELSE 0 END) /count(s.scores) AS percentLowScores

    FROM Sites AS s
    inner join Profiles as p on s.Profile_Id = p.Id
    inner join Counties as c on p.County_Id = c.Id
    inner join Rooms as room on s.Id = room.Site_Id

    Group By c.Description
于 2013-03-26T17:33:41.593 回答
0

我认为你把问题复杂化了,而不是子选择,你可以限制使用HAVING子句返回的数据,然后CASECOUNT

SELECT  County = c.Description,
        [Score > 3] = COUNT(CASE WHEN Sites.Score > 3 THEN 1 END),
        [% Score < 3] = 100.0 * COUNT(CASE WHEN Sites.Score < 3 THEN 1 END) / COUNT(1)
FROM    Sites
        INNER JOIN Profiles
            ON Sites.Profile_Id = Profiles.Id
        INNER JOIN Counties 
            ON Profiles.County_Id = Counties.Id
        INNER JOIN Rooms 
            ON Sites.Id = Rooms.Site_Id
GROUP BY c.Description
HAVING  COUNT(CASE WHEN Sites.Score > 3 THEN 1 END) > 0;

SQL Fiddle 演示

编辑

SELECT  County = c.Description,
        [Score > 3] = COUNT(CASE WHEN Sites.Score > 3 THEN 1 END),
        [% Score < 3] = 100.0 * SUM(CASE WHEN Sites.Score < 3 THEN 1 END) / COUNT(*),
        [Score > 3] = SUM(CASE WHEN Sites.Score > 3 THEN RoomCount ELSE 0 END),
        [% Score < 3] = 100.0 * SUM(CASE WHEN Sites.Score < 3 THEN RoomCount ELSE 0 END) / SUM(RoomCount)
FROM    Sites
        INNER JOIN Profiles
            ON Sites.Profile_Id = Profiles.Id
        INNER JOIN Counties 
            ON Profiles.County_Id = Counties.Id
        INNER JOIN 
        (   SELECT Site_Id, RoomCount = COUNT(*)
            FROM   Rooms 
            GROUP BY Site_Id
        ) Rooms
            ON Sites.Id = Rooms.Site_Id
GROUP BY c.Description
HAVING  COUNT(CASE WHEN Sites.Score > 3 THEN 1 END) > 0;
于 2013-03-26T18:02:38.967 回答