Convert Access crosstab query to SQL Server equivalent

Posted on 2004-11-16
Last Modified: 2008-02-07
Just need a conversion of an Access crosstab query to a SQL Server equivalent.

The query is as follows:

TRANSFORM Count(tblTrans.keyFUser) AS CountOfkeyFUser SELECT Val(Month([Timestamp])) AS [Month] FROM tblTrans WHERE (((Year([Timestamp]))=2004)) GROUP BY Val(Month([Timestamp])) ORDER BY Val(Month([Timestamp])) PIVOT "Users";

tblTrans contains a record for each "transaction" on the site. So the query basically has a row for each month (with each month indicated by its numeric value) and the value of CountOfkeyFUser will be the number of "transactions" in that month.

The structure of tblTrans is as follows:

keyTrans - Int (identity, auto-increment)
keyFUser - Int
Type - SmallInt
ID - Int
Timestamp - smalldatetime

Typical record:

keyTrans = 63 (unique identifier of the transaction)
keyFUser = 24 (user that performed the transaction)
Type = 1 (type of transaction)
ID = 44 (product the transaction relates to)
Timestamp = 30/09/2004 12:37:05 (date/time transaction occurred)

Thanks in advance
Question by:jonnyboy69
    LVL 1

    Accepted Solution

    Maybe I don't understand your problem but why don't the following work:

    SELECT MONTH(Timestamp), COUNT(keyFUser) AS Users FROM tblTrans
    WHERE YEAR(Timestamp) = 2004
    GROUP BY MONTH(Timestamp)
    ORDER BY MONTH(Timestamp)
    LVL 16

    Expert Comment

    That's my understanding of the question as well, I don't think you're going mad!

    Author Comment

    Awww crap I am going mad ... it's been a long day alright! ;)

    The last in a long list of errors from someone elses code and it looked hard! So I thought i'd get someone else to do it. Please have 500 points.

    /switches brain on
    LVL 16

    Expert Comment

    Why do I never see these questions quickly enough? ;)

    Featured Post

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    Suggested Solutions

    Introduced in Microsoft SQL Server 2005, the Copy Database Wizard ( is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
    JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
    Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
    Via a live example, show how to shrink a transaction log file down to a reasonable size.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now