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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:08:37+00:00 2026-06-01T20:08:37+00:00

I have a query that retrieves all agents and thier modules, the result set

  • 0

I have a query that retrieves all agents and thier modules, the result set will return 1 row per module.

SELECT
   am.agentID          AS agentid,
   pa.agentDisplayName agentdisplayname,
   m.ModuleName        ModuleName
FROM
   AgentModule AS am
   JOIN primaryagent AS pa
      ON am.agentID = pa.AgentID
   JOIN Module AS m
      ON am.ModuleID = m.ModuleID
WHERE
   m. Active = 1
   AND pa.groupID = 75

Dataset is return as below

agentid  |  agentdisplayname  |  modulename
94       |  Agent1            |  Module 1
94       |  Agent1            |  Module 2
94       |  Agent1            |  Module 3
23       |  Agent1            |  Module 2
23       |  Agent1            |  Module 3

I am trying to use the PIVOT function to return a table that looks more like

agentid  |  agentdisplayname  |  Module 1  |  Module 2  |  Module 3  |.. .. .. 
94       |  Agent1            |  1         |  1         |  1
23       |  Agent2            |  0         |  1         |  1

There are a dynamic list of modules so I cannot hard code them in the query. I have tried PICOT but it seems to expect an aggregate function and not quite sure it is what I would need for this scenario.

  • 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-01T20:08:38+00:00Added an answer on June 1, 2026 at 8:08 pm

    You can add a extra column to you result and use min() on that column. The result will be 1 or null. Use isnull to get a 0 instead of null.

    select agentid,
           agentdisplayname,
           isnull([Module 1], 0) as [Module 1],
           isnull([Module 2], 0) as [Module 2],
           isnull([Module 3], 0) as [Module 3]
    from
      (
        select agentid, agentdisplayname, modulename, 1 as dummy
        from YourResultset
      ) as T
    pivot
      (min(dummy) for modulename in ([Module 1],[Module 2],[Module 3])) as P
    

    If you want to build this dynamically you need to first do a query that returns the modules you have in the result and then you need to use that to build the dynamic statement. It is probably best for you to store the result from your query in a temp table and then use that table when you build your dynamic query.

    SELECT
       am.agentID          AS agentid,
       pa.agentDisplayName agentdisplayname,
       m.ModuleName        ModuleName
    INTO #Tmp
    FROM
       AgentModule AS am
       JOIN primaryagent AS pa
          ON am.agentID = pa.AgentID
       JOIN Module AS m
          ON am.ModuleID = m.ModuleID
    WHERE
       m. Active = 1
       AND pa.groupID = 75
    

    Build and run the dynamic query using #Tmp.

    declare @FieldList1 nvarchar(max)
    declare @FieldList2 nvarchar(max)
    declare @SQL nvarchar(max)
    
    set @FieldList1 =
      (select ',isnull('+quotename(modulename)+', 0) as '+quotename(modulename)
       from #Tmp
       group by modulename
       order by modulename
       for xml path(''), type).value('.', 'nvarchar(max)')
    
    set @FieldList2 = stuff(
      (select ','+quotename(modulename)
       from #Tmp
       group by modulename
       order by modulename
       for xml path(''), type).value('.', 'nvarchar(max)') , 1, 1, '')
    
    set @SQL = 
      'select agentid, agentdisplayname'+@FieldList1+
      'from (select agentid, agentdisplayname, modulename, 1 as dummy 
             from YourTable) as T 
       pivot (min(dummy) for modulename in ('+@FieldList2+')) as P'
    
    exec sp_executesql @SQL
    
    drop table #Tmp
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a select query that retrieves all entries that are 3 weeks old.
I have the following query that retrieve the number of users per country; SELECT
I have a query that is currently using a correlated subquery to return the
I need to make an outer join query that retrieves all of the definitions
I have a method that will return a list of suggested orders. If the
I have a query that gets all the info I need for a messaging
I have a generic MySQL statement used for many tables that just retrieves all
I have a LINQ query that retrieves rows from a dataset and binds it
I have an existing recordset that retrieves all the information from a table in
I have a query that retrieves the name of each friend a user has

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.