SQL查询不会给出明显的价值和其他问题

时间:2012-02-02 15:34:53

标签: sql sum distinct

我正在进行一个SQL查询,以显示每个用户的几个用户得分的高分列表,这也应该给出特定用户已经获得的avarage星,达到MAX级别,我还想进行计算通过总计每个级别的所有时间,用户在游戏中花费的总时间..

我有以下问题:

1:我的SQL查询没有给每个用户一行。

2:ginving的总和不是每个用户,而是所有用户的总和。

3:avarage STARS不显示小数。

4:我无法获得“score.time_spent”来计算特定用户一直在玩的所有时间的总和

我在当前时间的SQL查询如下所示:

SELECT DISTINCT
game_users.username AS 'User Name',
'Level Reached' = (SELECT DISTINCT MAX(score.game_level) FROM score
        WHERE score.game_users_id IN
        (SELECT game_users_id FROM game_users
        WHERE game_users.id = score.game_users_id)),
score.time_spent AS 'Total Time Spent',
'Stars' = (SELECT DISTINCT AVG(score.stars) FROM score
        WHERE score.game_users_id IN
        (SELECT game_users_id FROM game_users
        WHERE game_users.id = score.game_users_id)),
'High Score' = (SELECT DISTINCT SUM(score.game_level_score) FROM score
        WHERE score.game_users_id IN
        (SELECT game_users_id FROM game_users
        WHERE game_users.id = score.game_users_id)),
game_users.current_state AS 'Online State'          
FROM
game_users
INNER JOIN score ON game_users.id = score.game_users_id
GROUP BY
game_users.id,
game_users.username,
score.game_level_score,
score.game_level,
score.time_spent,
score.stars,
game_users.current_state
GO

“game_users”表格如下所示:

id                      int     Unchecked
firstname       varchar(100)    Checked
lastname        varchar(100)    Checked
email           varchar(100)    Checked
username        varchar(50)     Checked
usr_pass        varchar(50)     Checked
current_state           int     Checked

“得分”表格如下所示:

id                         int      Unchecked
game_users_id              int      Unchecked
game_level          varchar(4)      Checked
game_level_score    numeric(18, 0)  Checked
stars                      int      Checked
time_spent             time(7)      Checked

有人可以帮我解决这个烂摊子吗? : - )

最好的问候

Stig: - )

2 个答案:

答案 0 :(得分:1)

尝试在子查询中执行聚合。我不确定这是否正是您所需要的,但它应该让您朝着正确的方向前进:

SELECT  Game_Users.userName,
        MaxLevel,
        TimeSpent,
        Stars,
        HighScore,
        Game_Users.Current_State [Online State]
FROM    game_users
        INNER JOIN
        (   SELECT  Game_users_ID, 
                    MAX(Game_Level) [MaxLevel], 
                    AVG(Stars * 1.0) [Stars], 
                    SUM(game_level_Score) [HighScore],
                    SUM(DATEDIFF(SECOND, 0, CAST(GETDATE() AS TIME))) [TimeSpent]
            FROM    Score
            GROUP BY Game_Users_ID
        ) score
            ON score.Game_Users_ID = game_users.ID

答案 1 :(得分:0)

看起来应该解决它:

SELECT a.username AS 'User Name'
    , MAX(b.game_level) AS 'Level Reached'
    , CAST(DATEADD(millisecond,SUM(DATEDIFF(millisecond,0,CAST(b.time_spent AS DATETIME))),0) AS TIME) AS 'Total Time Spent'
    , AVG(CAST(b.stars AS FLOAT)) AS 'Stars'
    , SUM(b.game_level_score) AS 'High Score'
    , a.current_state AS 'Online State'
FROM game_users a
JOIN score b
    ON a.id = b.game_users_id
GROUP BY a.username, a.current_state

注意:CAST行来自此网站 - http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=121027

编辑:铸造星星漂浮。