可以使用SQL以下面的方式转换数据吗?我不确定枢轴是否会起作用

时间:2015-06-29 18:32:25

标签: sql-server reporting-services crystal-reports

用户希望数据显示如下:

Tax     Description             ML  Link            Link
Code                                Rate    Link    Description

SC001   Abbeville County, SC    Y   0.0600  SC      South Carolina State            
SC001   Abbeville County, SC    Y   0.0100  SCLO1   Local Option 1%                 
SC002   Aiken County, SC        Y   0.0600  SC      South Carolina State            
SC002   Aiken County, SC        Y   0.0100  SCCP1   Capital Projects 1%             
SC002   Aiken County, SC        Y   0.0100  SCEC1   Education Capital Improvement

但数据存储在数据库中,如下所示:

var data = "{"54":

             {"ID":"54",    
             "QTY":"1",     
             "NAME":"Large",    
             "TOTAL":1.86   
             }, 

            "TOTAL":10.54,          
           "313": 
           {"ID":"313",         
            "QTY":2,        
            "NAME":"Quater Pounder",        
            "TOTAL":8.68        
           } 
        }"  

//and wants to make it:  

var data =    [
   {"ID" : "54",
   "QTY" : "1",   
   "NAME": "Quarter Pounder",  
   "TOTAL": 8.68  
  },    

  {"ID":"313",
   "QTY":2,
   "NAME": "Quater Pounder",
   "TOTAL":8.68
   }
 ]

我得到了如何使用数据透视表,但我感到磕磕绊绊的是,他们希望看到链接,然后是每个税码旁边的价格。这甚至可能吗?

我在SQL Server 2012中使用SSRS或Crystal Reports执行此操作,无论哪种方法都可以实现此目的。

3 个答案:

答案 0 :(得分:0)

我在SQL中做了类似的事情。这是我的代码 - 我没有修改它以适合您的列,但它应该是一个很好的起点。

SELECT  [report_id]
  ,[id]
  ,[label]
  ,[rate]
  ,[per_trip]
  ,[per_mile]
  ,[per_hour]
  ,[per_year]
 FROM Table1

DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
DECLARE @Column1Name AS NVARCHAR(MAX)
DECLARE @Column2Name AS NVARCHAR(MAX)

--Get distinct values of the PIVOT Column 
SELECT @Column1Name= ISNULL(@Column1Name + ',','') 
   + QUOTENAME(label + ' Column1')
FROM (SELECT DISTINCT label FROM [costmodel].[tractor_cost_report] ) AS Labels

SELECT @Column2Name= ISNULL(@Column2Name + ',','') 
   + QUOTENAME(label + ' Column2')
FROM (SELECT DISTINCT label FROM [costmodel].[tractor_cost_report] ) AS Labels

--Prepare the PIVOT query using the dynamic 
-- the Column Names in the query are the values of the [label] field in the rows of data
SET @DynamicPivotQuery = 
N' SELECT id,
    MAX([Wages and Benefits PerTrip]) as WagesBenefitsAmt,      
    MAX([Fuel & Oil PerTrip]) as FuelOilAmt,    
    MAX([Licence PerTrip]) as LicenceAmt,
    MAX([Insurance PerTrip]) as InsuranceAmt,       
    MAX([Monthly Payment PerTrip])  as MonthlyPaymentAmt,
    MAX([Profit PerTrip]) as ProfitAmt,
    MAX([WCB (%) Rate]) as WCBRate,
    MAX([WCB (%) PerTrip]) as WCBAmt,
    MAX([EI ($) PerTrip]) as EIAmt,
    MAX([CPP ($) PerTrip]) as CPPAmt,
    MAX([Statutory Holiday ($) PerTrip]) as StatPayAmt,
    MAX([Vacation (%) Rate]) as VacationRate,
    MAX([Vacation (%) PerTrip]) as VacationAmt,
    MAX([Pension ($) Rate]) as PensionRate,
    MAX([Pension ($) PerTrip]) as PensionAmt

FROM (
    SELECT id, 
        rate,
        per_trip,
        label + '' PerTrip'' as RowPivot1,
        label + '' Rate'' as RowPivot2 
    FROM [Table1] ) a
PIVOT(SUM(per_trip) 
      FOR RowPivot1 IN (' + @Column1Name + ')) AS PVTTable1
PIVOT(SUM(rate) 
      FOR RowPivot2 IN (' + @Column2Name + ')) AS PVTTable2 
GROUP BY id'

--Execute the Dynamic Pivot Query
print @DynamicPivotQuery
EXEC sp_executesql @DynamicPivotQuery

答案 1 :(得分:0)

如果您知道不超过3个链接,则不需要动态SQL。但是您可以修改它以使用动态SQL添加任意数量的内容,或者您​​可以放置​​尽可能多的链接字段(或者适合您的报告页面)。

select TaxCode, 
    Description, 
    ML, 
    SUM(LinkRate) TotalRate,
    max(Case when LinkNo=1 then Link end) Link1,
    max(Case when LinkNo=1 then LinkRate end) LinkRate1,
    max(Case when LinkNo=2 then Link end) Link2,
    max(Case when LinkNo=2 then LinkRate end) LinkRate2,
    max(Case when LinkNo=3 then Link end) Link3,
    max(Case when LinkNo=3 then LinkRate end) LinkRate3
from
    (
    select *, ROW_NUMBER() over (partition by TaxCode, Description, ML Order by Link) LinkNo
    from #table
    ) Links
group by TaxCode, Description, ML

答案 2 :(得分:0)

你可以在水晶报告中做到这一点。

  1. 按税号分组。

  2. 对于所有列,请创建if语句,如下所示

  3. 例如:for Description column。

        if taxcode=Sc001
        then  Description
    else if taxcode=Sc002
        then  Description
    

    对于所有列都是明智的,并且您获得了所需的输出。