Emanuele Ciao,
con una sola query T-SQL credo che non sia possibile ottenere l'output richiesto.
Prova la multi-statement table-valued functions udf_get_periodi che trovi di seguito, permette di ottenere l'output richiesto. Non è ottimizzata e fa uso di un cursore... se pensi di utilizzarla e prevedi che più utenti possano lanciarla contemporaneamente su molti dati, forse è meglio dare un'occhiata anche alle prestazioni
.
-- Cancellazione funzione dbo.udf_get_periodi
if (object_id('udf_get_periodi') is not null)
DROP FUNCTION dbo.udf_get_periodi
GO
-- Creazione funzione dbo.udf_get_periodi
CREATE FUNCTION dbo.udf_get_periodi()
returns @tabdate table(id int not null, dal datetime not null, al datetime)
as
begin
declare @id int,
@currentid int,
@countperiodo int,
@countid int,
@i int,
@j int,
@data datetime
declare curdate cursor for
select t.id,
t.data,
(select (count(id) / 2) from testdate t1 where t.id=t1.id
group by t1.id) as countper,
(select (count(id)) from testdate t1 where t.id=t1.id
group by t1.id) as countid
from testdate t
open curdate
fetch next from curdate into @id, @data, @countperiodo, @countid
set @i = 1
set @currentid = @id
while (@@fetch_status = 0)
begin
if (@currentid = @id) and (@i <= @countperiodo)
begin
begin
if (@i = 1)
begin
insert into @tabdate (id, dal, al) values (@id, @data, null)
end
else if (@i = @countperiodo)
begin
update @tabdate set al= @data where (id= @id) and (al is null)
end
end
end
else begin
set @i = 1
set @currentid = @id
if (@i = @countperiodo) and (@countperiodo = 1)
begin
insert into @tabdate (id, dal, al) values (@id, @data, null)
set @j = 1
while (@@fetch_status = 0) and (@j < @countid)
begin
fetch next from curdate into @id, @data, @countperiodo, @countid
set @j = @j + 1
end
update @tabdate set al= @data where (id= @id) and (al is null)
end
else begin
insert into @tabdate (id, dal, al) values (@id, @data, null)
end
end
set @i = @i + 1
fetch next from curdate into @id, @data, @countperiodo, @countid
end
close curdate
deallocate curdate
return
END
Con questo scenario:
-- Cancellazione tabella TESTDATE
IF
(OBJECT_ID('TESTDATE') IS NOT NULL)DROP TABLE dbo.TESTDATE
GO
-- Creazione tabella TESTDATE
CREATE
TABLE dbo.TESTDATE (ID INT NOT NULL, DATA DATETIME NOT NULL)
-- Inserimento valori di test
INSERT
INTO testdate (id, data) VALUES (1, '01/01/2008')
INSERT
INTO testdate (id, data) VALUES (1, '01/02/2008')
INSERT
INTO testdate (id, data) VALUES (1, '01/03/2008')
INSERT
INTO testdate (id, data) VALUES (1, '01/04/2008')
INSERT
INTO testdate (id, data) VALUES (1, '01/05/2008')
INSERT
INTO testdate (id, data) VALUES (1, '01/10/2008')
INSERT
INTO testdate (id, data) VALUES (1, '01/11/2008')
INSERT
INTO testdate (id, data) VALUES (1, '01/12/2008')
INSERT
INTO testdate (id, data) VALUES (1, '01/13/2008')
INSERT
INTO testdate (id, data) VALUES (1, '01/14/2008')
INSERT
INTO testdate (id, data) VALUES (2, '01/15/2008')
INSERT
INTO testdate (id, data) VALUES (2, '01/17/2008')
INSERT
INTO testdate (id, data) VALUES (2, '01/18/2008')
INSERT
INTO testdate (id, data) VALUES (4, '01/19/2008')
ho ottenuto questo output:
SELECT
* FROM DBO.UDF_GET_PERIODI()
id dal al
----------- ----------------------- -----------------------
1 2008-01-01 2008-01-05
1 2008-01-10 2008-01-14
2 2008-01-15 2008-01-18
4 2008-01-19 NULL
Ciao!