Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8267259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:27:19+00:00 2026-06-08T05:27:19+00:00

I am really struggling with figuring out how to convert a MS Access CrossTab

  • 0

I am really struggling with figuring out how to convert a MS Access CrossTab query into T-SQL to run on SQL2000 or into Linq-to-SQL. What I have is a query that looks like this in Access:

Access Cross Tab Query
And produces this:
Access Cross Tab Query Results

The query basically groups by ItemID and StoreID and Sums the Qty sold, but with the CrossTab query in Access, I am able to have a unique ItemID for each row, a column for each StoreID, and the total Qty for each StoreID/ItemID combination as the value.

How do you construct this in T-SQL 2000? I can build a simple select query with grouping, but it gives me the data in three columns, StoreID, ItemID, and Qty. But what I need is a column for ItemID and a column for every StoreID in the result set

SELECT     Trans.TranSID as StoreID, TransDetail.TranItemID as ItemID, SUM(TransDetail.Qty) AS TotalQtyForStore
    FROM         Trans INNER JOIN
                          TransDetail ON Trans.TranID = TransDetail.TranID INNER JOIN
                          Item ON TransDetail.TranItemID = Item.ItemID
    WHERE     (Trans.TranDate > CONVERT(DATETIME, '2005-01-01 00:00:00', 102)) AND (Trans.TranTypeID = 'so' OR
                          Trans.TranTypeID = 'ca') AND (Trans.TranStatus <> 'v') AND (Item.ItemClassID = 'RHM')
    GROUP BY Trans.TranSID, TransDetail.TranItemID

Which produces this:

enter image description here

But what I really need to do is transform this data so that the StoreID values become columns, like the Access CrossTab query above.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T05:27:20+00:00Added an answer on June 8, 2026 at 5:27 am

    I would do this with dynamic SQL, since you probably don’t know all of the store (TranSID) values in advance. This stored procedure takes start/end date as parameters, and generates a cross-tab. You can probably parameterize some of the other things as well, such as TranStatus and ItemClassID, but it wasn’t clear if those are fixed or variable elements in your query.

    CREATE PROCEDURE dbo.CrossTabByItem
      @StartDate DATETIME,
      @EndDate   DATETIME = NULL
    AS
    BEGIN
      SET NOCOUNT ON;
    
      SET @EndDate = COALESCE(DATEADD(DAY, 1, @EndDate), GETDATE());
    
      DECLARE @sql NVARCHAR(4000); SET @sql = N'';
    
      SELECT @sql = @sql + ',
        [' + TranSID + '] = SUM(CASE WHEN t.TranSID = ''' 
        + TranSID + ''' THEN td.Qty ELSE 0 END)' FROM 
        (
          SELECT DISTINCT TranSID FROM dbo.Trans
          WHERE TranDate >= @StartDate AND TranDate < @EndDate
        ) AS x ORDER BY TranSID;
    
        SET @sql = N'SELECT ItemID = td.TranItemID' + @sql + '
          FROM dbo.Trans AS t
          INNER JOIN dbo.TransDetail AS td
           ON t.TranID = td.TranID
          INNER JOIN dbo.Item AS i
           ON td.TranItemID = i.ItemID
          WHERE t.TranDate >= @StartDate
          AND t.TranDate < @EndDate
          AND t.TranTypeID IN (''so'',''ca'')
          AND t.TranStatus <> ''v''
          AND i.ItemClassID = ''RHM''
          GROUP BY td.TranItemID;';
    
        EXEC sp_executesql @sql, 
          N'@StartDate DATETIME, @EndDate DATETIME', 
          @StartDate, @EndDate;
    END
    GO
    

    So you can call it like this, to get everything from January 1st, 2005 until right now:

    EXEC dbo.CrossTabByItem @StartDate = '20050101';
    

    Or like this, to get just January of 2005:

    EXEC dbo.CrossTabByItem @StartDate = '20050101', @EndDate = '20050131';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm really struggling with LINQ. I've written my query out in SQL (in effect
I'm really struggling with this query. I have 4 tables (http://oberto.co.nz/db-sql.png): Invoice_Payement, Invoice, Client
Really struggling to work this out... I have a text file with data like
Really struggling to figure out extending the immutable Set with a class that will
I'm really struggling with a MySQL query that I'm really hoping someone can help
Really struggling to figure this out. Just trying to take in data from php
I'm really struggling with require.js and jquery mobile. I have a loosely based file
I'm really struggling with this. I have a viewdetails.php page, addnew.php page and a
I am really struggling to understand tail recursion in Erlang. I have the following
I'm really struggling with something that should be a simple matter: Showing a user

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.