简体   繁体   中英

MS Access 2007 pivoting data in query and calling from Excel

I'm a bit stumped on this. There's similar threads here on Stackoverflow, but I haven't found an answer, and it's actually hard to know how to describe what I'm searching for to put into the search box.

I have this query in an MS Access database, which returns items sold grouped by product and total quantity. The way the data is stored, it's broken down by year, and then again by month in the year (FiscalPeriod 01-12 (Jan-Dec)). At the moment, my query looks like this:

在此处输入图片说明

SELECT CI_item.ItemCode, CI_item.ItemCodeDesc, Im_ItemWhseHistoryByPeriod.FiscalCalYear, Im_ItemWhseHistoryByPeriod.FiscalCalPeriod, ([QuantitySold]+[QuantityIssued]-[QuantityReturnedCustomer]) AS Quantity FROM CI_item INNER JOIN Im_ItemWhseHistoryByPeriod ON CI_item.ItemCode = Im_ItemWhseHistoryByPeriod.ItemCode;

I'm trying to figure out a way of pulling at the months into their own columns so I can achieve something like this:

在此处输入图片说明

I've tried some experimenting with the "Pivot View" in the Access query, but this doesn't give me what I want, and I won't be able to query it from Excel using SQL statements. I considered breaking this down into lots of different queries pulled together, but I just wondered if anyone knows of a better way. I'm not an expert at all at SQL queries, particularly in the awkward Access editor.

Many thanks in advance!

By imagining data i tried like this

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT ',' + QUOTENAME(fiscal_per_month) 
                    from table
                    group by fiscal_per_month
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT item_code,item_code_desc,' + @cols + ',total from 
             (
                select *,sum(quantity)over(partition by year) as total
                from table
            ) x
            pivot 
            (
                sum(quantity)
                for months in (' + @cols + ')
            ) p '

exec(@query);

Duh. This is what the "Crosstab" query is for, so I solved this myself. Running through the wizard produced this code, which works well:

TRANSFORM Sum(qry_AllProducts.[Quantity]) AS SumOfQuantity

SELECT qry_AllProducts.[ItemCode], qry_AllProducts.[ItemCodeDesc], qry_AllProducts.[FiscalCalYear], Sum(qry_AllProducts.[Quantity]) AS [Total Of Quantity] FROM qry_AllProducts GROUP BY qry_AllProducts.[ItemCode], qry_AllProducts.[ItemCodeDesc], qry_AllProducts.[FiscalCalYear] PIVOT qry_AllProducts.[FiscalCalPeriod];

The only trick is that you can't actually pull this via an ADODB SQL command from Excel, so I had to create an additional "Make Table" query, pointing to the results of the crosstab, which then sits as a temp table in the database. This can then be queried from Excel. I'll need to figure out how it should refresh, but this works. Thanks for looking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM