0

我正在努力寻找从 SQL Server 中的动态数据透视表到 Crystal Report 或合并到存储过程的报告解决方案。

DECLARE @Columns as VARCHAR(MAX)

SELECT @Columns = COALESCE(@Columns + ', ','') + QUOTENAME(loc_id)
FROM
   (SELECT DISTINCT loc_id 
    FROM request_hd 
    WHERE entry_date = '2018-06-23' 
      AND valid = '1') AS B
ORDER BY B.loc_id

DECLARE @SQL as VARCHAR(MAX)

SET @SQL = 'SELECT item_code,item_ref,item_des,Shelf,WH_stock,WH_stockchk_qty, ' + @Columns + '
FROM
(
  SELECT I.item_code,
  I.item_ref,
  I.item_des,
  I.unit_qty,
  L.loc_id AS LocationName, 
     (select Sum(qty) from stock where stock.item_code = I.item_code and loc_id = ''901'' ) as WH_stock,
(select isnull(sum(qty),0) from stock_chk where stock_chk.item_code = I.item_code and loc_id like ''901'') as WH_stockchk_qty,
 (CAST( (Q.shelf_desc) AS VARCHAR)+''-''+CAST( (Q.shelf_rowSCOUNT) AS VARCHAR)+CAST( (Q.shelf_rowlength) AS VARCHAR)) as Shelf
from request_tran I, request_hd L, shelf_detail P, shelf_mast Q
 where I.entry_no= L.entry_no and I.item_code=P.item_code and P.shelf_id= Q.shelf_id 
 and P.sec_id= Q.sec_id and   L.entry_date >= ''2018-06-23'' and L.entry_date <= ''2018-06-23''  and L.valid = ''1''


) as PivotData
PIVOT
(
   SUM(unit_qty)
   FOR LocationName IN (' + @Columns + ')
) AS PivotResult
ORDER BY Shelf'

EXEC(@SQL)

根据我的概念,此代码运行良好。

结果

这是结果

我想要水晶报表中的这个报告,我尝试了很多东西,不幸的是它失败了

请在 SQL Server 的 PIVOT 表中是新的。请帮我解决这个问题

4

1 回答 1

0

使用原始数据并让 Crystal 使用 CrossTab 进行数据透视。这将使事情变得简单,并确保结果集始终具有相同的列。

于 2018-06-24T21:08:28.033 回答