|
|
Subject: query per intervallo di date
From: Pinco Pallo
Date: 5/13/2007 8:52:55 AM
Ciao a tutti,
ho una tabella con i seguenti campi
NOME - varchar(50)
DATA_ARRIVO - smalldatetime
DATA_PARTENZA - smalldatetime
Esempio di dati:
NOME: PincoPallo
DATA_ARRIVO: 01/05/2007
DATA_PARTENZA: 06/05/2007
Dovrei fare un query per ottenere un risultato del tipo:
PincoPallo, 01/05/2007
PincoPallo, 02/05/2007
PincoPallo, 03/05/2007
PincoPallo, 04/05/2007
PincoPallo, 05/05/2007
Nota: la data 06/05/2007 non deve apparire!
Ci sto provando da diversi giorni... inutilmente... adesso provo a chiedere
a voi...
Grazie a tutti!!!
Subject: query per intervallo di date
From: Pinco Pallo
Date: 5/13/2007 11:31:00 AM
Grazie per la risposta...
ma siccome non sono "esperto" di stored procedure non so bene come
muovermi...
Non si potrebbe fare con una semplice query SQL o T-SQL ?
Tra le due soluzioni da te proposte, con tab calendario e senza, direi che
senza è sicuramente meglio.
Grazie ancora!
"Albe V°" <vaccariTOGLI@hotmail.com> ha scritto nel messaggio
news:mn.6a5b7d750ce9560f.58073@hotmail.com...
> Pinco Pallo ci ha detto :
>> Ciao a tutti,
>>
>> ho una tabella con i seguenti campi
>>
>> NOME - varchar(50)
>> DATA_ARRIVO - smalldatetime
>> DATA_PARTENZA - smalldatetime
>>
>> Esempio di dati:
>>
>> NOME: PincoPallo
>> DATA_ARRIVO: 01/05/2007
>> DATA_PARTENZA: 06/05/2007
>>
>>
>> Dovrei fare un query per ottenere un risultato del tipo:
>>
>> PincoPallo, 01/05/2007
>> PincoPallo, 02/05/2007
>> PincoPallo, 03/05/2007
>> PincoPallo, 04/05/2007
>> PincoPallo, 05/05/2007
>>
>> Nota: la data 06/05/2007 non deve apparire!
>>
>>
>>
>> Ci sto provando da diversi giorni... inutilmente... adesso provo a
>> chiedere a voi...
>
> Ovviamente devi avere una tabella con l'elenco di tutti i giorni, ossia il
> calendario. Oppure puoi fartela generare in una SP on the fly, ma mi piace
> meno.
>
> Con la tabella calendario:
> SELECT NOME, DATA
> FROM TABELLA_CLIENTI AS TC
> INNER JOIN TABELLA_CALENDARIO AS CALEN
> ON TC.DATA_ARRIVO<=CALEN.DATA
> AND TC.DATA_PARTENZA>CALEN.DATA
> ORDER BY TC.NOME, CALEN.DATA
>
> Senza tabella appoggio:
> CREATE PROCEDURE QRY_ELENCO_PRESENZA
> @NOME VARCHAR(50),
> @DATA_ARRIVO SMALLDATETIME,
> @DATA_PARTENZA SMALLDATETIME
> AS
> SET NOCOUNT ON
> CREATE TABLE #DATE (NOME VARCHAR(50), DATA SMALLDATETIME)
> DECLARE @DATA_CORRENTE SMALLDATETIME
> SET @DATA_CORRENTE=@DATA_ARRIVO
>
> WHILE @DATA_CORRENTE<@DATA_PARTENZA
> BEGIN
> INSERT INTO #DATE (NOME, DATA)
> VALUES(@NOME, @DATA_CORRENTE)
> SET @DATA_CORRENTE=DATEADD(DAY,1,@DATA_CORRENTE)
> END
>
> SELECT NOME, DATA
> FROM #DATE
> ORDER BY DATA
>
>
> Ciao
>
> Alberto
>
> --
> Georges Pompidou: Un uomo di stato è un politico che dona se stesso al
> servizio della nazione. Un politico è un uomo di stato che pone la nazione
> al suo servizio.
>
>
Subject: query per intervallo di date
From: Andrea Benedetti
Date: 5/13/2007 8:29:50 PM
Salve Pinco Pallo,
"Pinco Pallo" <prova@microsoft.com> ha scritto nel messaggio
news:f26lrm$qcg$2@nnrp.ngi.it...
> Grazie per la risposta...
> ma siccome non sono "esperto" di stored procedure non so bene come
> muovermi...
Bhè attenzione... perchè se devi lavorare con SQL Server la conoscenza delle
stored procedure diventa indispensabile!
> Non si potrebbe fare con una semplice query SQL o T-SQL ?
>
> Tra le due soluzioni da te proposte, con tab calendario e senza, direi che
> senza è sicuramente meglio.
use tempdb
go
/* Costruiamo la tabella myTable */
create table myTable
(
idRecord int primary key identity(1,1),
nome varchar(30),
data_arrivo datetime,
data_partenza datetime
)
/* Inserisco myTable di esempio */
insert myTable values ('pinco pallo','20070501', '20070506')
/*
Imposto una data di inzio ed una di fine
Ovvero il range di date che voglio analizzare
*/
declare @dataInizio datetime
declare @dataFine datetime
select @dataInizio = data_arrivo, @dataFine = dateadd(day,-1,data_partenza )
from myTable
where nome = 'pinco pallo'
/*
Costruisco un mio calendario contenente tutte le date comprese nel range che
voglio analizzare
*/
declare @dateTable TABLE
(myCalendarDate DATETIME)
declare @myCalendarDate DATETIME
SET @myCalendarDate = @dataInizio
WHILE @myCalendarDate BETWEEN @dataInizio AND @dataFine
BEGIN
INSERT INTO @DateTable (myCalendarDate)
VALUES (@myCalendarDate)
SET @myCalendarDate = DATEADD(DAY, 1, @myCalendarDate)
END
/* Questo è il mio calendario */
select * from @dateTable
/* Faccio pulizia */
drop table myTable
go
> Grazie ancora!
Ciao!
--
Andrea Benedetti
Microsoft MVP - SQL Server
www.absistemi.it - www.ugiss.org
http://blogs.ugidotnet.org/ab
http://mvp.support.microsoft.com
http://italy.mvps.org
Subject: query per intervallo di date
From: Lorenzo Benaglia
Date: 5/14/2007 9:01:35 AM
Alessandro AKA Trinità wrote:
> Scusa Lorenzo che differenza ci sarebbe tra Cross Join e Cross Apply ?
Dai BOL:
"The APPLY operator allows you to invoke a table-valued function for each
row returned by an outer table expression of a query. The table-valued
function acts as the right input and the outer table expression acts as the
left input. The right input is evaluated for each row from the left input
and the rows produced are combined for the final output. The list of columns
produced by the APPLY operator is the set of columns in the left input
followed by the list of columns returned by the right input.
There are two forms of APPLY: CROSS APPLY and OUTER APPLY. CROSS APPLY
returns only rows from the outer table that produce a result set from the
table-valued function. OUTER APPLY returns both rows that produce a result
set, and rows that do not, with NULL values in the columns produced by the
table-valued function".
"A cross join that does not have a WHERE clause produces the Cartesian
product of the tables involved in the join. The size of a Cartesian product
result set is the number of rows in the first table multiplied by the number
of rows in the second table. The following example shows a Transact-SQL
cross join".
Ciao!
--
Lorenzo Benaglia
Microsoft MVP - SQL Server
http://blogs.dotnethell.it/lorenzo
http://italy.mvps.org
|