Domanda

Se scelgo da un gruppo di tabelle entro il mese, il giorno, l'anno, restituisce solo righe con record e lascia le combinazioni senza alcun record, facendolo apparire a colpo d'occhio che ogni giorno o mese ha attività, devi guardare alla data colonna attivamente per lacune.Come posso ottenere una riga per ogni giorno/mese/anno, anche quando non sono presenti dati, in T-SQL?

È stato utile?

Soluzione 2

Il mio sviluppatore mi ha risposto con questo codice, i caratteri di sottolineatura convertiti in trattini perché StackOverflow stava alterando i caratteri di sottolineatura: non è richiesta alcuna tabella numerica.Il nostro esempio è un po' complicato da un'unione a un'altra tabella, ma forse il codice di esempio aiuterà qualcuno un giorno.

declare @career-fair-id int 
select @career-fair-id = 125

create table #data ([date] datetime null, [cumulative] int null) 

declare @event-date datetime, @current-process-date datetime, @day-count int 
select @event-date = (select careerfairdate from tbl-career-fair where careerfairid = @career-fair-id) 
select @current-process-date = dateadd(day, -90, @event-date) 

    while @event-date <> @current-process-date 
    begin 
    select @current-process-date = dateadd(day, 1, @current-process-date) 
    select @day-count = (select count(*) from tbl-career-fair-junction where attendanceregister <= @current-process-date and careerfairid = @career-fair-id) 
        if @current-process-date <= getdate() 
        insert into #data ([date], [cumulative]) values(@current-process-date, @day-count) 
    end 

    select * from #data 
    drop table #data 

Altri suggerimenti

Crea una tabella di calendario e un join esterno su tale tabella

Considera l'utilizzo di a tabella dei numeri.Sebbene possa essere un po' hacker, è il metodo migliore che ho utilizzato per interrogare rapidamente i dati mancanti o mostrare tutte le date o qualsiasi cosa in cui desideri esaminare i valori all'interno di un intervallo, indipendentemente dal fatto che vengano utilizzati tutti i valori in quell'intervallo.

Basandosi su quanto affermato da SQLMenace, è possibile utilizzare un CROSS JOIN per popolare rapidamente la tabella o crearla in modo efficiente in memoria.
http://www.sitepoint.com/forums/showthread.php?t=562806

L'attività richiede che un set completo di date venga unito a sinistra sui tuoi dati, come ad esempio


DECLARE @StartInt int
DECLARE @Increment int
DECLARE @Iterations int

SET @StartInt = 0
SET @Increment = 1
SET @Iterations = 365


SELECT
    tCompleteDateSet.[Date]
  ,AggregatedMeasure = SUM(ISNULL(t.Data, 0))
FROM
        (

            SELECT
                [Date] = dateadd(dd,GeneratedInt, @StartDate)
            FROM
                [dbo].[tvfUtilGenerateIntegerList] (
                        @StartInt,
                        ,@Increment,
                        ,@Iterations
                    )
            ) tCompleteDateSet
    LEFT JOIN tblData t
          ON (t.[Date] = tCompleteDateSet.[Date])
GROUP BY
tCompleteDateSet.[Date]

dove la funzione con valori di tabella tvfUtilGenerateIntegerList è definita come


-- Example Inputs

-- DECLARE @StartInt int
-- DECLARE @Increment int
-- DECLARE @Iterations int
-- SET @StartInt = 56200
-- SET @Increment = 1
-- SET @Iterations = 400
-- DECLARE @tblResults TABLE
-- (
--     IterationId int identity(1,1),
--     GeneratedInt int
-- )


-- =============================================
-- Author: 6eorge Jetson
-- Create date: 11/22/3333
-- Description: Generates and returns the desired list of integers as a table
-- =============================================
CREATE FUNCTION [dbo].[tvfUtilGenerateIntegerList]
(
    @StartInt int,
    @Increment int,
  @Iterations int
)
RETURNS
@tblResults TABLE
(
    IterationId int identity(1,1),
    GeneratedInt int
)
AS
BEGIN

  DECLARE @counter int
  SET @counter= 0
  WHILE (@counter < @Iterations)
    BEGIN
    INSERT @tblResults(GeneratedInt) VALUES(@StartInt + @counter*@Increment)
    SET @counter = @counter + 1
    END


  RETURN
END
--Debug
--SELECT * FROM @tblResults

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top