2016-09-07 26 views
0

私はクエリのピボットをいくつかのデータにしようとしています。私が気づいたのは、集計関数を必要とするピボットのために最大化する必要があるため、列がはっきりと同じではないことを考慮に入れずに文字通り値を最大にしていることです。私は旋回することに新しいので、私が望む結果に間違ったことをしているかもしれません。どんな助けや方向性も高く評価されます。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 
関連する問題