Если у тебя всегда не более конкретного числа записей с одинаковым id (например 2), то можно сделать что-то вроде
select id1, text1, text2, comment1, comment2
from
(select
rownum as r1,
id as id1,
text as text1,
comment as comment1
from table
order by id,text,comment) A
left join
(select
rownum as r2,
id as id2,
text as text2,
comment as comment2
from table
order by id,text,comment) B
on
id1=id2 and r1<>r2
Если же количество записей для одного id не ограничено, то одним запросом никак.
Server: Msg 1033, Level 15, State 1, Line 9
The ORDER BY clause is invalid in views, inline functions, derived tables, and subqueries, unless TOP is also specified.
Server: Msg 156, Level 15, State 1, Line 17
Incorrect syntax near the keyword 'order'.
select id1, text1, text2, comment1, comment2
from
(select top 100
(SELECT COUNT(*) FROM dbo.test_table) as r1,
id as id1,
text as text1,
comment as comment1
from dbo.test_table
order by id,text,comment) A
left join
(select top 100
(SELECT COUNT(*) FROM dbo.test_table) as r2,
id as id2,
text as text2,
comment as comment2
from dbo.test_table
order by id,text,comment) B
on
id1=id2 and r1<>r2
Здравствуйте, DedPakhom, Вы писали:
DP>я вот так сделал:
Count(*) там совершенно ни к месту. Я не знаю есть ли в MSSQL rownum, в оракле это псевдополе является порядковым номером строки в конкретной выборке. Это поле+правила сортировки позволяют при джоине таблицы самой с собой джоинить различные строки. В принципе в оракле я мог бы использовать rowid и забить на rownum и сортировку. Если нет ни rownum ни чего-то вроде rowid, то можно в таблицу ввести уникальный идентификатор uniqueid и в джоине писать uniqueid1<>uniqueid2...
select id1, text1, text2, comment1, comment2
from
(select top 100
rowid as r1,
id as id1,
text as text1,
comment as comment1
from dbo.test_table
order by id,text,comment) A
left join
(select top 100
rowid as r2,
id as id2,
text as text2,
comment as comment2
from dbo.test_table
order by id,text,comment) B
on
id1=id2 and r1<>r2
Ну во-первых сортировки в подзапросах уже не нужны.
Во-вторых, проблему можно решить так:
Первый подзапрос:
(select top 100
rowid as r1,
id as id1,
text as text1,
comment as comment1
from dbo.test_table
where rowid in (select max(rowid) from dbo.test_table group by id)) A
Здравствуйте, yogi, Вы писали:
Y>Здравствуйте, DedPakhom, Вы писали:
Y>Ну во-первых сортировки в подзапросах уже не нужны. Y>Во-вторых, проблему можно решить так: Y>Первый подзапрос: Y>
Y> (select top 100
Y> rowid as r1,
Y> id as id1,
Y> text as text1,
Y> comment as comment1
Y> from dbo.test_table
Y> where rowid in (select max(rowid) from dbo.test_table group by id)) A
Y>
Y>Второй подзапрос соответственно будет с условием