简体   繁体   English

如何在Crystal Reports for Visual Studio中使用动态列来实现透视报表?

[英]How do I achieve a pivot report with a dynamic columns in Crystal Reports for Visual Studio?

I have a stored procedure (SQL), it is make with a pivot query with dynamic columns . 我有一个存储过程(SQL),它是由带有动态列数据透视查询组成的。 Some querys generate 2 or more columns, it all depends by the parameter. 某些查询会生成2列或更多列,这全部取决于参数。 How do I achieve a report in Crystal Reports for Visual Studio (C# or VB) with this dynamic columns pivot? 如何使用此动态列枢轴在Crystal Reports for Visual Studio (C#或VB)中实现报表

This can be done. 可以做到的。 Here is an example below. 这是下面的例子。

DECLARE @ColumnString varchar(200), 
    @sql varchar(1000)


CREATE TABLE #ColumnValue
(
  Value varchar(500)
)

INSERT INTO #ColumnValue (Value)
SELECT DISTINCT '[' + 'value' + Convert(Varchar(20),ROW_NUMBER() Over(Partition by id Order by id )) + ']'
FROM Test

SELECT @ColumnString = COALESCE(@ColumnString + ',', '') + Value
FROM #ColumnValue
Drop table #ColumnValue



SET @sql =
'
SELECT *
FROM
(
SELECT
        id,name,val,''value'' + Convert(Varchar(20),ROW_NUMBER() Over(Partition by id Order by id ))as [values]
FROM Test
) AS P
    PIVOT
(
    MAX(val) FOR [values] IN ('+@ColumnString+')
) AS pv
'

--print @sql 
EXEC (@sql)

I could resolve this, and this is how I did it for future similar problems: 我可以解决这个问题,这是我为以后出现类似问题所做的工作:

  • First. 第一。 My stored procedure makes a query with a pivot with dynamic columns. 我的存储过程使用带有动态列的数据透视表进行查询。 I had to remove the pivot from my query, so only select the 3 same columns with a lot of rows. 我不得不从查询中删除数据透视 ,因此只能选择3个相同的列并包含很多行。

The results (without pivot) is like this: 结果(无枢轴)如下所示:

ClaveLMG Calificacion   Resultado
i7p-TP2 P1_1Excelente   100.0
i7p-TP2 P1_2Bueno       0.0
i7p-TP2 P1_3Regular     0.0
i7p-TP2 P1_4Malo        0.0
78-BD1  P1_1Excelente   100.0
78-BD1  P1_2Bueno       0.0
78-BD1  P1_3Regular     0.0
78-BD1  P1_4Malo        0.0
41-TP2  P1_1Excelente   100.0
41-TP2  P1_2Bueno       0.0
41-TP2  P1_3Regular     0.0
41-TP2  P1_4Malo        0.0
42-TC2  P1_1Excelente   100.0
42-TC2  P1_2Bueno       0.0
42-TC2  P1_3Regular     0.0
42-TC2  P1_4Malo        0.0
  • Doing a research from how to use Crystal Reports, when you create a report from Crystal Report, yo have to select an assistant. 根据如何使用Crystal Reports进行研究,从Crystal Report创建报表时,您必须选择一个助手。 I select Crosstabs . 我选择“ 交叉表”
  • Finally, after select my stored procedure (without pivot), in the crosstab assistant, you have to add rows , columns , and summary fields from the fields that generate my stored procedure. 最后,选择我的存储过程(无枢轴)后,在交叉表助手中,您必须从生成我的存储过程的字段中添加rowcolumnsummary字段 Almost like when you coding a pivot. 就像在编码轴时一样。

    ____________ i7p-TP2 78-BD1 41-TP2 42-TC2 P1_1Excelente 100.000000 100.000000 100.000000 100.000000 P1_2Bueno 0.000000 0.000000 0.000000 0.000000 P1_3Regular 0.000000 0.000000 0.000000 0.000000 P1_4Malo 0.000000 0.000000 0.000000 0.000000

The result from this is a beatiful report like a pivot query. 这样的结果是一个漂亮的报告,如数据透视查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Crystal报表创建动态报表-Visual Studio 2008? - How to create dynamic reports with crystal reports - Visual Studio 2008? 如何在Visual Studio 2008中创建列式报表-Crystal Reports - How to create a columnar report in Visual Studio 2008 - Crystal Reports 适用于 Visual Studio 2017 的 Crystal Reports - 导出报表 - Crystal Reports for Visual Studio 2017 - Exporting a Report 我有用于Visual Studio 2010的SAP Crystal Reports,但Crystal Report Viewer没有创建其组件 - I have SAP Crystal Reports for Visual Studio 2010 but Crystal Report Viewer is not creating its component 如何使Crystal Reports与Visual Studio的较新版本一起使用? - How do I make Crystal Reports work with newer versions of Visual Studio? 如何使用Crystal Reports动态修改报告? - How do I dynamically modify a report using Crystal Reports? Visual Studio Crystal报表 - Visual Studio Crystal Reports 如何在Visual Studio解决方案C#中添加现有的Crystal报表文件 - How do I add existing crystal report file in Visual Studio solution C# Visual Studio 2017水晶报表错误加载报表失败 - Visual Studio 2017 Crystal Reports Error Load Report Failed Visual Studio 2008的Crystal Reports:报表和子报表中的数据源混合吗? - Crystal Reports for Visual Studio 2008: Mix of data sources in Report and subreport?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM