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 757989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:25:36+00:00 2026-05-14T15:25:36+00:00

Here is the scenario: I have a table that records the user_id, the module_id,

  • 0

Here is the scenario:
I have a table that records the user_id, the module_id, and the date/time the module was viewed.

eg.

Table: Log
------------------------------
User_ID  Module_ID   Date
------------------------------
1       red         2001-01-01
1       green       2001-01-02
1       blue        2001-01-03
2       green       2001-01-04
2       blue        2001-01-05
1       red         2001-01-06
1       blue        2001-01-07
3       blue        2001-01-08
3       green       2001-01-09
3       red         2001-01-10
3       green       2001-01-11
4       white       2001-01-12

I need to get a result set that has the user_id as the 1st column, and then a column for each module. The row data is then the user_id and the count of the number of times that user viewed each module.

eg.

---------------------------------
User_ID  red green   blue    white
---------------------------------
1       2   1       2       0
2       0   1       1       0
3       1   2       1       0
4       0   0       0       1

I was initially thinking that I could do this with PIVOT, but no dice; the database is a converted SQL Server 2000 DB that is running in SQL Server 2005. I’m not able to change the compatibility level, so pivot is out.

The other catch is that the modules will vary, and it isn’t feasible to re-write the query every time a module is added or removed. This means that I can’t hard-code in the modules because I don’t know in advance which will and will not be installed.

How can I accomplish this?

  • 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-05-14T15:25:36+00:00Added an answer on May 14, 2026 at 3:25 pm

    PIVOT can be simulated with CASE and GROUP BY

    select
        [user_id],
        sum(case when [Module_ID] = 'red' then 1 else 0 end) as red,
        sum(case when [Module_ID] = 'green' then 1 else 0 end) as green,
        sum(case when [Module_ID] = 'blue' then 1 else 0 end) as blue,
        sum(case when [Module_ID] = 'white' then 1 else 0 end) as white
    from [log]
    group by
        [user_id]
    

    Of course this doesn’t work if the modules vary (as stated in the question) but then, PIVOT has the same problem.

    Dynamically generating some sql overcomes this problem but this solution smells a bit!

    declare @sql nvarchar(max)
    
    set @sql = '
    select
        [user_id],'
    
    select @sql = @sql + '
        sum(case when [Module_ID] = ''' + replace([Module_ID], '''','''''') + ''' then 1 else 0 end) as [' + replace([Module_ID], '''','') + '],'
    from (select distinct [Module_ID] from [log]) as moduleids
    
    set @sql = substring(@sql,1,len(@sql)-1) + '
    from [log]
    group by
        [user_id]
    '
    print @sql
    exec sp_executesql @sql
    

    Note that this may be vulnerable to sql-injection if the module id data can’t be trusted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's my scenario - I have an SSIS job that depends on another prior
Here's the scenario: You have a Windows server that users remotely connect to via
Here's the scenario: I have a set of buttons that I want to bind
Here's my scenario: I have a simple stored procedure that removes a specific set
Here's the scenario: I have 2 tables: CREATE TABLE dbo.API_User ( id int NOT
Here's the scenario: I have a textbox and a button on a web page.
Here's the scenario: You have an ASP.Net application supported by a Microsoft SQL Server
Here's the scenario I have a Grid with some TextBlock controls, each in a
Here's my scenario: I have a page containing several links; each link is meant
Here is the scenario: I have two asp pages. a.aspx is layout and b.aspx

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.