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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:36:54+00:00 2026-06-14T02:36:54+00:00

I have a table : IADATA Id Studentid Mon Value Type 1 ABC1 1

  • 0

I have a table :

IADATA

    Id     Studentid     Mon   Value  Type
    1      ABC1           1      12     1
    1      ABC1           1      02     2
    1      ABC1           1      18     4
    1      ABC1           1      09     7
    1      ABC1           1      12     8
    1      ABC1           1      22     10
    1      ABC2           2      12     1
    1      ABC2           2      02     2
    1      ABC2           2      18     4
    1      ABC2           2      09     7
    1      ABC2           2      12     10
    1      ABC2           3      05     1
    1      ABC2           3      02     2
    1      ABC2           3      20     4
    1      ABC2           3      09     7
    1      ABC2           3      12     10

In the above table we have two Students ABC1 and ABC2 and their data.

Now the kind of result I want is as follows:

   Id      Studentid      Mon     Obtained   Benefits   Max    Type
    1        ABC1          1         12         02      18       I
    2        ABC1          1         09         12      22       II
    3        ABC2          2         12         02      18       I
    4        ABC2          2         09         00      12       II
    5        ABC2          3         05         02      20       I
    6        ABC2          3         09         00      12       II

Now in the result you can see that I am placing the values according to the studentid and mon and the types in a certain order. if the type is I it should have obtained, then benefits then max and same with II. What can be the select query for it?

The type in previous tables are linked. When it has the value 1,2,4 it should be like obtained, benefits and maximum for new type I and when 7,8,10, it should be like obtained,benefits and maximum for new type II. And when there is no column available, it should be zero.

  • 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-14T02:36:56+00:00Added an answer on June 14, 2026 at 2:36 am

    This should do it:

    SELECT  ID,
            StudentID,
            Mon,
            MAX(CASE WHEN Type LIKE 'Obtained%' THEN Value END) AS Obtained,
            MAX(CASE WHEN Type LIKE 'Benefit%' THEN Value END) AS Benefit,
            MAX(CASE WHEN Type LIKE 'Max%' THEN Value END) AS `Max`,
            CASE WHEN RIGHT(Type, 2) = 'II' THEN 'II' ELSE 'I' END AS Type
    FROM    T
    GROUP BY ID, StudentID, Mon, CASE WHEN RIGHT(Type, 2) = 'II' THEN 'II' ELSE 'I' END
    ORDER BY ID, StudentID, Mon, Type
    

    EXAMPLE ON SQL FIDDLE

    Although it would make more sense to store type separately. i.e. have one column for “obtained”, “max” etc and another column for “I”, “II”

    EDIT

    With your revised data structure this should work:

    SELECT  ID,
            StudentID,
            Mon,
            COALESCE(MAX(CASE WHEN Type IN (1, 7) THEN Value END), 0) AS Obtained,
            COALESCE(MAX(CASE WHEN Type IN (2, 8) THEN Value END), 0) AS Benefit,
            COALESCE(MAX(CASE WHEN Type IN (4, 10) THEN Value END), 0) AS `Max`,
            CASE WHEN Type IN (7, 8, 10) THEN 'II' WHEN Type IN (1, 2, 4) THEN 'I' END AS Type
    FROM    T
    WHERE   Type IN (1, 2, 4, 7, 8, 10)
    GROUP BY ID, StudentID, Mon, CASE WHEN Type IN (7, 8, 10) THEN 'II' WHEN Type IN (1, 2, 4) THEN 'I' END
    ORDER BY ID, StudentID, Mon, Type
    

    EXAMPLE ON SQL FIDDLE

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

Sidebar

Related Questions

I have table of data that is sorted as follows: Item | Sample |
I have table with column Percentage varchar(10) Data in that table is Pecentage 2/10
I have table with two columns (varchar from, varchar to). This table represents connections
I have two tables tabData and tabDataDetail. I want all idData(PK) from Parent-Table(tabData) that
I have table of user actions, each having a user associated, a type, and
I have table the following data structure in SQL Server: ID Date Allocation 1,
I have table like below : <table> <thead> <th>Product Type</th> <th>Quantity</th> <th>Unit</th> <th>Total</th> </thead>
I have table with Persian data and utf8_general_ci collection and with php program i
I have table with statistical data. I'm trying to count the records grouped by
I have table rows of data in html being filled from a CGI application.

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.