views:

8

answers:

1

Hi all,

I'm using the following database:

CREATE TABLE datas (d_id INTEGER PRIMARY KEY, name_id numeric, countdata numeric);
INSERT INTO datas VALUES(1,1,20);  //(NULL,1,20);
INSERT INTO datas VALUES(2,1,47);  //(NULL,1,47);
INSERT INTO datas VALUES(3,2,36);  //(NULL,2,36);
INSERT INTO datas VALUES(4,2,58);  //(NULL,2,58);
INSERT INTO datas VALUES(5,2,87);  //(NULL,2,87);
CREATE TABLE names (n_id INTEGER PRIMARY KEY, name text);
INSERT INTO names VALUES(1,'nameA'); //(NULL,'nameA');
INSERT INTO names VALUES(2,'nameB'); //(NULL,'nameB');

What I would like to do, is to select all values (rows) of names - to which all columns of datas will be appended, for the row where datas.countdata is maximum for n_id (and of course, where name_id = n_id).

I can somewhat get there with the following query:

sqlite> .header ON

sqlite> SELECT * FROM names AS n1 
    LEFT OUTER JOIN (
        SELECT d_id, name_id, countdata FROM datas AS d1 
        WHERE d1.countdata IN (
            SELECT MAX(countdata) FROM datas 
            WHERE name_id=1 
            ) 
        ) AS p1 ON n_id=name_id;

n1.n_id|n1.name|p1.d_id|p1.name_id|p1.countdata
1|nameA|2|1|47
2|nameB|||

... however - obviously - it only works for a single row (the one explicitly set by name_id=1).

The problem is, the SQL query fails whenever I try to somehow reference the "current" n_id:

sqlite> SELECT * FROM names AS n1 
    LEFT OUTER JOIN (
        SELECT d_id, name_id, countdata FROM datas AS d1 
        WHERE d1.countdata IN (
            SELECT MAX(countdata) FROM datas 
            WHERE name_id=n1.n_id 
            ) 
        ) AS p1 ON n_id=name_id;

SQL error: no such column: n1.n_id

Is there any way of achieving what I want in Sqlite2??

Thanks in advance,

Cheers!

A: 

Oh, well - that wasn't trivial at all, but here is a solution:

sqlite> SELECT * FROM names AS n1 
        LEFT OUTER JOIN ( 
            SELECT d1.* 
            FROM datas AS d1, ( 
                SELECT max(countdata) as countdata,name_id 
                FROM datas 
                GROUP BY name_id
                ) AS ttemp 
            WHERE d1.name_id = ttemp.name_id AND d1.countdata = ttemp.countdata 
        ) AS p1 ON n1.n_id=p1.name_id;


n1.n  n1.name       p1.d_id     p1.name_id  p1.countdata                       
----  ------------  ----------  ----------  -----------------------------------
1     nameA         2           1           47                                 
2     nameB         5           2           87                                 

   

Well, hope this ends up helping someone, :) Cheers!

     

Notes: note that just calling max(countdata) screws up competely d_id:

sqlite> select d_id,name_id,max(countdata) as countdata from datas group by name_id;

d_id  name_id       countdata 
----  ------------  ----------
3     2             87        
1     1             47        

so to get correct corresponding d_id, we must do max() on datas separately - and then perform sort of an intersect with the full datas (except that intersect in sqlite requires that there are equal number of columns in both datasets, which is not the case here - and even if we made it that way, as seen above d_id will be wrong, so intersect will not work).

One way to do that is in using a sort of a temporary table, and then utilize a multiple table SELECT query so as to set conditions between full datas and the subset returned via max(countdata), as shown below:

sqlite> CREATE TABLE ttemp AS SELECT max(countdata) as countdata,name_id FROM datas GROUP BY name_id;
sqlite> SELECT d1.*, ttemp.* FROM datas AS d1, ttemp WHERE d1.name_id = ttemp.name_id AND d1.countdata = ttemp.countdata;

d1.d  d1.name_id    d1.countda  ttemp.coun  ttemp.name_id                      
----  ------------  ----------  ----------  -----------------------------------
2     1             47          47          1                                  
5     2             87          87          2                                  

sqlite> DROP TABLE ttemp;

or, we can rewrite the above so a SELECT subquery (sub-select?) is used, like this:

sqlite> SELECT d1.* FROM datas AS d1, (SELECT max(countdata) as countdata,name_id FROM datas GROUP BY name_id) AS ttemp WHERE d1.name_id = ttemp.name_id AND d1.countdata = ttemp.countdata;

d1.d  d1.name_id    d1.countda
----  ------------  ----------
2     1             47        
5     2             87        
sdaau