0

郡ごとに3を超えるスコアの数が必要なSQLクエリを作成しようとしています。次に、そのリストの郡について、スコアが3未満の部屋のパーセンテージを生成する必要があります。したがって、3つの列、郡名、#郡別スコア>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
[Score > 3] = count(s.Score) 

...

where s.Score > 3

両方のステートメントは必要ありません。実際、Where句は、すべての操作をs.score> 3に制限します。これは、スコア<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

使用する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

副選択ではなく、HAVING句を使用して返されるデータを制限CASEし、COUNT:で使用することで、問題を複雑にしすぎていると思います。

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フィドルのデモ

編集

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 に答える