jeudi 16 avril 2015

Create autoincremental column in particular select statement

This is my table and example of rows:



CREATE TABLE A (
_id INTEGER PRIMARY KEY AUTOINCREMENT,
id_spfirm INTEGER NOT NULL,
sum INTEGER NOT NULL,
type TEXT


);



INSERT INTO [A] ([_id], [id_spfirm], [sum], [type]) VALUES (1, 10, 100, 'name1');
INSERT INTO [A] ([_id], [id_spfirm], [sum], [type]) VALUES (2, 20, 200, 'name1');
INSERT INTO [A] ([_id], [id_spfirm], [sum], [type]) VALUES (3, 20, 300, 'name1');
INSERT INTO [A] ([_id], [id_spfirm], [sum], [type]) VALUES (4, 30, 10, 'name2');


I saw similar questions here and this is a select statement with GROUP_CONCAT, where I tried to insert an autoincremental field:



SELECT (
SELECT count( * )
FROM A b
WHERE a._id >= b._id
)
AS ord,a.type as name,
GROUP_CONCAT(a.sum) AS sums
FROM A AS a
GROUP BY a.type,
a.id_spfirm


The output in like here:



1 name1 100
3 name1 200,300
4 name2 10


Original select is much complex, and the result ORD column have big gaps. I need to add autoincrement row in select. How to do this? I need something like identity(int,1,1) in sql-server.


Aucun commentaire:

Enregistrer un commentaire