Click here to Skip to main content
15,886,032 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi ,I have resultset as

SQL
Department     |achead              | location
Admin          |Telecommunication   | usa                                                                    
Admin          |Telecommunication   | usa  
Admin          |Computers           | uae
Admin          |Computers           | uae
Admin1         |Micro               | usa
Admin1         |Micro               | usa
Admin1         |Computers           | uae
Admin1         |Computers           | uae


I want to change the resultset with following resultset
SQL
Department     |achead              | Total_Count_Location
Admin          |Telecommunication   | 2
Admin          |Computers           | 2
                              Total | 4
Admin1         |Micro               | 2
Admin1         |Computers           | 2
                              Total | 4



I was able to get the desire output using while loop ,but i dont want to use while loop and want desire output resultset .please help
Posted
Updated 31-Jan-14 20:00pm
v2

Try select Department, achead, count(achead) group by department, achead.

To get the total count per department, simply group by Department.
 
Share this answer
 
SQL
------------use temperory table
-----------if it is executing in aloop u should delete temp table
    IF OBJECT_ID('tempdb..#TMP2') IS NOT NULL
        BEGIN
            drop table #TMP2
        END

SELECT      CASE GROUPING([departmant])
                  WHEN 1 THEN 'Total'
                   ELSE [departmant] END AS 'departmant',
            CASE GROUPING([achead])
                  WHEN 1 THEN 'Total'
                  ELSE [achead] END AS 'achead',
            COUNT([location])  AS 'Total_Count_Location' INTO #TMP2
FROM        temptest
GROUP BY    [departmant], [achead] WITH ROLLUP

SELECT  CASE achead WHEN 'Total' then '' else departmant end as departmant,
        achead,
        Total_Count_Location
FROM    #TMP2 WHERE departmant!='TOTAL'




Try The Above code. If u have a simpler solution Plz share Gud Luck
 
Share this answer
 

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