English 中文(简体)
2. 动态用词——如何准数据获取一栏标题?
原标题:Dynamic pivot - how to obtain column titles parametrically?
  • 时间:2011-10-16 13:10:15
  •  标签:
  • tsql

我想写一张SAP B1(t-sql)表,按月和月列出所有收入和支出项目。

我已成功用普万塔语写了一张书状,但我不想像Jan-11, Feb-11, Mar-11, 12-11。

相反,我希望各栏标题的定型生成,这样,如果我投入的话:

--------------------------------------
Query - Selection Criteria
--------------------------------------

Posting Date      greater or equal        01.09.10

Posting Date      smaller or equal        31.08.11

[OK]   [Cancel]

彩票将产生以下各栏:

Sep-10, Oct-10, Nov-10, .... Aug-11.

I guess DYNAMIC PIVOT can do the trick. So, I modified one SQL obtained from another forum to suit my purpose, but it does not work. The error message I get is Incorrect Syntax near 20100901.

谁能帮助我找到我的错误?

注:在SAP B1中,[%1]是一个投入变量

我在此问:

/*Section 1*/

DECLARE @listCol VARCHAR(2000)
DECLARE @query VARCHAR(4000)

-------------------------------------
/*Section 2*/

SELECT @listCol = 
STUFF(
   ( SELECT DISTINCT  ],[  + CONVERT(VARCHAR, MONTH(T0.RefDate), 102)
       FROM JDT1 
        FOR XML PATH(  ))
     , 1, 2,   ) +  ]  

------------------------------------
/*Section 3*/

SET @query =  
SELECT * FROM 
(
   SELECT 
        T0.Account,
        T1.GroupMask,
        T1.AcctName,
        MONTH(T0.RefDate) as [Month],   
        (T0.Debit - T0.Credit) as [Amount]
    FROM dbo.JDT1 T0
   JOIN dbo.OACT T1 ON T0.Account = T1.AcctCode 
   WHERE
      T1.GroupMask IN (4,5,6,7) AND
      T0.[Refdate] >=  [%1]  AND
      T0.[Refdate] <=  [%2] 
 ) S
PIVOT 
(
    Sum(Amount)
    FOR [Month] IN ( +@listCol+ )
) AS pvt
 

--------------------------------------------
/*Section 4*/

EXECUTE (@query)
问题回答

我不了解SAP,但想一想:

  • 请参看@listCol,以包含在方括号内收集的编号,例如[07],[08],[09]。 但是,您的代码似乎并未在本指示的开头处加上“。

  • 替换线路

    T0.[Refdate] >=  [%1]  AND
    T0.[Refdate] <=  [%2] 
    

    iii

    T0.[Refdate] >=   [%1]   AND
    T0.[Refdate] <=   [%2]  
    

(I also added a space before the AND in the first of these two lines while I was editing your question.)





相关问题
How to write this T-SQL WHERE condition?

I ve got two tables: TableA Col1 Col2 TableB Col3 Col4 I want to join them together: SELECT * from TableA join TableB ON (...) Now, in place of ... I need to write an expression ...

Customer and Order Sql Statement

TSQL query to select all records from Customer that has an Order and also select all records from customer that does not have an Order. The table Customer contains a primary key of CustomerID. The ...

Recommended way of querying multiple Versioned tables

Have a win 2003 box with MSSQL 2005 running on it. There is a database which is populated every morning with new/modified SalesOrder made the previous day. The database has several tables: SalesOrder, ...

update duplicate record

I have a table with the following fields Id Name IsPublic i need to write a sql query that updates IsPublic to false where name has a duplicate. Only one of the duplicates should have IsPublic = ...

Define variable to use with IN operator (T-SQL)

I have a Transact-SQL query that uses the IN operator. Something like this: select * from myTable where myColumn in (1,2,3,4) Is there a way to define a variable to hold the entire list "(1,2,3,4)"? ...

Selecting records during recursive stored procedure

I ve got a content management system that contains a hierarchical structure of categories, with sub-categories subject to different ordering options at each level. Currently, that s retrieved by a (...

热门标签