4

これは達成するのが簡単なことのように思えますが、望ましい結果を得るために正しく考えているかどうかはわかりません. 私はピボットを使用していますが、それとペアになっている何かが必要だと思います。

各クライアントの毎月の請求書を含む請求書テーブルがあります。多くても、クライアントは毎月 1 通ずつ、年間 12 通の請求書を受け取ります。

+----------+-------+-------+--------------+--------------+--------------+
| ClientID | Month | Year  | ColumnValue1 | ColumnValue2 | ColumnValue3 |
+----------+-------+-------+--------------+--------------+--------------+
|        1 |     1 |  2012 |           20 |           30 |           50 |
|        1 |     2 |  2012 |           25 |           35 |           40 |
|        2 |     1 |  2012 |           28 |           38 |           48 |
+----------+-------+-------+--------------+--------------+--------------+

今、各クライアントに基づいて以下のようなリストを作成したいと考えています。各月の列があります。したがって、クライアント 1 は次のようになります。

+--------------+----+----+---+---+---+---+---+---+---+----+----+----+-------+
|  ColumnName  | 1  | 2  | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | Total |
+--------------+----+----+---+---+---+---+---+---+---+----+----+----+-------+
| ColumnValue1 | 20 | 25 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |  0 |  0 |  0 |   45  |
| ColumnValue2 | 30 | 35 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |  0 |  0 |  0 |   65  |
| ColumnValue3 | 50 | 40 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |  0 |  0 |  0 |   90  |
+--------------+----+----+---+---+---+---+---+---+---+----+----+----+-------+
4

3 に答える 3

3

これを試して

Declare @t Table(ClientId Int,[Month] Int,[Year] Int,ColumnValue1 Int,ColumnValue2 Int, ColumnValue3 Int)
Insert Into @t Values(1,1,2012,20,30,50),(1,2,3012,25,35,40),(2,1,2012,28,38,48)

;With Cte As
(
    Select ClientId,[Month],ColumnName,ColumnNameValues
    From @t 
    UnPivot(ColumnNameValues For ColumnName In (ColumnValue1,ColumnValue2,ColumnValue3)) As unpvt
)

Select ClientId,
        ColumnName 
        ,[1] = Coalesce([1],0)
        ,[2] = Coalesce([2],0)
        ,[3] = Coalesce([3],0)
        ,[4] = Coalesce([4],0)
        ,[5] = Coalesce([5],0)
        ,[6] = Coalesce([6],0)
        ,[7] = Coalesce([7],0)
        ,[8] = Coalesce([8],0)
        ,[9] = Coalesce([9],0)
        ,[10]= Coalesce([10],0)
        ,[11]= Coalesce([11],0)
        ,[12] = Coalesce([12],0)
        ,Total = Coalesce([1],0) + Coalesce([2],0) + Coalesce([3],0) + Coalesce([4],0) + 
                 Coalesce([5],0) + Coalesce([6],0) + Coalesce([7],0) + Coalesce([8],0) + 
                 Coalesce([9],0) + Coalesce([10],0) + Coalesce([11],0) + Coalesce([12],0) 
From  Cte 
PIVOT 
(   
    MAX(ColumnNameValues) For [Month] In ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12] )   
) As pvt 
--Where ClientId =1 -- uncomment for specific client report
Order By 1

結果

ここに画像の説明を入力

于 2012-10-23T05:16:32.963 に答える
2
Create table sequence (seqid bigint)
go

--Create a table which has sequence from 1 to 12 for monthId
Insert into sequence
Select Top 12 ROW_NUMBER() over(order by name)
from sys.objects
go


USE tempdb 
GO

CREATE TABLE TestReport
(
        ClientId int 
        ,MonthId int 
        ,YearId int 
        ,val1 int 
        ,val2 int 
        ,val3 int 
)
go


insert into TestReport
Select 1, 1,2012, 20,30,50
union 
Select 1,2,2012,25, 35, 40
union 
Select 2, 1, 2012, 28,38,48

Select *
from testReport


--Cross join with the Sequence table to get rows for each month

Select clientId
        , seqid as monthId
        , YearId 
        , case when MonthId = seqid then val1 else 0 end val1
        , case when MonthId = seqid then val2 else 0 end val2
        , case when MonthId = seqid then val3 else 0 end val3
into #Temp
from sequence seq
cross join  testReport rpt
where seq.seqid <=12


    --Select *      from #Temp

SELECT 'ColumnValue1' AS [columnName],   [1], [2], [3], [4],[5],[6],[7],[8],[9],[10],[11],[12]
,[1]+  [2]+  [3]+  [4]+ [5]+ [6]+ [7]+ [8]+ [9]+ [10]+ [11]+ [12] as Total
 FROM
(SELECT monthId, val1 
    FROM #Temp
    where ClientId =1 

    ) AS SourceTable
PIVOT
(
max(val1) FOR MonthId IN ( [1], [2], [3], [4],[5],[6],[7],[8],[9],[10],[11],[12])
) 
AS PivotTable


go

Drop table #Temp
Drop table sequence 
drop table TestReport
go
于 2012-10-23T05:00:08.773 に答える