选择Query need as function

时间:2012-11-30 09:43:56

标签: sql sql-server

这是我作为SQL函数需要的选择查询,方法是传递INTSID作为此查询返回一组记录的参数。

DECLARE @colsUnpivot AS NVARCHAR(MAX),
        @query As nvarchar(max),    
        @colsPivot as  NVARCHAR(MAX)

select @colsUnpivot = stuff((select ','+ quotename(C.name)
         from sys.columns as C
         where C.object_id = object_id('tblHRIS_ChildDetails') and
               C.name not in ('intCHID', 'intsid')
         for xml path('')), 1, 1, '')

select @colsPivot = STUFF((SELECT  ',' 
                      + quotename(c.name 
                         +'_'+ cast(t.rn as varchar(10)))
                    from 
                    (
                      select row_number() over(partition by intsid order by intCHID) rn
                      from tblHRIS_ChildDetails
                    ) t
                    cross apply sys.columns as C
                   where C.object_id = object_id('tblHRIS_ChildDetails') and
                         C.name not in ('intCHID', 'intsid')
                   group by c.name, t.rn
                   order by t.rn
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query 
  = 'select *
      from
      (
        select col+''_''+cast(rn as varchar(10)) col, value
        from 
        (
            select nvrchildname,
            cast(nvrchildgender as nvarchar(250)) nvrChildGender,
            convert(nvarchar(250), dttchildDOB, 120) dttchildDOB,
            cast(nvrnominee as nvarchar(250)) nvrnominee,
            nvrchildoccupation,
             convert(nvarchar(250),  dttCreatedon, 120)  dttCreatedon,
               convert(nvarchar(250),  dttModifiedOn, 120)  dttModifiedOn,
                 convert(nvarchar(250),  nvrModifiedby, 120)  nvrModifiedby,
                                    row_number() over(partition by intsid order by intCHID) rn
          from tblHRIS_ChildDetails
          where intsid = 463
        ) x
        unpivot
        (
          value
          for col in ('+ @colsunpivot +')
        ) u
      ) x1
      pivot
      (
        max(value)
        for col in  ('+ @colspivot +')
      ) p'
  exec (@query) 

1 个答案:

答案 0 :(得分:0)

使用例如:

DECLARE @Site VARCHAR(1000) = '''Avonmouth'',''Lifton'''
DECLARE @Year VARCHAR(50) = '12'
DECLARE @Period VARCHAR(50) = '10'
DECLARE @Week VARCHAR(50) ='41'

DECLARE @SQL AS VARCHAR(MAX)

SELECT @SQL = 'select Production_Site, [Target],[Action],[Fail]'
SELECT @SQL = @SQL + ' from'
SELECT @SQL = @SQL + ' ('
SELECT @SQL = @SQL + ' select Production_Site, value, Period, YEAR, week'
SELECT @SQL = @SQL + ' from t_Pqe_Grocery'
SELECT @SQL = @SQL + ' unpivot ('
SELECT @SQL = @SQL + ' value'
SELECT @SQL = @SQL + ' for col in (Grocery_Packaging_And_Coding, Grocery_Measurable,'
SELECT @SQL = @SQL + ' Grocery_Appearance, Grocery_Aroma,'
SELECT @SQL = @SQL + ' Grocery_Flavour, Grocery_Texture)) unp'
SELECT @SQL = @SQL + ' ) src '
SELECT @SQL = @SQL + ' pivot '
SELECT @SQL = @SQL + ' ('
SELECT @SQL = @SQL + ' count(value)'
SELECT @SQL = @SQL + ' for value in ([Target], [Action], [Fail])'
SELECT @SQL = @SQL + ' ) piv'
SELECT @SQL = @SQL + ' where Production_Site IN (' + @Site + ') AND YEAR = ' + 
                     CONVERT(varchar(50), CONVERT(BIGINT, @Year)) + ' AND Period = ' + 
                     CONVERT(varchar(50), CONVERT(BIGINT, @Period)) + ' and Week = ' +   
                     CONVERT(varchar(50), CONVERT(BIGINT, @Week)) 
PRINT @SQL
EXECUTE (@SQL)