Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > General > Chit Chat
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Chit Chat The place for open discussions.

SQLite newbie question

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-24-2008, 09:47 AM
Junior Member
 
Join Date: Mar 2008
Location: Thailand
Posts: 46
Default SQLite newbie question

I am using an SQLite database to store chess game data from a pgn file. I have a table ("Gamestable") in which each record stores data for one game. The data includes "Event", the "Eventdate" and the name of the "White" player. I wish to summarise the table by unique event (that is event||eventdate), giving for each unique event the number of different white players and the number of games.

The way I have managed to do this is by creating two temporary tables to find the number of players for each event. The code looks something like this:

Quote:
CREATE TABLE eview (event1 CHAR(20), Name CHAR(30))

INSERT INTO eview (event1, Name) Select Event||Eventdate as event1, White from Gamestable group by event1, Name

CREATE TABLE eview2 (event1 CHAR(20), NrP SMALLINT)

INSERT INTO eview2 (event1, NrP) Select event1, count() as NrP from eview group by event1

CREATE TABLE summaryevents (Event CHAR(20), NrPlayers SMALLINT, NrGames SMALLINT, EventDate CHAR(10), Qevent CHAR(30))

INSERT INTO summaryevents (Event, NrPlayers, NrGames, EventDate, Qevent) Select Event, NrP, count() as NrGames, EventDate, Event||Eventdate As Qevent from Gamestable, eview2 where event1=Event||Eventdate group by Qevent

DROP TABLE eview

DROP TABLE eview2
It works, but I'm sure there's a much better and faster way of doing it.

Any suggestions would be appreciated!

Thanks

James

Last edited by JamesC : 07-24-2008 at 01:39 PM. Reason: Removed the b4ppc code from the SQL
Reply With Quote
  #2 (permalink)  
Old 08-01-2008, 09:26 AM
Junior Member
 
Join Date: Mar 2008
Location: Thailand
Posts: 46
Smile Problem Solved

The rather simple solution is to use the DISTINCT keyword. The original SQL can be replaced with:

Quote:
CREATE TABLE summaryevents (Event CHAR(20), NrPlayers SMALLINT, NrGames SMALLINT, EventDate CHAR(10), Qevent CHAR(30))

INSERT INTO summaryevents (Event, NrPlayers, NrGames, EventDate, Qevent) Select Event, Count(DISTINCT White) as NrP, count() as NrGames, EventDate, Event||Eventdate As Qevent from Gamestable group by Qevent
Useful to know, and about 100 times faster!

James
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie Question Stevenator65 Questions (Windows Mobile) 9 05-30-2008 09:53 PM
SQLite external library newbie question JamesC Questions (Windows Mobile) 2 05-29-2008 04:48 AM
Newbie question - arrays and tables BjornF Questions (Windows Mobile) 3 05-15-2008 10:29 AM
Another newbie question: How to refere to the active form? yildi Questions (Windows Mobile) 2 11-17-2007 08:55 PM
Newbie - question about loadXML yildi Questions (Windows Mobile) 6 11-13-2007 12:43 AM


All times are GMT. The time now is 09:52 PM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0