|
помогите, плиз, с запросом
|
|||
---|---|---|---|
#18+
Здравствуйте, подскажите плиз, есть ли возможность обратится повторно с exists к одной и той же таблице declare @T table (id int, tm smalldatetime, st int) insert @T values(1, '01.01.2019', 100) insert @T values(1, '01.01.2020', 2) insert @T values(1, '01.01.2021', 30) insert @T values(2, '01.01.2020', 54) insert @T values(2, '01.01.2021', 12) insert @T values(2, '01.01.2022', 4) insert @T values(3, '01.01.2022', 8) /* суть не просто обратится к Т, сдесь для примера, т.к. получение А довольно сложный запрос и нужно к нему потом еще добавить недостающие данные 2, '01.01.2022' фактически полем или строкой без разницы select A.*, B.* from (select * from @T where tm < '01.01.2022') as A left join @T B on B.id=A.id and not exists(select 1 from A where B.id=A.id and B.tm=A.tm) order by A.id, A.tm, B.tm */ --типа такого, через временную таблицу declare @T2 table (id int, tm smalldatetime, st int) insert @T2 select * from @T where tm < '01.01.2022' select * from @T2 select * from @T2 union all select T1.id, T1.tm, null from @T T1 where exists(select 1 from @t2 T2 where T2.id=T1.id) and not exists(select 1 from @t2 T2 where T2.id=T1.id and T2.tm=T1.tm) order by 1, 2 ... |
|||
:
Нравится:
Не нравится:
|
|||
19.01.2021, 14:23 |
|
помогите, плиз, с запросом
|
|||
---|---|---|---|
#18+
Код: sql 1. 2. 3. 4. 5.
... |
|||
:
Нравится:
Не нравится:
|
|||
19.01.2021, 16:42 |
|
|
start [/forum/topic.php?fid=46&msg=40037141&tid=1685193]: |
0ms |
get settings: |
10ms |
get forum list: |
14ms |
check forum access: |
4ms |
check topic access: |
4ms |
track hit: |
161ms |
get topic data: |
10ms |
get forum data: |
3ms |
get page messages: |
42ms |
get tp. blocked users: |
1ms |
others: | 301ms |
total: | 550ms |
0 / 0 |