Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I need to make a view that pairs rows in a table that has this DDL:
SQL
CREATE TABLE Events (
    source        TEXT,
    entity1       TEXT,
    entity2       TEXT,
    datetime      TEXT,
    backOdd1      TEXT NOT NULL,
    backOdd2      TEXT NOT NULL,
    insertionDate TEXT,
    PRIMARY KEY (
        source,
        entity1,
        entity2
    )
);


Vague depiction of what I have in mind

I'm currently in an SQLite database and I'm losing my head over this. I'm still noobish with databases and don't even know where to begin with this. I tried several times messing with a SELECT statement and an INNER JOIN to which I don't actually know what to join to. If anyone could give me a direction that would be great
Posted
Comments
Sascha Lefèvre 4-Jan-16 23:12pm    
Do you want to do this all in SQL only or do you want to feed it into some application which you develop? In the latter case I could probably help.
Member 12244050 4-Jan-16 23:43pm    
As a matter of a fact I am developing a Java application with Selenium along this. The program inputs the Events as a row in the Events table in the SQLite database. Now I need to find a way to group them in a way that you can see an Event being pared to another Event in the same table, with only two conditions: the sources must be different and the two entities must be the same (ignoring order of course).

I wouldn't know of a way to do this with SQL only in SQLite. So this is the best solution I can think of:

1. Get all different entity1/entity2-pairs:
SQL
SELECT DISTINCT entity1, entity2 FROM Events;

2. In Java filter out the duplicates (those pairs that only differ in order).

3. Loop through the remaining pairs and query the matching events:
SQL
SELECT * FROM Events WHERE (entity1=@e1 AND entity2=@e2) OR (entity1=@e2 AND entity2=@e1);

Where @e1 and @e2 are the parameter names for the filtered entity pairs.
 
Share this answer
 
Comments
Member 12244050 5-Jan-16 21:02pm    
I just came here to close the topic and I saw that solution. That is farily my solution. I unfortunately had to take it to Java instead of doing it all on SQLite. Felt it was much easier to compare the events on the software rather than in a view. Thank you all for the answers!
Sascha Lefèvre 6-Jan-16 3:17am    
You're welcome. Good Luck!
I think something like this is what you're after:
SQL
SELECT e1.source, e2.source FROM Events e1 INNER JOIN 
(SELECT source FROM Events e2 
ON e1.entity1=e2.entity1 AND e1.entity2 = e2.entity2 AND e1.source <> e2.Source)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900