Click here to Skip to main content
16,020,381 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi Friends

It's urgent

My problem is...
I have two table
First table naem is P
===============================
id	PARENTID	ITEM                 
1	NULL	     EMPLOYE  PERSON         
2	1	     UNEMPLOYPERSON          
3	1	     MALEUNEMPLOY            
4       1 	     FEMALEUNEMPLOY          
5	NULL	     EMPLOYE  PERSON         
6	1	     UNEMPLOYPERSON          
7	1	     MALEUNEMPLOY            
8       1 	     FEMALEUNEMPLOY          


================================

==================================

Second table is PV
=====================================
id	PID	VALUE
1	1	20
2	2	45
3	3	5
4	4	12
=================================


where PID is P Table ID

And the last table is TP
======================================
ID    PID           NAME           PrentID
1     1             COUNTRY         null
2     1             STATE            1
3     1             CITY             2
4     2             COUNTRY          null
5     2             STATE            1
6     2             CITY             2
==================================

where pid is P Table ID

How can I display this like ...

IN CITY
TOTALPERSON     ENEMPLOYPERSON       EMPLOYPERSON    MALEUNEMPLOY     
82                45                      20                 5        


IN STATE
TOTALPERSON     ENEMPLOYPERSON       EMPLOYPERSON    MALEUNEMPLOY     
82                45                      20                 5    


This is not static it is dynamic depending on area selected like ...COUNTRY,STATE,CITY
[Edit - CHill60 SHOUTING removed]
Posted
Updated 23-May-13 2:54am
v6
Comments
[no name] 20-May-13 7:38am    
No it's not urgent at all. Are you screaming because you think it's urgent?
rahul 92 23-May-13 8:38am    
HII.....
I Have Update My Qestion Plzzzzz Give Me Some Solution...I'm Now Online
rahul 92 23-May-13 8:52am    
It's Urgent
Aarti Meswania 20-May-13 7:39am    
where is relation of city & state with employee?
rahul 92 23-May-13 8:37am    
HII.....
I Have Update My Qestion
Plzzzzz Give Me Some Solution...I'm Now Online

SQL
with a as
(
select 'Total Employees' as item, sum(pv.value) as [value]
from p
inner join pv on p.id=pv.pid

union all
 
select p.item,pv.value
from p
inner join pv on p.id=pv.pid
)
SELECT [Total Employees],[EMPLOYE  PERSON],[UNEMPLOYPERSON],[MALEUNEMPLOY]
FROM
a
PIVOT
(Sum([value]) FOR [item]
    IN ( [Total Employees],[EMPLOYE  PERSON],[UNEMPLOYPERSON],[MALEUNEMPLOY])
) AS b

Happy Coding!
:)
 
Share this answer
 
v3
Try dynamic pivot:

SQL
DECLARE @cols NVARCHAR(300)
DECLARE @dt NVARCHAR(2000)
DECLARE @pt NVARCHAR(MAX)

SET @cols = STUFF((SELECT DISTINCT '],[' + [ITEM]
			FROM P
			ORDER BY '],[' + [ITEM]
		FOR XML PATH('')),1,2,'') + ']'

SET @dt = N'SELECT TP.[NAME], PV.[VALUE], P.ITEM ' + 
	'FROM P INNER JOIN TP ON P.ID = TP.PID ' +
		'INNER JOIN PV ON P.ID = PV.PID '

SET @pt = 'SELECT [NAME], ' + @cols + ' ' +
         'FROM (' + @dt + ') AS DT ' +
         'PIVOT(SUM([VALUE]) FOR [ITEM] IN(' + @cols + ')) AS PT'

EXEC (@pt)
 
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