|
我以前发布过一个关键问题,我已经回答了。然而,这种特殊情况需要一个日期,因为标题和日期将在每次生成数据透视表时更改(未知)。我相信我需要根据一些示例进行动态枢轴/取消枢轴,但我找不到语法。
3 a. C! t& i0 H: i; Z/ ]6 C2 b表格如下:
" S- ^ Y9 L1 w& y vCREATE TABLE [dbo].[PhaseFlowChart]( [pfckey] [int] NULL, [hourlykey] [bigint] NULL, [daykey] [bigint] NULL, [weekkey] [int] NULL, [monthkey] [int] NULL, [bbkey] [int] NULL, [Day] [varchar](100) NULL, [Date] [varchar](100) NULL, [Bull Bear Gap] [varchar](100) NULL, [Monthly] [varchar](100) NULL, [Weekly] [varchar](100) NULL, [Daily] [varchar](100) NULL, [Hour 1] [varchar](100) NULL, [Hour 2] [varchar](100) NULL, [Hour 3] [varchar](100) NULL, [Hour 4] [varchar](100) NULL, [Hour 5] [varchar](100) NULL, [Hour 6] [varchar](100) NULL, [Hour 7] [varchar](100) NULL) ON [PRIMARY]我不需要输出中的任何关键列。因此,这是一个简单的表格选择:( [# O. `! K- t9 C8 _( [
select [DAY],[Date],[Bull Bear Gap],[Monthly],[Weekly],[Daily],[Hour 1],[Hour 2],[Hour 3],[Hour 4],[Hour 5],[Hour 6],[Hour 7] from PhaseFlowChartorder by pfckey asc最后,我在网上找到了该怎么办sql,但经过几天的语法操作,我仍然想不出如何移植它来满足我的需要。
/ E2 G5 ^: B. r' {0 C) lCREATE TABLE #yt ([ID] int,[expense] int,[revenue] int,[date] datetime);INSERT INTO #yt ([ID],[expense],[revenue],[date])VALUES -12- (2,32,32,2013-01- (3,64,56,2013-01- (4,31,32,2013-03-03 00:00:00')DECLARE @cols AS NVARCHAR(MAX), @query AS NVARCHAR(MAX)select @cols = STUFF((SELECT , QUOTENAME(c.col '_' convert(varchar(10),#yt.date,110) from #yt cross apply select 'expense' col union all select 'revenue c FOR XML PATH(''),TYPE ).value('.','NVARCHAR(MAX)1、1、1、1、')set @query = 'SELECT id,' @cols from select id, col ''_'' convert(varchar(10),date,110) new_col, value from #yt unpivot value for col in (expense,revenue) un ) src pivot sum(value) for new_col in @cols ') ) p 'execute(@query);任何/所有的帮助都将不胜感激。) `, B9 o4 l; C5 P
% i$ O3 K- W: [* S 解决方案: |
|