如何禁止在动态表上显示特定列

时间:2015-03-02 16:58:33

标签: sql-server dynamic sql-server-2008-r2

我有两个动态表格,我要加入。我想禁止显示两个表中的特定列。到目前为止,这是我的疑问:

SELECT a.*
    ,b.*
FROM APEXCOUNTYREVIEW_REPORT a
LEFT JOIN TBL_LOW_VOLUME_test b 
    ON a.fips_data_type = b.fips_datatype

这两个都是具有大量列的动态表。我想压制表中间的列。 例如,Table1C1, C2, C3, C4, C5, C6, C7,...etc.我不想提取列C3, C4 and C5,但希望显示C1, C2, C6, C7等等。由于这两个都是动态表,因此在执行查询之前不知道列数。我正在使用SSMS 2008 R2。

有没有办法用SQL查询执行此操作?

这里是构建table2,tbl_low_voulume_test的查询: 我还试图在table2上的列名中添加一个文本,因为它们与table1上的列名相同,这是动态创建的,并且我得到了各种无效的错误消息。如何添加文字" Flag"在动态列名称?

以下查询运行良好。

DECLARE @T AS TABLE(Y INT NOT NULL PRIMARY KEY);

DECLARE
@COLS AS NVARCHAR(MAX),
@COLNAMES AS NVARCHAR(MAX),
@Y    AS INT,
@SQL  AS NVARCHAR(MAX);


SET @COLS = STUFF(
(SELECT N',' + QUOTENAME(Y)
FROM (SELECT DISTINCT RECMONTH AS Y FROM REPORTING.dbo.TBL_LOW_VOLUME A

WHERE (RECMONTH>= '2005-01')
AND RECMONTH <= CONVERT(VARCHAR(7), getdate(), 120)
) AS Y
ORDER BY Y
FOR XML PATH('')),
1, 1, N'');

SET @COLNAMES = STUFF(
(SELECT N',ISNULL(' + QUOTENAME(Y)+',0) AS '+ QUOTENAME(Y) 
FROM (SELECT DISTINCT RECMONTH AS [Y] FROM REPORTING.dbo.TBL_LOW_VOLUME A

WHERE (RECMONTH>= '2005-01')
AND RECMONTH <= CONVERT(VARCHAR(7), getdate(), 120)
) AS Y
ORDER BY Y
FOR XML PATH('')),
1, 1, N'');

IF OBJECT_ID('TBL_LOW_VOLUME_test') IS NOT NULL 
BEGIN DROP TABLE reporting.dbo.TBL_LOW_VOLUME_test END 


SET @SQL = N'SELECT * INTO dbo.TBL_LOW_VOLUME_test 
FROM (
SELECT CONVERT(DATE, GETDATE()) AS RUN_DATE, FIPS+DATATYPE AS FIPS_DATATYPE,
 FIPS,DATATYPE,' +@COLNAMES + ' 
FROM (SELECT A.[FIPS], A.DATATYPE
      ,RECMONTH
      ,[LV_FLAG]
    FROM dbo.TBL_LOW_VOLUME A
    where FIPS is not NULL and isnumeric(FIPS) = 1 and len(ltrim(FIPS)) = 5
     and FIPS not in (''-0000'',''00000'',''-0001'')
     and FIPS <= ''56999''

 ) AS D
PIVOT(SUM([LV_FLAG]) FOR RECMONTH IN(' + @COLS + N')) AS P) AS Z';

EXEC SP_EXECUTESQL @SQL;

这是我的两张桌子的照片。 表1:

col1,col2,col3,col4,col5,col6等...... 第1列到第5列是静态的,从第c016列开始,它变为动态,每月的每个月末都会增加一个新月。

表2: 可乐,colb,colc,冷,油菜,colf等... 列a和b都是静态的,其余的都是动态的,每月的每个月末它都会增加一个新月。

这应该是我想要显示的最终结果。

col1,col2,col6等...(table1上的所有动态列),cola,colc,cole等...(table2上的所有动态列)

所以我想从每个表中选择我需要的列。

这是我打印@sql后的查询: 对于此表,我不想显示Run_Date和FIPS_DATATYPE,只显示所有日期列。

SELECT * INTO dbo.TBL_LOW_VOLUME_test 
FROM (
SELECT CONVERT(DATE, GETDATE()) AS RUN_DATE, FIPS+DATATYPE AS FIPS_DATATYPE,
 ISNULL([2005-01],0) AS [2005-01],ISNULL([2005-02],0) AS [2005-02],ISNULL([2005-03],0) AS [2005-03],ISNULL([2005-04],0) AS [2005-04],
ISNULL([2005-05],0) AS [2005-05],ISNULL([2005-06],0) AS [2005-06],ISNULL([2005-07],0) AS [2005-07],ISNULL([2005-08],0) AS [2005-08],
ISNULL([2005-09],0) AS [2005-09],ISNULL([2005-10],0) AS [2005-10],ISNULL([2005-11],0) AS [2005-11],ISNULL([2005-12],0) AS [2005-12],
ISNULL([2006-01],0) AS [2006-01],ISNULL([2006-02],0) AS [2006-02],ISNULL([2006-03],0) AS [2006-03],ISNULL([2006-04],0) AS [2006-04],
ISNULL([2006-05],0) AS [2006-05],ISNULL([2006-06],0) AS [2006-06],ISNULL([2006-07],0) AS [2006-07],ISNULL([2006-08],0) AS [2006-08],
ISNULL([2006-09],0) AS [2006-09],ISNULL([2006-10],0) AS [2006-10],ISNULL([2006-11],0) AS [2006-11],ISNULL([2006-12],0) AS [2006-12],
ISNULL([2007-01],0) AS [2007-01],ISNULL([2007-02],0) AS [2007-02],ISNULL([2007-03],0) AS [2007-03],ISNULL([2007-04],0) AS [2007-04],
ISNULL([2007-05],0) AS [2007-05],ISNULL([2007-06],0) AS [2007-06],ISNULL([2007-07],0) AS [2007-07],ISNULL([2007-08],0) AS [2007-08],
ISNULL([2007-09],0) AS [2007-09],ISNULL([2007-10],0) AS [2007-10],ISNULL([2007-11],0) AS [2007-11],ISNULL([2007-12],0) AS [2007-12],
ISNULL([2008-01],0) AS [2008-01],ISNULL([2008-02],0) AS [2008-02],ISNULL([2008-03],0) AS [2008-03],ISNULL([2008-04],0) AS [2008-04],
ISNULL([2008-05],0) AS [2008-05],ISNULL([2008-06],0) AS [2008-06],ISNULL([2008-07],0) AS [2008-07],ISNULL([2008-08],0) AS [2008-08],
ISNULL([2008-09],0) AS [2008-09],ISNULL([2008-10],0) AS [2008-10],ISNULL([2008-11],0) AS [2008-11],ISNULL([2008-12],0) AS [2008-12],
ISNULL([2009-01],0) AS [2009-01],ISNULL([2009-02],0) AS [2009-02],ISNULL([2009-03],0) AS [2009-03],ISNULL([2009-04],0) AS [2009-04],
ISNULL([2009-05],0) AS [2009-05],ISNULL([2009-06],0) AS [2009-06],ISNULL([2009-07],0) AS [2009-07],ISNULL([2009-08],0) AS [2009-08],
ISNULL([2009-09],0) AS [2009-09],ISNULL([2009-10],0) AS [2009-10],ISNULL([2009-11],0) AS [2009-11],ISNULL([2009-12],0) AS [2009-12],
ISNULL([2010-01],0) AS [2010-01],ISNULL([2010-02],0) AS [2010-02],ISNULL([2010-03],0) AS [2010-03],ISNULL([2010-04],0) AS [2010-04],
ISNULL([2010-05],0) AS [2010-05],ISNULL([2010-06],0) AS [2010-06],ISNULL([2010-07],0) AS [2010-07],ISNULL([2010-08],0) AS [2010-08],
ISNULL([2010-09],0) AS [2010-09],ISNULL([2010-10],0) AS [2010-10],ISNULL([2010-11],0) AS [2010-11],ISNULL([2010-12],0) AS [2010-12],
ISNULL([2011-01],0) AS [2011-01],ISNULL([2011-02],0) AS [2011-02],ISNULL([2011-03],0) AS [2011-03],ISNULL([2011-04],0) AS [2011-04],
ISNULL([2011-05],0) AS [2011-05],ISNULL([2011-06],0) AS [2011-06],ISNULL([2011-07],0) AS [2011-07],ISNULL([2011-08],0) AS [2011-08],
ISNULL([2011-09],0) AS [2011-09],ISNULL([2011-10],0) AS [2011-10],ISNULL([2011-11],0) AS [2011-11],ISNULL([2011-12],0) AS [2011-12],
ISNULL([2012-01],0) AS [2012-01],ISNULL([2012-02],0) AS [2012-02],ISNULL([2012-03],0) AS [2012-03],ISNULL([2012-04],0) AS [2012-04],
ISNULL([2012-05],0) AS [2012-05],ISNULL([2012-06],0) AS [2012-06],ISNULL([2012-07],0) AS [2012-07],ISNULL([2012-08],0) AS [2012-08],
ISNULL([2012-09],0) AS [2012-09],ISNULL([2012-10],0) AS [2012-10],ISNULL([2012-11],0) AS [2012-11],ISNULL([2012-12],0) AS [2012-12],
ISNULL([2013-01],0) AS [2013-01],ISNULL([2013-02],0) AS [2013-02],ISNULL([2013-03],0) AS [2013-03],ISNULL([2013-04],0) AS [2013-04],
ISNULL([2013-05],0) AS [2013-05],ISNULL([2013-06],0) AS [2013-06],ISNULL([2013-07],0) AS [2013-07],ISNULL([2013-08],0) AS [2013-08],
ISNULL([2013-09],0) AS [2013-09],ISNULL([2013-10],0) AS [2013-10],ISNULL([2013-11],0) AS [2013-11],ISNULL([2013-12],0) AS [2013-12],
ISNULL([2014-01],0) AS [2014-01],ISNULL([2014-02],0) AS [2014-02],ISNULL([2014-03],0) AS [2014-03],ISNULL([2014-04],0) AS [2014-04],
ISNULL([2014-05],0) AS [2014-05],ISNULL([2014-06],0) AS [2014-06],ISNULL([2014-07],0) AS [2014-07],ISNULL([2014-08],0) AS [2014-08],
ISNULL([2014-09],0) AS [2014-09],ISNULL([2014-10],0) AS [2014-10],ISNULL([2014-11],0) AS [2014-11],ISNULL([2014-12],0) AS [2014-12],
ISNULL([2015-01],0) AS [2015-01],ISNULL([2015-02],0) AS [2015-02]

1 个答案:

答案 0 :(得分:0)

  

如何在动态列名称上添加“Flag”文本?

在我看来,你想要替换

QUOTENAME(Y)

QUOTENAME(Y+'Flag')

它在SET @COLSSET @COLNAMES语句中都会出现。

编辑:要过滤掉列,查看最终选择,您正在执行SELECT * FROM(指定静态列+ COLNAMES的派生表)。

因此,要过滤掉列,请将它们从派生表中过滤掉。它们必须是静态列RUN_DATE,FIPS_DATATYPE,FIPS,DATATYPE或COLNAMES中的一个列,您可以使用REPLACE函数过滤掉它们。

如果我不了解问题,请发布当前查询的示例输出以及要过滤的行。

2ND编辑:要从结果中取出Run_Date和FIPS_DATATYPE,只需将它们从中级选择中取出:

SET @SQL = N'SELECT * INTO dbo.TBL_LOW_VOLUME_test 
FROM (
SELECT  ' +@COLNAMES + ' 
FROM ...