简体   繁体   English

sql或sqlite查询并在列表中获取结果

[英]sql or sqlite query and getting result in a list

I am new to sql and In my app I am using sqlite DB's and I have trouble in creating a query. 我是sql的新手,在我的应用程序中,我使用的是sqlite DB,我在创建查询时遇到了麻烦。

I have a DB with column names to be: 我有一个包含列名的数据库:

Name1  Name2  Score1  Score2 Winner  Aces1  Aces2

Imagine that thse are the stats of a single game, so the same name can be in name 1 or in name 2 in different rows. 想象一下,这是单个游戏的统计数据,因此同名可以在名称1中或在名称2中在不同的行中。

I want the following: 我想要以下内容:

a) Return in a list the player names with a descending number of aces. a)在列表中返回具有降序数量的ace的玩家名称。

For example imagine these rows: 例如想象这些行:

Nick George 39  35  Nick  2   4
Joe  Nick   17  39  Nick  1   0
George Jorge  11  39  Jorge  3  1 

Should give me in a list the following: //number or aces 应该在列表中给我以下内容://数字或者aces

George 7
Nick 2
Joe 1
Jorge 1

b)I want the same with number of wins. b)我希望获胜次数相同。

c)and the same avg points. c)和相同的平均点数。

Can you please give me the query? 你能问我一下吗?

Your problem here is a result of storing your data in a denormalised form. 这里的问题是以非规范化形式存储数据的结果。 If you read up on data normalisation , then you will store your data in a form that is more amenable to producing these queries easily. 如果您阅读了数据规范化 ,那么您将以更易于生成这些查询的形式存储数据。

select Winner, COUNT(*) from yourtable group by Winner order by COUNT(*) desc

and

select name, SUM(aces)
from
(
select Name1 as Name, Score1 as Score, Aces1 as Aces from yourtable
union all
select Name2 as Name, Score2 as Score, Aces2 as Aces from yourtable
) v
group by Name
order by SUM(aces) desc

Average points is left as an exercise for the reader. 平均积分留给读者练习。

选择名称,SUM(aces)来自(选择Name1作为名称,Score1作为分数,Aces1作为来自yourtable union的Aces全部选择Name2作为名称,Score2作为分数,Aces2作为来自yourtable的Aces)v group by Name order by SUM(aces)降序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM