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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:33:34+00:00 2026-06-12T18:33:34+00:00

From Database ID Name Department position ================================ 1 A MK IT 2 B MK

  • 0

From Database

ID   Name Department position
================================
1    A    MK          IT
2    B    MK          RD
3    C    MK          EN
4    D    BD          IT
5    E    BD          RD
6    F    BD          EN
7    G    BD          IT
8    H    OB          IT  
9    I    MK          EN

how to write the code to display in table as following this

display something like this in the table

position/departmet    MK  BD   OB
===================================
IT                    1    2   1    
RD                    1    1   0   
EN                    2    1   0

i just confuse.

  • 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-12T18:33:35+00:00Added an answer on June 12, 2026 at 6:33 pm

    Try it by using CASE,

    SELECT position,
            SUM(CASE WHEN Department = 'MK' THEN 1 ELSE 0 END) MK,
            SUM(CASE WHEN Department = 'BD' THEN 1 ELSE 0 END) BD,
            SUM(CASE WHEN Department = 'OB' THEN 1 ELSE 0 END) OB            
    FROM tableName
    GROUP BY position
    

    SQLFiddle Demo

    Or use Prepared Statement if you have unknown number of columns

    SET @sql = NULL;
    SELECT
      GROUP_CONCAT(DISTINCT
        CONCAT(
          'SUM(case when Department = ''',
          Department,
          ''' then 1 ELSE 0 end) AS ',
          Department
        )
      ) INTO @sql
    FROM tableName;
    
    SET @sql = CONCAT('SELECT position, ', @sql, ' 
                       FROM tableName 
                       GROUP BY position');
    
    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
    

    SQLfiddle Demo

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

Sidebar

Related Questions

SELECT name FROM sys.databases -- this can list all database name in the server
(LocalVariable)ABC.string(Name) = (IDataReader)dataReader.GetString(0); This name value is coming from database. What happening here is
I have some values return from database as a result set like following **resource_name
I have some values return from database as a result set like following **resource_name
I have the following database design: Employees Table: EmployeeID, Name, OrgCode Departments Table: OrgCode,
I have the following database design: Employees Table: EmployeeID, Name, OrgCode Departments Table: OrgCode,
I am trying to set the database name as the request input parameter from
I have requests in a database from which I need to extract the name
I want to echo a full name from my MySQL database in my header.
Trying to create Database as follows: USE Master GO IF NOT EXISTS(SELECT [Name] FROM

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.