Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have sql table like:
VB
Date            Pagename
6/6/2015         home.aspx
6/6/2015         home.aspx
6/6/2015         home.aspx
6/6/2015         home.aspx
6/6/2015         home.aspx
6/6/2015         homebn.aspx
6/6/2015         homebn.aspx
6/6/2015         homebn.aspx
6/6/2015         homebn.aspx
7/6/2015         welcome.aspx
7/6/2015         welcome.aspx
7/6/2015         welcome.aspx
7/6/2015         welcome.aspx
7/6/2015         welcomebn.aspx



now i want to fetch data from this table and my output should be:
VB
Date		Pagename            Bn     En    
6/6/2015        home.aspx	    4      5
7/6/2015     welcomebn.aspx     1      4



how can i count samepage when it found the same page name
Posted
Comments
Mehdi Gholam 6-Jun-15 2:22am    
and what have you done so far?

Counting is simple, just use the COUNT function in conjunction with GROUP BY:
http://www.w3schools.com/sql/sql_func_count.asp[^]
http://www.w3schools.com/sql/sql_groupby.asp[^]

But first, you will have to work out exactly what you want to achieve and what the rules are to get to that point: it is not at all obvious why SQL should return "home.aspx" instead of "homebn.aspx" but also "welcomebn.aspx" instead of "welcome.aspx" and that is likely to have a big effect on your eventual query.
 
Share this answer
 
SQL
select date, Replace(Pagename,'bn.','.'),
count(case when charindex('bn.',Pagename,0) > 0 then 1 else null end),
count(case when charindex('bn.',Pagename,0) = 0 then 1 else null end)
FROM Pages
group by date, Replace(Pagename,'bn.','.')


This will only give you the base name without 'bn' in

Output

Date            Pagename        bn    en              
2015-06-06      home.aspx       4     5
2015-06-07      welcome.aspx    1     4


Regards
Kevin
 
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