convert rows to columns in Access

后端 未结 4 1008
深忆病人
深忆病人 2021-01-24 06:21

I have read many question on Stack Overflow related to my problem, but I don\'t think they quite address my problem. Basically I download a XML dataset with lots of data, and in

相关标签:
4条回答
  • 2021-01-24 06:33

    Try this...in sql query may be it is your answer

       SELECT NameID , [AA] as AA,[BB] as BB,[CC] as CC,[DD]  as DD     
            FROM        
            (
                SELECT Name,Data,NameID FROM Table1
            )PivotData
    
            PIVOT
            (
                max(Data) for Name in ([AA],[BB],[CC],[DD])     
            ) AS Pivoting
    
    0 讨论(0)
  • 2021-01-24 06:44

    I think you need to this

    1) Take all your Table1 as it is in SQL Server

    2) Then run following query

    DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)
    
    select @cols = STUFF((SELECT distinct ',' + QUOTENAME(Name) 
                        from [Table1]
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    set @query = 'SELECT countryid,' + @cols + ' 
                from 
                (
                    select NameID, Name 
                    from Table1 cc                
                ) T 
                pivot 
                (
                    max (Name)
                    for languagename in (' + @cols + ')
                ) p '
    
    execute sp_executesql @query;   
    
    0 讨论(0)
  • 2021-01-24 06:45
    DECLARE @Table1 TABLE ([Index] INT,[Name] CHAR(2),[Data] INT,[NameID] INT) 
    
    INSERT INTO @Table1
    VALUES
     (1,'AA',14,1),              
     (2,'BB',42,1),            
     (3,'CC',64,1),             
     (4,'DD',61,1),             
     (5,'AA',15,2),                
     (6,'BB',35,2),
     (7,'CC',67,2),        
     (8,'DD',63,2),
     (9,'AA',9,3),
    (10,'CC',10,3),
    (11,'BB',83,4),
    (12,'CC',1,4),
    (13,'DD',87,4)
    
    SELECT [NameID] , ISNULL([AA], '') AS [AA], ISNULL([BB], '') AS [BB]
            , ISNULL([CC], '') AS [CC], ISNULL([DD], '') AS [DD]
    FROM  
        (
        SELECT NAME, DATA, NAMEID
        FROM @Table1
        )q
        PIVOT
            (
            SUM(DATA)
            FOR NAME
            IN ([AA], [BB], [CC], [DD])
            )P
    

    Result Set

    NameID  AA    BB    CC  DD
    1       14    42    64  61
    2       15    35    67  63
    3       9           10  
    4             83    1   87
    
    0 讨论(0)
  • 2021-01-24 06:55

    To accomplish this in Access, all you need to do is

    TRANSFORM Sum([Data]) AS SumOfData
    SELECT [NameID]
    FROM [Table1]
    GROUP BY [NameID]
    PIVOT [Name]
    

    edit re: revised question

    To handle some [Name]s differently we would need to assemble the results (Sum()s, etc.) first, and then crosstab the results

    For test data in [Table1]:

    Index  Name  Data        NameID
    -----  ----  ----------  ------
        1  AA    14               1
        2  BB    2013-12-01       1
        3  CC    64               1
        4  DD    61               1
        5  AA    15               2
        6  BB    2013-12-02       2
        7  CC    67               2
        8  DD    63               2
        9  AA    9                3
       10  CC    10               3
       11  AA    19               2
       12  BB    2013-12-02       2
       13  CC    21               2
       14  DD    12               2
       15  BB    2013-12-04       4
       16  CC    1                4
       17  DD    87               4
    

    the query

    TRANSFORM First(columnData) AS whatever
    SELECT [NameID]
    FROM
        (
                SELECT [NameID], [Name], Sum([Data]) AS columnData
                FROM [Table1]
                WHERE [Name] <> 'BB'
                GROUP BY [NameID], [Name]
            UNION ALL
                SELECT DISTINCT [NameID], [Name], [Data]
                FROM [Table1]
                WHERE [Name] = 'BB'
        )
    GROUP BY [NameID]
    PIVOT [Name]
    

    produces

    NameID  AA  BB          CC  DD
    ------  --  ----------  --  --
         1  14  2013-12-01  64  61
         2  34  2013-12-02  88  75
         3  9               10    
         4      2013-12-04  1   87
    
    0 讨论(0)
提交回复
热议问题