Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
this is sample of my database in this way data is stored in ms-sqlserver
--------------------
ID | BloodGroup
--------------------
1 | A
2 | B
3 | A
4 | AB
5 | B
6 | O
7 | A
8 | AB

but my representation of data is totally different how i can perfom this kind of operation.
--------
A|B|AB|O
--------
3|2|2|1

Mr.Peter Leow has help me in get this data but now i want some modification in this data

XML
<pre lang="xml">&lt;pre lang=&quot;sql&quot;&gt;SELECT  A,B,AB,O
FROM
(SELECT bloodgroup  FROM table1) AS src
PIVOT
(
  COUNT(bloodgroup) FOR bloodgroup IN (A,B,AB,O)
)AS piv&lt;/pre&gt;</pre>




this code work good but how i can do this
-----------------------------------------------------------------------
A|B|AB|O|Total
-----------------------------------------------------------------------
3|2|2|1|8
Posted

1 solution

SELECT  A,B,AB,O, total
FROM
(SELECT bloodgroup, (select count(*) from table1) as total FROM table1) AS src
PIVOT
(
  COUNT(bloodgroup) FOR bloodgroup IN (A,B,AB,O)
)AS piv
 
Share this answer
 
Comments
Tarun Jaiswal 2-Apr-14 1:04am    
thank you sir
Peter Leow 2-Apr-14 1:08am    
You are welcome. Consider voting for both solutions?
Tarun Jaiswal 2-Apr-14 1:53am    
yes done voting for both the post
Peter Leow 2-Apr-14 1:55am    
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900