SQL将列拆分到另一行

时间:2016-08-24 09:40:44

标签: sql sql-server tsql

enter image description here

希望我能正确解释这一点。我有这个代码将数据提取到excel上。我想要发生的是在一行上显示获取等的细节,然后再次显示相同的信息,但显示处理量,因为当前两个值都显示在同一行上对用户不利。

我必须获取数据的代码是

DECLARE @DateNull DATE;
SET @DateNull = CAST('1900/01/01' AS DATE)

set transaction isolation level read uncommitted
select 
a.DataAreaId,
a.AssetId AS AssetNumber,
a.Name as AssetName,
a.AssetGroup,
CASE WHEN b.Status = 0 then 'Not yet aquired' 
 WHEN b.Status = 1 then 'Open' 
 WHEN b.Status = 2 then 'Suspeneded' 
 WHEN b.Status = 3 then 'Closed' 
 WHEN b.Status = 4 then 'Sold' 
 WHEN b.Status = 5 then 'Scrapped' 
 WHEN b.Status = 6 then 'Transfered to low value pool'  END as 

AssetStatus,
NULLIF(b.AcquisitionDate,@DateNull) AS AcquisitionDate,
(b.ServiceLife) AS ServiceLife,
NULLIF(b.DisposalDate,@DateNull) AS DisposalDate,
AquisitionPrice.AmountMST*-1 As Aquisition,
--ISNULL(AquisitionPrice.AquisitionMonth,@DateNull) AS AquisitionMonth,
Depreciation.AmountMST*-1 As Depreciation,
NULLIF(Depreciation.DepreciationMonth,@DateNull) AS DepreciationMonth,
Disposal.AmountMST*-1 AS DisposalAmount,
--ISNULL(Disposal.DisposalMonth,@DateNull) AS DisposalMonth,
dv.OperatingDivision



    from assettable a
    Inner join AssetBook b
        ON a.assetid=b.assetid
        and a.dataareaid=b.dataareaid
    LEFT JOIN 
        dbo.PSV_AX_KPIDefaultDimensionView  DV
            ON b.DefaultDimension =  dv.DefaultDimension
    LEFT JOIN 
        (select        AssetID,BookId,DataAreaId,/*LowValuePoolType_AU,Reclassification*/
            --DATEADD(month, DATEDIFF(month, 0, TRANSDATE), 0) AS AquisitionMonth,
            TransType,SUM(AmountMST) AS AmountMST,SUm(RevaluationAmount) AS RevaluationAmount from AssetTrans
            WHERE TransType =1 /*Aquisition*/Or TRANSTYPE =2 /*AcquisitionAdj*/
            GROUP BY AssetID,BookId,DataAreaId,TransType

        --DATEADD(month, DATEDIFF(month, 0, TRANSDATE), 0)
    ) AquisitionPrice
ON a.AssetId=AquisitionPrice.AssetId    
AND a.DataAreaId=AquisitionPrice.DataAreaId
AND b.BookId=AquisitionPrice.BookId
LEFT JOIN 
    (select AssetID,BookId,DataAreaId,/*LowValuePoolType_AU,Reclassification*/
        DATEADD(month, DATEDIFF(month, 0, TRANSDATE), 0) AS DepreciationMonth,
        TransType,SUM(AmountMST) AS AmountMST,SUm(RevaluationAmount) AS RevaluationAmount from AssetTrans
        WHERE TransType =3 /*Depreciation*/Or TRANSTYPE =4 /*DepreciationAdj*/ 
        GROUP BY AssetID,BookId,DataAreaId,
                DATEADD(month, DATEDIFF(month, 0, TRANSDATE), 0),
                TransType
    ) Depreciation
ON a.AssetId=Depreciation.AssetId   
AND a.DataAreaId=Depreciation.DataAreaId
AND b.BookId=Depreciation.BookId

Left join

    (select AssetID,BookId,DataAreaId,TransType,
        --DATEADD(month, DATEDIFF(month, 0, TRANSDATE), 0) AS DisposalMonth,
        SUM(AmountMST) AS AmountMST,SUm(RevaluationAmount) AS RevaluationAmount from AssetTrans 
        WHERE TransType in (8,9) /*Disposal Sale,Disposal Scrap*/
        GROUP BY AssetID,BookId,DataAreaId,TransType
        --DATEADD(month, DATEDIFF(month, 0, TRANSDATE), 0)
    ) Disposal  
ON a.AssetId=Disposal.AssetId   
AND a.DataAreaId=Disposal.DataAreaId
AND b.BookId=Disposal.BookId

1 个答案:

答案 0 :(得分:0)

虽然这整个场景通常都是一个坏主意,但您可以通过{{1}在您的联接中UNION获取和折旧数据来实现此目的,这将为您提供两行。然后,您需要使用CASE语句来检查要显示的语句。

如果您不使用数据连接表,则需要处理我在Excel工作表中null部分ELSE部分的CASE值。

我还建议您清理代码。至少可以说你的格式是不一致的,你在同一个剧本中对“获取”有几种不同的拼写......

没有数据可以尝试这个我不知道它是否会按原样运行,但它应该让你顺利:

DECLARE @DateNull DATE;
SET @DateNull = CAST('1900/01/01' AS DATE)

set transaction isolation level read uncommitted
select 
a.DataAreaId,
a.AssetId AS AssetNumber,
a.Name as AssetName,
a.AssetGroup,
CASE WHEN b.Status = 0 then 'Not yet aquired' 
 WHEN b.Status = 1 then 'Open' 
 WHEN b.Status = 2 then 'Suspeneded' 
 WHEN b.Status = 3 then 'Closed' 
 WHEN b.Status = 4 then 'Sold' 
 WHEN b.Status = 5 then 'Scrapped' 
 WHEN b.Status = 6 then 'Transfered to low value pool'  END as 

AssetStatus,
case when APD.APDType = 'AcquisitionPrice' then NULLIF(b.AcquisitionDate,@DateNull) else null end AS AcquisitionDate,
case when APD.APDType = 'AcquisitionPrice' then (b.ServiceLife) else null end AS ServiceLife,
case when APD.APDType = 'AcquisitionPrice' then NULLIF(b.DisposalDate,@DateNull) else null end AS DisposalDate,
case when APD.APDType = 'AcquisitionPrice' then APD.AmountMST*-1 else null end As Aquisition,
--ISNULL(AquisitionPrice.AquisitionMonth,@DateNull) AS AquisitionMonth,

case when APD.APDType = 'Depreciation' then APD.AmountMST*-1 else null end As Depreciation,
case when APD.APDType = 'Depreciation' then NULLIF(APD.DepreciationMonth,@DateNull) else null end AS DepreciationMonth,
case when APD.APDType = 'Depreciation' then APD.AmountMST*-1 AS else null end DisposalAmount,
--ISNULL(Disposal.DisposalMonth,@DateNull) AS DisposalMonth,

dv.OperatingDivision



    from assettable a
    Inner join AssetBook b
        ON a.assetid=b.assetid
        and a.dataareaid=b.dataareaid
    LEFT JOIN 
        dbo.PSV_AX_KPIDefaultDimensionView  DV
            ON b.DefaultDimension =  dv.DefaultDimension
    LEFT JOIN 
        (select 'AcquisitionPrice' as APDType, AssetID,BookId,DataAreaId,/*LowValuePoolType_AU,Reclassification*/
            --DATEADD(month, DATEDIFF(month, 0, TRANSDATE), 0) AS AquisitionMonth,
            TransType,SUM(AmountMST) AS AmountMST,SUm(RevaluationAmount) AS RevaluationAmount from AssetTrans
            WHERE TransType =1 /*Aquisition*/Or TRANSTYPE =2 /*AcquisitionAdj*/
            GROUP BY AssetID,BookId,DataAreaId,TransType

        --DATEADD(month, DATEDIFF(month, 0, TRANSDATE), 0)

        union all 

        select 'Depreciation' as APDType,  AssetID,BookId,DataAreaId,/*LowValuePoolType_AU,Reclassification*/
        DATEADD(month, DATEDIFF(month, 0, TRANSDATE), 0) AS DepreciationMonth,
        TransType,SUM(AmountMST) AS AmountMST,SUm(RevaluationAmount) AS RevaluationAmount from AssetTrans
        WHERE TransType =3 /*Depreciation*/Or TRANSTYPE =4 /*DepreciationAdj*/ 
        GROUP BY AssetID,BookId,DataAreaId,
                DATEADD(month, DATEDIFF(month, 0, TRANSDATE), 0),
                TransType
    ) APDepreciation
ON a.AssetId=APD.AssetId    
AND a.DataAreaId=APD.DataAreaId
AND b.BookId=APD.BookId


Left join

    (select AssetID,BookId,DataAreaId,TransType,
        --DATEADD(month, DATEDIFF(month, 0, TRANSDATE), 0) AS DisposalMonth,
        SUM(AmountMST) AS AmountMST,SUm(RevaluationAmount) AS RevaluationAmount from AssetTrans 
        WHERE TransType in (8,9) /*Disposal Sale,Disposal Scrap*/
        GROUP BY AssetID,BookId,DataAreaId,TransType
        --DATEADD(month, DATEDIFF(month, 0, TRANSDATE), 0)
    ) Disposal  
ON a.AssetId=Disposal.AssetId   
AND a.DataAreaId=Disposal.DataAreaId
AND b.BookId=Disposal.BookId