【问题标题】:Creating a sheet from sql requests从 sql 请求创建工作表
【发布时间】:2020-10-20 13:39:56
【问题描述】:

我正在请求从游戏数据库中获取值,以帮助开发人员根据每 10 个级别使用的符文和符文升级进行平衡更改,所以为此,我为每 10 个级别创建了一个请求,并且我将 var 更改为 runeID(总共有 60 个 rune,所以我这样做了 60 次),并且对于每个结果,我手动填写了一个谷歌表。 我想知道是否可以在一个请求中直接创建所有这些请求,所以我可以复制列并将它们全部过去,这将使我赢得很多时间,即使可能使所有符文都相同时间,所以一个简单的复制粘贴和所有的数据都在那里

这是我用来每 10 级获取符文值的代码,从 1 级到 130 级,130 级之后都在同一个请求中

declare @runeID varchar(100)
set @runeID=22001

select counT(i.characterid) as 'user level 1 to 10', avg(i.maxUpgrade) as 'average level' from items i inner join characters c on i.characterId=c.characterId 
        where itemId=@runeID and level>0 and level<11 and attached>0
select counT(i.characterid) as 'user level 11 to 20', avg(i.maxUpgrade) as 'average level' from items i inner join characters c on i.characterId=c.characterId 
        where itemId=@runeID and level>10 and level<21 and attached>0
select counT(i.characterid) as 'user level 21 to 30', avg(i.maxUpgrade) as 'average level' from items i inner join characters c on i.characterId=c.characterId 
        where itemId=@runeID and level>20 and level<31 and attached>0
select counT(i.characterid) as 'user level 31 to 40', avg(i.maxUpgrade) as 'average level' from items i inner join characters c on i.characterId=c.characterId 
        where itemId=@runeID and level>30 and level<41 and attached>0
select counT(i.characterid) as 'user level 41 to 50', avg(i.maxUpgrade) as 'average level' from items i inner join characters c on i.characterId=c.characterId 
        where itemId=@runeID and level>40 and level<51 and attached>0
select counT(i.characterid) as 'user level 51 to 60', avg(i.maxUpgrade) as 'average level' from items i inner join characters c on i.characterId=c.characterId 
        where itemId=@runeID and level>50 and level<61 and attached>0
select counT(i.characterid) as 'user level 61 to 70', avg(i.maxUpgrade) as 'average level' from items i inner join characters c on i.characterId=c.characterId 
        where itemId=@runeID and level>60 and level<71 and attached>0
select counT(i.characterid) as 'user level 71 to 80', avg(i.maxUpgrade) as 'average level' from items i inner join characters c on i.characterId=c.characterId 
        where itemId=@runeID and level>70 and level<81 and attached>0
select counT(i.characterid) as 'user level 81 to 90', avg(i.maxUpgrade) as 'average level' from items i inner join characters c on i.characterId=c.characterId 
        where itemId=@runeID and level>80 and level<91 and attached>0
select counT(i.characterid) as 'user level 91 to 100', avg(i.maxUpgrade) as 'average level' from items i inner join characters c on i.characterId=c.characterId 
        where itemId=@runeID and level>90 and level<101 and attached>0
select counT(i.characterid) as 'user level 101 to 110', avg(i.maxUpgrade) as 'average level' from items i inner join characters c on i.characterId=c.characterId 
        where itemId=@runeID and level>100 and level<111 and attached>0
select counT(i.characterid) as 'user level 111 to 120', avg(i.maxUpgrade) as 'average level' from items i inner join characters c on i.characterId=c.characterId 
        where itemId=@runeID and level>110 and level<121 and attached>0
select counT(i.characterid) as 'user level 121 to 130', avg(i.maxUpgrade) as 'average level' from items i inner join characters c on i.characterId=c.characterId 
        where itemId=@runeID and level>120 and level<131 and attached>0
select counT(i.characterid) as 'user level 131+', avg(i.maxUpgrade) as 'average level' from items i inner join characters c on i.characterId=c.characterId 
        where itemId=@runeID and level>130 and attached>0

所以现在这个查询给了我 14 个结果(我只能将它们复制 2 个信息乘 2 个信息、使用次数和每 10 个级别的平均级别),所以总共 28 个信息需要手动填充 1 个符文。 所以再次,我在这里看看是否有可能至少能够将所有这 28 条信息放在一个 2 列表中,我可以直接复制所有 28 条信息,甚至可以输入更大的信息所有符文 ID 都是一个数组,它用所有符文做一个巨大的表格,每个符文 2 列

【问题讨论】:

  • 使用 GROUP BY 执行一次 SELECT。
  • 我查看了关于 group by 的 sql 文档,但我真的不明白如何在我的情况下使用它
  • 这是this的副本?
  • 不是真正的重复,在这里我得到了一个关于为单个符文重新组合结果的解决方案,而另一方面,我希望在一个查询中使用一段时间来完成所有符文,并通过如果我必须添加更多符文,这更容易更新代码,只需将符文 id 添加到数组中

标签: sql sql-server


【解决方案1】:

你能做到吗?

select floor((level - 1) / 10) * 10 as range_start,
       count(*) as num_user,
       avg(i.maxUpgrade) as avg_level
from items i inner join
     characters c
     on i.characterId = c.characterId
where attached > 0
group by floor((level - 1) / 10) * 10
order by nmin(level);

这不会将 130 以上的所有内容放在同一个组中,但这是对查询的简单调整。

【讨论】:

  • 我试图得到的是具有特定符文 id 的行数和该符文的平均级别,这里我没有得到符文的数量,也适用于“range_start”,它从 0 开始,然后是 130,然后是 50,等等
  • 我添加了count(i.characterId) as 'number of user', 来获取用户数量,但是如何按级别范围 asc 分组?
  • 也需要 1 到 9 级,然后是 10 到 19 等。但我想要 1 到 10、11 到 20 等...
  • 我使用了ORDER BY range_start ASC,但范围仍然是错误的,因为它是 1-9、10-19 等而不是 1-10、11-20 等
  • @SamyPereger 。 . .范围问题很容易通过减去 1 来解决。
【解决方案2】:

这是另一种方法

select lvl.lvl as range_start,
       avg(i.maxUpgrade) as tot_avg_level,
       count(i.characterId) as tot_num_users
from items i 
     join characters c on i.characterId = c.characterId
     cross apply (select (c.[level]/10)*10 lvl) lvl
where attached > 0
group by lvl.lvl
order by lvl.lvl;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-13
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-08
    相关资源
    最近更新 更多