Click here to Skip to main content
15,913,941 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
E.g.
AppNo   wc           status        
100     Delhi        to HR
101     Delhi        to wc
104     dehradun     from wc
107     kolkata      from HR

I want to count the no. of applications that came from Delhi,Dehradun and so on other places ,how many applications were sent to HR,to Wc and similarly received also.

Kindly Help me.please...
Thank You
Posted
Updated 17-Jul-13 21:45pm
v3
Comments
Thanks7872 18-Jul-13 3:48am    
As per i understand your question,something is going from delhi to HR with app no 100.
something from delhi to wc with app no 101,and so on?
Shivangi Rawat 18-Jul-13 4:04am    
yes,now i have to count the number of all such applications denoted by AppNo that are going to different places(say Delhi,Dehradun....)and to whom(HR,Wc) and received from (HR,Dehradun...) so that i can prepare a report based on that.
so how should i do it?
Maciej Los 18-Jul-13 4:11am    
See my answer ;)

Try this:
SQL
SELECT [status], wc AS [Place], COUNT(AppNo) AS CountOfApps
FROM Table1
GROUP BY [status], wc
ORDER BY [status], wc


But if you want to count distinct apps, use:
SQL
COUNT(DISTINCT AppNo) AS CountOfDistinctApps


Another way is to use PIVOT[^]

SQL
CREATE TABLE #tmp (AppNo INT, wc VARCHAR(30), [status] VARCHAR(30))



INSERT INTO #tmp (AppNo, wc, [status])
SELECT 100, 'Delhi', 'to HR'
UNION ALL SELECT 101, 'Delhi', 'to wc'
UNION ALL SELECT 104, 'dehradun', 'from wc'
UNION ALL SELECT 107, 'kolkata', 'from HR'

DECLARE @cols NVARCHAR(200)
 
SET @cols = STUFF((SELECT DISTINCT '],[' + [status]
					FROM #tmp
					ORDER BY '],[' + [status]
			FOR XML PATH('')),1,2,'') + ']'


DECLARE @dt VARCHAR(2000)
DECLARE @pt VARCHAR(MAX)

--first pivot
SET @dt = N'SELECT AppNo, wc, [status]
			FROM #tmp'

SET @pt = N'SELECT AppNo, wc,' + @cols +
		'FROM (' + @dt + ') AS DT
		PIVOT (COUNT([status]) FOR [status] IN (' + @cols + ')) AS PT'
EXEC(@pt)


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


--second pivot

SET @dt = N'SELECT AppNo, wc, [status]
			FROM #tmp'

SET @pt = N'SELECT AppNo, [status],' + @cols +
		'FROM (' + @dt + ') AS DT
		PIVOT (COUNT([wc]) FOR [wc] IN (' + @cols + ')) AS PT'

EXEC(@pt)
DROP TABLE #tmp


first pivot result:
Appno   wc             from...         to...
104	dehradun	0	1	0	0
100	Delhi	0	0	1	0
101	Delhi	0	0	0	1
107	kolkata	1	0	0	0


second pivot result:
AppNo   wc      Dehra.  Delphi  Kolk.
107	from HR	0	0	1
104	from wc	1	0	0
100	to HR	0	1	0
101	to wc	0	1	0
 
Share this answer
 
v4
Comments
Shivangi Rawat 18-Jul-13 5:06am    
that will bring together all the data on the basis of rows i specify..

I have to "count" how many Applications came from Delhi,Dehradun,...how many were send to HR,Finance...,How many were received from HR,Finance.
I have to display it in following format:-


WC Received From Sent To
HR Finance HR Finance
Delhi 2 4 5 10
Dehradun 4 0 10 0
Maciej Los 18-Jul-13 5:48am    
And where is the problem? Simply remove Appno from select list (first pivot) and you'll get your result ;) That's all!
Shivangi Rawat 18-Jul-13 11:07am    
to use pivot do i have to import any namespace.Sorry to ask this but i have not used pivot before.
Maciej Los 18-Jul-13 12:16pm    
No, you don't ;)
Shivangi Rawat 18-Jul-13 16:08pm    
void Page_Load(Object a, EventArgs b)
{
// SqlConnection s = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\grdproject.mdf;
//Integrated Security=True;Connect Timeout=30;User Instance=True");

// int f=0;
// s.Open();

DECLARE @dt VARCHAR(2000)
DECLARE @pt VARCHAR(MAX)
string x,x1;
SET @dt= "SELECT AppNo, WorkCenter, [Sent_Received]from dataentry";


SET @pt = "SELECT AppNo,WorCenter,' + @dt + 'FROM (' + @dt + ') AS DT PIVOT (COUNT([status]) FOR [status] IN (' + @cols + ')) AS PT";


EXEC(@pt);


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

}


it is giving an error.please check it..
From where you get this data is this in DataSet.
If this data coming from Dataset then you can use Dataview.

DataView dv = new DataView(YourDatasetTablename);
dv.RowFilter = YourFilterValue;
int countval=dv.Count;
 
Share this answer
 
SQL
try this
select  count (AppNo) where wc= Delhi
 
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