Re: [sqlite] Multitable query question
I read the replies in this thread on the sqlite mailing list. It's true that the data in the two tables are not really related in the relational sense, where one table contains a foreign key, which is a key in another table. This implies a one to many relationship -- for example, many calls relating to a given letter and many letters relating to a given house. When the same foreign key (houseid) occurs in both tables, the best you can hope for is to join all the values in eacj row of one table with all the values each row of the other which share the same foreign key. I thought about the problem of displaying "unrelated data" side by side in sql queries. But first a quick and dirty solution, if all you want is a reference. This also shows how some sorts of formatting can be done directly in a query. The data appears in it's own column, but not side by side. Also, using the standard date data type is really the *best* way to store dates. .separator "" .mode list select "House 16: Letters Calls"; select "--"; select " ", date from letters where houseid = 16; select " ", date from calls where houseid = 16; To create a side by side report in SQLite, you would need to create a relationship between the columns you want to display. This can be done by creating temporary tables with auto-incrementing primary keys. Select the unrelated data you want to display into these tables, as well as a number of blank rows into each table so there can be columns of different lengths. Now, you have a relationship between the data based on the auto-incrementing key (id, for instance). Do a select from columns in these tables "where table1.id = table2.id and table1.id = table3.id etc. limit 40" (or whatever is the maximum length of your report. If somebody has found another way to do what you want, at least this technique can be used when the data is completely unrelated and you want a report which can be done completely in SQLite. ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] Multitable query question
On Thu, May 29, 2008 at 1:09 AM, <[EMAIL PROTECTED]> wrote: > I've implemented this behaviour in my program. I was just curious whether > it was possible in sql when I learnt about the Left Join (as there are more > values in one column than the other). > > I guess it isn't or is not really the proper use of the database. Thanks > very much for the prompt replies though. > I'll also have to sort out the date entry bit. Ah, okay :) Cool. Just for the heck of it, I decided to attempt what you were going for. I do not, repeat, I do NOT recommend using this technique. I'm just proving what SQLite is capable of if you put a sufficiently twisted mind towards it :) CREATE TABLE Calls (id integer primary key, houseId integer, date date); CREATE TABLE Letters (id integer primary key, houseId integer, date date); CREATE TABLE callSeq (sequence integer primary key, date date); CREATE TABLE letterSeq (sequence integer primary key, date date); CREATE INDEX Calls_HouseId on Calls(houseId); CREATE INDEX Letters_HouseId on Letters(houseId); sqlite> .dump BEGIN TRANSACTION; CREATE TABLE Calls (id integer primary key, houseId integer, date date); INSERT INTO "Calls" VALUES(1,1,'2008-05-15'); INSERT INTO "Calls" VALUES(9,1,'2008-05-28'); INSERT INTO "Calls" VALUES(10,1,'2008-05-28'); INSERT INTO "Calls" VALUES(24,16,'2008-05-15'); INSERT INTO "Calls" VALUES(27,16,'2008-05-15'); INSERT INTO "Calls" VALUES(31,16,'2008-05-15'); CREATE TABLE Letters (id integer primary key, houseId integer, date date); INSERT INTO "Letters" VALUES(1,16,'2008-05-26'); INSERT INTO "Letters" VALUES(3,16,'2008-05-27'); INSERT INTO "Letters" VALUES(4,16,'2008-05-28'); INSERT INTO "Letters" VALUES(7,16,'2008-05-16'); CREATE INDEX Calls_HouseId on Calls(houseId); CREATE INDEX Letters_HouseId on Letters(houseId); COMMIT; And here's the magic: create temporary table letterSeq (sequence integer primary key, date date); create temporary table callSeq (sequence integer primary key, date date); insert into letterSeq (date) select date from Letters where houseId=16 order by date desc; insert into callSeq (date) select date from Calls where houseId=16 order by date desc; insert into callSeq (date) select NULL from Letters where (select count(*) from callSeq) < (select count(*) from letterSeq); select l.date as LetterDate, c.date as CallDate from callSeq c join letterSeq l on c.sequence = l.sequence order by c.sequence; Let the IOSQC (International Obfuscated SQLite Querying Contest) begin! -- -- Stevie-O Real programmers use COPY CON PROGRAM.EXE ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] Multitable query question
Stephen Oberholtzer wrote: > > On Wed, May 28, 2008 at 11:15 PM, beejayzed <[EMAIL PROTECTED]> wrote: > >> >> I have two tables, one which has each visit(call) on a house logged, >> another >> which has each letter sent to a house logged. >> I would like to create a query which has two columns, Letters.Date and >> Calls.Date, for a specific HouseID. >> >> So how do I get the desired result? >> This would be: >> 26/5/08 15/5/08 >> 27/5/08 15/5/08 >> 28/5/08 15/5/08 >> 16/5/08 >> >> > Before anything else: Use ISO date formats, e.g. 2008-05-26 instead of > 26/5/08. Not only is it reasonably unambiguous (e.g. to me, 12/5/08 is > December 5th), but when you sort them as strings they also sort in > date/time > order. > > Secondly, glancing over your request, it seems that you have confused a > database table with the generic tabular data layout you might achieve > using > e.g. HTML. > Each row of a table AKA 'relation' contains related information. What you > seem to want, however, is completely different: You seem to actually want > two *different* sets of data, that have nothing to do with each other, and > display them in columns side-by-side. > > The proper way to do this is to have your application pull back all of the > letter dates in one query, then all of the visit dates in a second query, > and then piece them together for display purposes outside of SQL. > > > -- > -- Stevie-O > Real programmers use COPY CON PROGRAM.EXE > ___ > sqlite-users mailing list > sqlite-users@sqlite.org > http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users > > Oops.. I sent the reply to Stephen by mistake. Anyway, I'd created this behaviour in my program, but I was just curious whether it could be done in an sql query as well. I guess it's not really suitable for it. Thanks for the prompt replies. Also thanks for the suggestion about the dates. -- View this message in context: http://www.nabble.com/Multitable-query-question-tp17526832p17527752.html Sent from the SQLite mailing list archive at Nabble.com. ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] Multitable query question
On Wed, May 28, 2008 at 11:15 PM, beejayzed <[EMAIL PROTECTED]> wrote: > > I have two tables, one which has each visit(call) on a house logged, > another > which has each letter sent to a house logged. > I would like to create a query which has two columns, Letters.Date and > Calls.Date, for a specific HouseID. > > So how do I get the desired result? > This would be: > 26/5/08 15/5/08 > 27/5/08 15/5/08 > 28/5/08 15/5/08 > 16/5/08 > > Before anything else: Use ISO date formats, e.g. 2008-05-26 instead of 26/5/08. Not only is it reasonably unambiguous (e.g. to me, 12/5/08 is December 5th), but when you sort them as strings they also sort in date/time order. Secondly, glancing over your request, it seems that you have confused a database table with the generic tabular data layout you might achieve using e.g. HTML. Each row of a table AKA 'relation' contains related information. What you seem to want, however, is completely different: You seem to actually want two *different* sets of data, that have nothing to do with each other, and display them in columns side-by-side. The proper way to do this is to have your application pull back all of the letter dates in one query, then all of the visit dates in a second query, and then piece them together for display purposes outside of SQL. -- -- Stevie-O Real programmers use COPY CON PROGRAM.EXE ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] Multitable query question
"beejayzed" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > I have two tables, one which has each visit(call) on a house logged, > another which has each letter sent to a house logged. > I would like to create a query which has two columns, Letters.Date and > Calls.Date, for a specific HouseID. What for? Why do you want to put two completely unrelated values into the same row? This makes no sense from relational calculus standpoint, and that's why you have a hard time expressing such a beast in SQL. What are you trying to achieve? Igor Tandetnik ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
[sqlite] Multitable query question
I have two tables, one which has each visit(call) on a house logged, another which has each letter sent to a house logged. I would like to create a query which has two columns, Letters.Date and Calls.Date, for a specific HouseID. I have tried this query for HouseID 16: Select Letters.Date,Calls.Date From (Select * From Letters Where HouseID=16) As Letters JOIN Calls On Calls.HouseID=Letters.HouseID Unfortunately, there are many duplicates, because there are many combinations where the HouseID matches. So I tried using Distinct and Group By, but each time I do that it shows more Call dates than there really are. This is the query that does that: Select Distinct Letters.Date,Calls.Date From (Select * From Letters Where HouseID=16) As Letters JOIN Calls On Calls.HouseID=Letters.HouseID There are meant to be three call dates, but it shows four. So how do I get the desired result? This would be: 26/5/08 15/5/08 27/5/08 15/5/08 28/5/08 15/5/08 16/5/08 The tables and data used are: CREATE TABLE "Letters" ( "ID" INTEGER PRIMARY KEY, "HouseID" TEXT, "Date" TEXT ) BEGIN TRANSACTION; insert into Letters ("ID", "HouseID", "Date") values ('1', '16', '26/5/08'); insert into Letters ("ID", "HouseID", "Date") values ('2', '11', '17/5/08'); insert into Letters ("ID", "HouseID", "Date") values ('3', '16', '27/5/08'); insert into Letters ("ID", "HouseID", "Date") values ('4', '16', '28/5/08'); insert into Letters ("ID", "HouseID", "Date") values ('5', '18', '89/89/89'); insert into Letters ("ID", "HouseID", "Date") values ('6', '10', '25/5/08'); insert into Letters ("ID", "HouseID", "Date") values ('7', '16', '16/5/08'); insert into Letters ("ID", "HouseID", "Date") values ('8', '10', 'test'); insert into Letters ("ID", "HouseID", "Date") values ('9', '7', '29/5/08'); insert into Letters ("ID", "HouseID", "Date") values ('10', '11', '25/5/08'); insert into Letters ("ID", "HouseID", "Date") values ('11', '7', '6/5/08'); COMMIT; CREATE TABLE "Calls" ( "ID" INTEGER PRIMARY KEY, "HouseID" TEXT, "Date" TEXT ) BEGIN TRANSACTION; insert into Calls ("ID", "HouseID", "Date") values ('1', '1', '15/5/08'); insert into Calls ("ID", "HouseID", "Date") values ('2', '2', '15/5/08'); insert into Calls ("ID", "HouseID", "Date") values ('3', '3', '15/5/08'); insert into Calls ("ID", "HouseID", "Date") values ('4', '4', '15/5/08'); insert into Calls ("ID", "HouseID", "Date") values ('5', '5', '15/5/08'); insert into Calls ("ID", "HouseID", "Date") values ('6', '6', '15/5/08'); insert into Calls ("ID", "HouseID", "Date") values ('7', '7', '15/5/08'); insert into Calls ("ID", "HouseID", "Date") values ('8', '8', '15/5/08'); insert into Calls ("ID", "HouseID", "Date") values ('9', '1', '28/5/08'); insert into Calls ("ID", "HouseID", "Date") values ('10', '1', '28/5/08'); insert into Calls ("ID", "HouseID", "Date") values ('11', '7', '28/5/08'); insert into Calls ("ID", "HouseID", "Date") values ('12', '7', '28/5/08'); insert into Calls ("ID", "HouseID", "Date") values ('13', '7', '28/5/08'); insert into Calls ("ID", "HouseID", "Date") values ('14', '2', '28/5/08'); insert into Calls ("ID", "HouseID", "Date") values ('15', '8', '28/5/08'); insert into Calls ("ID", "HouseID", "Date") values ('16', '9', '19/5/08'); insert into Calls ("ID", "HouseID", "Date") values ('18', '11', '19/5/08'); insert into Calls ("ID", "HouseID", "Date") values ('19', '11', '19/5/08'); insert into Calls ("ID", "HouseID", "Date") values ('20', '12', '19/5/08'); insert into Calls ("ID", "HouseID", "Date") values ('21', '12', '19/5/08'); insert into Calls ("ID", "HouseID", "Date") values ('22', '13', '19/5/08'); insert into Calls ("ID", "HouseID", "Date") values ('23', '14', '25/5/08'); insert into Calls ("ID", "HouseID", "Date") values ('24', '14', '5'); insert into Calls ("ID", "HouseID", "Date") values ('25', '14', ''); insert into Calls ("ID", "HouseID", "Date") values ('26', '15', '15/5/08'); insert into Calls ("ID", "HouseID", "Date") values ('27', '16', '15/5/08'); insert into Calls ("ID", "HouseID", "Date") values ('29', '18', '15/5/08'); insert into Calls ("ID", "HouseID", "Date") values ('30', '19', '15/5/08'); insert into Calls ("ID", "HouseID", "Date") values ('31', '16', '15/5/08'); insert into Calls ("ID", "HouseID", "Date") values ('32', '20', '15/5/08'); insert into Calls ("ID", "HouseID", "Date") values ('33', '21', '15/5/08'); insert into Calls ("ID", "HouseID", "Date") values ('34', '18', '15/5/08'); insert into Calls ("ID", "HouseID", "Date") values ('35', '16', '15/5/08'); insert into Calls ("ID", "HouseID", "Date") values ('36', '22', '15/5/08'); insert into Calls ("ID", "HouseID", "Date") values ('37', '23', '12/5/08'); insert into Calls ("ID", "HouseID", "Date") values ('38', '10', '12/5/08'); insert into Calls ("ID", "HouseID", "Date") values ('39', '24', '12/5/08'); insert into Calls ("ID", "HouseID", "Date") values ('40', '25', '12/5/08'); insert into Calls ("ID", "HouseID", "Date") values ('41', '26', '12/5/08'); i