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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:59:58+00:00 2026-06-13T17:59:58+00:00

I have Following Tables in SQL Server Database: (1) StudentMaster (StudentId, StudentName) (2) SubjectMaster

  • 0

I have Following Tables in SQL Server Database:

(1) StudentMaster (StudentId, StudentName)

(2) SubjectMaster (SubjectId, SubjectName)

(3) AttendanceMaster (AttendanceId,StudentId,SubjectId,Attendance,Date)


Data in AttendanceMaster can be in Following format :

AttendanceMaster :
           AttendanceId   StudentId   SubjectId    Attendance   Date
              3001          33          1            P           1/1/2011 
              3001          57          2            P1          1/2/2011 
              3001          33          1            P           1/3/2011 
              3001          57          2            P2          1/4/2011 
              3001          33          1            P1          1/5/2011  

I want to get SubjectWise Individual Attendance Details in Following Format:

StudentName  SubjectName   Total(P)  Total(P1)  Total(P2)
 Ghanshyam     Maths         90        10          5
 John          Maths         85        15          5
 Ghanshyam     Science       70        20          15
 John          Science       80        30          5  

i tried following Query :

select StudentName, SubjectName,
(select count(*) from AttendanceMaster innerAM where innerAM.StudentId = StdM.StudentId and innerAM.SubjectId=SubM.SubjectId and innerAM.Attendance = 'P') as Total(P),
(select count(*) from AttendanceMaster innerAM where innerAM.StudentId = StdM.StudentId and innerAM.SubjectId=SubM.SubjectId and innerAM.Attendance = 'P1') as Total(P1),
(select count(*) from AttendanceMaster innerAM where innerAM.StudentId = StdM.StudentId and innerAM.SubjectId=SubM.SubjectId and innerAM.Attendance = 'P1') as Total(P2)
from AttendanceMaster AM inner join StudentMaster StdM on AM.StudentId = StdM.StudentId
inner join SubjectMaster SubM on AM.SubjectId = SubM.SubjectId

I got result but it gets too much time to execute..( about 5 to 6 minut )

so what can i do to decrease execution time...

and also is it right way to write a query to get Total(P),Toal(P1),Total(P2) ??
please specify other SQL Syntax

Thanks

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

    Try this. If your query is still running slow, you need to check if there indexes on your table. If you table are large and there’re no indexes it still can be slow.

    select
        StM.StudentName,
        SbjM.SubjectName,
        count(case when AtM.Attendance = 'P' then 1 else null end) as Total(P),
        count(case when AtM.Attendance = 'P1' then 1 else null end) as Total(P1),
        count(case when AtM.Attendance = 'P2' then 1 else null end) as Total(P2)
    from StudentMaster  as StM
        inner join AttendanceMaster as AtM on AtM.StudentId = StM.StudentId
        inner join SubjectMaster as SbjM on SbjM.SubjectId = AtM.SubjectId
    group by
        StM.StudentName,
        SbjM.SubjectName
    

    If columns Attendance has no values besides P, P1 and P2, to count total you need just add count(*) as Total. If there are other values besides P, P1 and P2, there two ways. First – you can add
    AtM.Attendance in (‘P’, ‘P1’, ‘P2’) in where clause:

    select
        StM.StudentName,
        SbjM.SubjectName,
        count(case when AtM.Attendance = 'P' then 1 else null end) as Total(P),
        count(case when AtM.Attendance = 'P1' then 1 else null end) as Total(P1),
        count(case when AtM.Attendance = 'P2' then 1 else null end) as Total(P2),
        count(*) as Total
    from StudentMaster  as StM
        inner join AttendanceMaster as AtM on AtM.StudentId = StM.StudentId
        inner join SubjectMaster as SbjM on SbjM.SubjectId = AtM.SubjectId
    where AtM.Attendance in ('P', 'P1', 'P2')
    group by
        StM.StudentName,
        SbjM.SubjectName
    

    Or you can write it like this

    select
        StM.StudentName,
        SbjM.SubjectName,
        count(case when AtM.Attendance = 'P' then 1 else null end) as Total(P),
        count(case when AtM.Attendance = 'P1' then 1 else null end) as Total(P1),
        count(case when AtM.Attendance = 'P2' then 1 else null end) as Total(P2),
        count(case when AtM.Attendance in ('P', 'P1', 'P2') then 1 else null) as Total
    from StudentMaster  as StM
        inner join AttendanceMaster as AtM on AtM.StudentId = StM.StudentId
        inner join SubjectMaster as SbjM on SbjM.SubjectId = AtM.SubjectId
    group by
        StM.StudentName,
        SbjM.SubjectName
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following XML generated from various tables in my SQL SERVER database
I have the following 2 tables defined in a SQL Server database: CREATE TABLE
I have a following scenario in my SQL Server 2005 database. zipcodes table has
I have the following tables in SQL Server 2005 ReceiptPoint: ID (PK), Name GasIndexLocation:
i have the following tables in sql server: photoalbumsTable: album_ID album_caption albumtagmap id album_id
I have two tables in a SQL Server 2008 database in my company. The
In a SQL Server database, I have two tables, s and t. A further
In an SQL Server 2008 database, I have tables such as: CREATE TABLE t_DeviceType
I have the following situation (SQL Server Express): 2 tables connected via a pk-fk
I have a SQL Server 2000 database with 7 tables. I want to copy

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.