简体   繁体   中英

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.

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.

I want the following:

a) Return in a list the player names with a descending number of aces.

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

George 7
Nick 2
Joe 1
Jorge 1

b)I want the same with number of wins.

c)and the same avg points.

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)降序

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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