2016-09-07 216 views
0

我想要一個查詢透視一些數據,它工作正常,除了我發現我們的應用程序開發進一步下游的數據問題。我注意到的是,由於我需要最大化,因爲需要一個聚合函數的樞軸,它是字面上最大的價值,而不考慮列不明顯相同。我是新來的pivoting,所以我可能做錯了我想要的結果。任何幫助或方向將不勝感激。SQL Server:與分組樞軸

查詢:

SELECT 
    @cols = STUFF((SELECT ',' + QUOTENAME(proto_name) 
        FROM #temp_rackplan 
        GROUP BY proto_name 
        ORDER BY proto_name 
        FOR XML PATH(''),TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '') 
SET @query = 'SELECT 
        dept_catg_grp_desc AS [Category Group] 
        ,dept_category_desc AS [Category] 
        ,fineline_desc AS [Fineline] 
        ,Supplier 
        ,ty_cust_choice_qty AS [CC] 
        ,season_code AS [SC] 
        ,set_strategy_desc AS [Set Strategy] 
        ,ty_landed_cost_amt AS [Landed Cost] 
        ,ty_freight_factor_pct AS [FF%] 
        ,ty_cost_w_ff AS [Cost w/FF] 
        ,ty_retail_amt AS [Retail $] 
        ,mu_pct AS [MU%] 
,in_store_wm_yr_wk_id AS [In Str Wk] 
,ty_start_wm_yr_wk_id AS [Start Wk] 
,ty_end_wm_yr_wk_id AS [End Wk] 
,ty_md_wm_yr_wk_id AS [MD Wk] 
,fixture_group_desc AS [Fixture] 
,Brand 
,' + @cols + 
' 
FROM 
(
SELECT 
    dept_catg_grp_desc 
    ,dept_category_desc 
    ,fineline_desc 
    ,Supplier 
    ,ty_cust_choice_qty 
    ,season_code 
    ,set_strategy_desc 
    ,ty_landed_cost_amt 
    ,ty_freight_factor_pct 
    ,ty_cost_w_ff 
    ,ty_retail_amt 
    ,mu_pct 
    ,in_store_wm_yr_wk_id 
    ,ty_start_wm_yr_wk_id 
    ,ty_end_wm_yr_wk_id 
    ,ty_md_wm_yr_wk_id 
    ,fixture_group_desc 
    ,Brand 
    ,proto_name 
    ,trgt_rack_cnt 
FROM 
    #temp_rackplan 
) AS x 
PIVOT 
(
    MAX(trgt_rack_cnt) 
    FOR proto_name in (' + @cols + ') 
) AS p ' 

查詢結果:

Catgrp Category Fineline Set Strategy Fixture Proto A Proto B Proto C 
---------------------------------------------------------------------------- 
SHOES MENS  254  10-50   4WAY  2  1  1 
SHOES MENS  254  10-50   H-RACK 2  1  1 
SHOES MENS  254  60-90   4WAY  2  1  1 
SHOES MENS  254  60-90   H-RACK 2  1  1 
SHOES MENS  2920  10-50   4WAY  2  1  1 
SHOES MENS  2920  10-50   H-RACK 2  1  1 
SHOES MENS  2920  60-90   4WAY  2  1  1 
SHOES MENS  2920  60-90   H-RACK 2  1  1 

期望的結果(匹配數據庫):

Catgrp Category Fineline Set Strategy Fixture Proto A Proto B Proto C 
---------------------------------------------------------------------------- 
SHOES MENS  254  10-50   4WAY  2  1  1 
SHOES MENS  254  10-50   H-RACK 1  1  1 
SHOES MENS  254  60-90   4WAY  1  1  1 
SHOES MENS  254  60-90   H-RACK 1  1  1 
SHOES MENS  2920  10-50   4WAY  1  1  1 
SHOES MENS  2920  10-50   H-RACK 1  1  1 
SHOES MENS  2920  60-90   4WAY  2  1  1 
SHOES MENS  2920  60-90   H-RACK 1  1  1 

回答

0

基礎上的研究,我是不是能夠使用旋轉功能每報告的要求。這裏是我創建的解決方案,輸出正確的數據(粗略草稿)

DECLARE @ps_id NVARCHAR(5) = 2519 
DECLARE @cols NVARCHAR(MAX) 
DECLARE @curproto NVARCHAR(50) 
DECLARE @loopcnter INT = 0 
DECLARE @protocnt INT = (SELECT DISTINCT COUNT(DISTINCT proto_name) FROM apparel_planning_reporting.dbo.Plan_Proto WHERE planning_session_id = @ps_id) 
DECLARE @looptbl TABLE 
(
id INT, 
proto_name NVARCHAR(50) 
) 

CREATE TABLE #temploop(fineline INT, strategy_zone_group_cd INT) 

--Insert initial records into proto table 
INSERT INTO @looptbl 
SELECT DISTINCT 0 AS id, proto_name FROM apparel_planning_reporting.dbo.Plan_Proto WHERE planning_session_id = @ps_id 

--Generate record id 
UPDATE @looptbl SET @loopcnter = id = @loopcnter + 1 

DECLARE @sqlexe nvarchar(max) 
--Loop to generate report 
SET @loopcnter = 1 
WHILE(@loopcnter <= @protocnt) 
BEGIN 
     SET @curproto = (SELECT proto_name FROM @looptbl WHERE id = @loopcnter) 
     SET @sqlexe = 'ALTER TABLE #temploop ADD [' + @curproto + '] DECIMAL(18,2)' 
     EXEC (@sqlexe) 
     SET @sqlexe = 'INSERT INTO #temploop (fineline, strategy_zone_group_cd,['[email protected]+']) SELECT pras.fineline_nbr, pras.strategy_zone_group_cd, pras.trgt_rack_cnt FROM apparel_planning_reporting.dbo.Plan_Rack_Alloc_Store AS pras, apparel_planning_reporting.dbo.Plan_Proto AS pp WHERE COALESCE(pras.planning_session_id, pp.planning_session_id) =' + @ps_id + ' AND pras.str_collection_id = pp.plan_proto_id AND pp.proto_name = '''[email protected]+''' OPTION(MAXDOP 12)' 
     EXEC (@sqlexe) 
     SET @loopcnter = @loopcnter + 1 
END 
--SELECT * FROM #temploop 

SELECT @cols = STUFF((
      SELECT ',MAX(' + QUOTENAME(proto_name) + ') AS ' + QUOTENAME(proto_name) 
      FROM @looptbl 
      GROUP BY proto_name 
      ORDER BY proto_name 
      FOR XML PATH('') 
       ,TYPE 
      ).value('.', 'NVARCHAR(MAX)'), 1, 1, '') 

SET @sqlexe = ' 
SELECT 
    fineline 
    ,strategy_zone_group_cd 
    ,'[email protected]+' 
FROM #temploop 
GROUP BY fineline, strategy_zone_group_cd 
' 
EXEC(@sqlexe) 

drop table #temploop