Sqlserver
 sql >> Teknologi Basis Data >  >> RDS >> Sqlserver

sql query untuk menambahkan bulan fiskal secara dinamis menggunakan pivot

Anda perlu menggunakan SQL dinamis untuk melakukan ini. kasar kode akan menjadi seperti ini:

ALTER PROCEDURE [dbo].[_sp_GetDMActivityTrackerReport]
    @CoachId VARCHAR(7),
    @Month INT,
    @FiscalYear INT
AS 
BEGIN    

INSERT @FiscalMonth (ID,Month,NbHolidays,MonthDate,TotalDays)
EXECUTE dbo._sp_GetFiscalMonths @Month, @FiscalYear

DECLARE @cols AS NVARCHAR(MAX),
    @colsNull AS NVARCHAR(MAX),
    @colsSum AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(DateName(Month,nft.MonthPeriodStart)) 
                    from NonFieldTime nft
                    where datename(Month,nft.MonthPeriodStart) + '-'+ substring(datename(Year,nft.MonthPeriodStart),3,2) 
                        IN (SELECT Month +'-' +substring(datename(Year,MonthDate),3,2) [Months] 
                            FROM [email protected])
            FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,1,'')

select @colsNull = STUFF((SELECT distinct ', IsNull(' + QUOTENAME(DateName(Month,nft.MonthPeriodStart))+', 0) as '+DateName(Month,nft.MonthPeriodStart)
                    from NonFieldTime nft
                    where datename(Month,nft.MonthPeriodStart) + '-'+ substring(datename(Year,nft.MonthPeriodStart),3,2) 
                        IN (SELECT Month +'-' +substring(datename(Year,MonthDate),3,2) [Months] 
                            FROM [email protected])
            FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)') ,1,1,'')

select @colsSum = STUFF((SELECT distinct '+ IsNull(' + QUOTENAME(DateName(Month,nft.MonthPeriodStart))+', 0)'
                    from NonFieldTime nft
                    where datename(Month,nft.MonthPeriodStart) + '-'+ substring(datename(Year,nft.MonthPeriodStart),3,2) 
                        IN (SELECT Month +'-' +substring(datename(Year,MonthDate),3,2) [Months] 
                            FROM [email protected])
            FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)') ,1,1,'')+' as [Total Field TIME] '

set @query = 'SELECT PreparationID,CoachId,UserID, MemberID,
                    [Rep Name], ' + @colsNull + ', '+ @colsSum+' 
             from 
             (
                SELECT up.PreparationID,
                    tt.UserId [CoachId],
                    up.UserID, utm.MemberID, 
                    (ui.FirstName + '' '' + ui.LastName) AS [Rep Name],
                    DateName(Month,nft.MonthPeriodStart) [Month], 
                    sum(nft.Quantity) [Days]
                FROM TransferedTime tt
                INNER JOIN UPreparation up 
                    ON tt.PreparationID = up.PreparationID 
                RIGHT JOIN UTeamMembers utm 
                    ON tt.UserId = utm.CoachID AND utm.MemberID = up.UserID
                INNER JOIN UserInfo ui 
                    ON utm.MemberID = ui.UserID
                LEFT JOIN NonFieldTime nft 
                    ON nft.UserId = tt.UserId 
                    AND tt.MonthPeriodFrom = nft.MonthPeriodStart
                    AND datename(Month,nft.MonthPeriodStart) + ''-''+ substring(datename(Year,nft.MonthPeriodStart),3,2) IN 
                        (SELECT Month +''-'' +substring(datename(Year,MonthDate),3,2) [Months] 
                         FROM [email protected])
                WHERE utm.MemberID IN (SELECT MemberID 
                                        FROM UTeamMembers 
                                        WHERE CoachID = '[email protected]+')
                GROUP BY up.PreparationID,tt.UserId,up.UserID, utm.MemberID,
                (ui.FirstName + '' '' + ui.LastName),DateName(Month,nft.MonthPeriodStart)
            ) x
            pivot 
            (
                sum(Days)
                for Month in (' + @cols + ')
            ) p '

execute(@query)

Saran saya daripada menggunakan tabel temp @FiscalMonth adalah membuat tabel yang permanen untuk ini. Akan jauh lebih mudah untuk melakukan kueri terhadap tabel perm daripada tabel temp saat menggunakan sql dinamis. Tabel temp mungkin berada di luar cakupan untuk kueri dinamis.



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Cara Mengaktifkan CDC di Kumpulan Tabel ATAU Mengaktifkan Semua Tabel di Database di SQL Server - Tutorial SQL Server

  2. TSQL-2008 SUM(X) OVER (PARTISI ... ORDER BY CLAUSE)

  3. menentukan set karakter dari tabel / database?

  4. Penginstalan dan Konfigurasi SQL Server Log Pengiriman &Pemulihan Bencana -4

  5. Menggunakan Web.Config untuk mengatur string koneksi database SQL saya?