If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Need help in writting query

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-21-10, 07:38
prakashac prakashac is offline
Registered User
 
Join Date: Feb 2010
Posts: 1
Need help in writting query

Hi,
Can any one please tell me, how to write query for the below example.

Standard TotalStudents Name
2 50 Raj
2 40 Sathish
3 35 Ganesh
4 50 Raj
5 40 Raj

The resulting query should display the total no of students and in another column the number of students whose name matches "Raj".

Like:

Toal Students Name
215 3

I can write query like this

select SUM(TotalStudents) as 'Total Students', (select SUM(TotalStudents) from student where name like 'Raj') as 'Name' from student.
Is there any other way to write it.

And also if i want to get the remaining students whose name don't matches with 'Raj' as another column.

Please reply soon..

Thanks in advance
Regards,
Prakash
Reply With Quote
  #2 (permalink)  
Old 02-21-10, 08:31
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,084
Code:
SELECT SUM(TotalStudents) AS 'Total Students'
     , COUNT(CASE WHEN name = 'Raj'
                  THEN 'Todd'
                  ELSE NULL END) AS 'Total Rajs'
  FROM student
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 07-24-10, 20:09
cavefish cavefish is offline
Registered User
 
Join Date: Jul 2010
Posts: 1
Code:
SELECT  X.[Total Students]
           ,SUM(X.[TotalUser]) AS [Total Rajs]
FROM (SELECT SUM(TotalStudents) AS [Total Students]
           ,[TotalUser] = CASE WHEN [Name] = 'Raj' THEN 1 ELSE 0 END
          FROM student) X
Reply With Quote
  #4 (permalink)  
Old 07-24-10, 22:12
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,084
dear cavefish, if you are going to resurrect a thread that has been sleeping quietly for five months, then you must at least make sure your post is reasonably free of errors

several expressions, e.g. X.[Total Students], are wrong because ANSI SQL (which is the forum you posted in) does not allow square brackets

[TotalUser] = is wrong because ANSI SQL does not allow assigning aliass in that manner

the CASE expression is wrong because it's missing the closing END keyword
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On