Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to display category list on my sidebar with count of its total posts.
for ex: if there are 40 posts under category sports it should display like --> Sports(40)
pls help guys i'm stuck with this
Posted

Hi,

Check the below Code....
SQL
DECLARE @Cat TABLE(CatID INT, CatDesc VARCHAR(200))
DECLARE @TranDtls TABLE (CatID INT, PostCount INT)

INSERT INTO @Cat(CatID, CatDesc)
SELECT 1, 'Sports'
UNION ALL SELECT 2,'Accounts'
UNION ALL SELECT 3,'IT'
UNION ALL SELECT 4,'General'

INSERT INTO @TranDtls (CatID, PostCount )
SELECT 1,10
UNION ALL SELECT 1,1
UNION ALL SELECT 1,3
UNION ALL SELECT 2,1
UNION ALL SELECT 2,5
UNION ALL SELECT 4,16


SELECT C.CatDesc+'('+CAST(ISNULL(T.PostCount ,0) AS VARCHAR(20))+')' AS Category
FROM @Cat C
LEFT OUTER JOIN (SELECT CatID, SUM(PostCount) 'PostCount' FROM @TranDtls GROUP BY CatID) T ON T.CatID=C.CatID 

-- Note : In case you have single entry for each category post, Use COUNT Function.


Regards,
GVPrabu
 
Share this answer
 
SQL
select count(sportID)as SportCount from sportTable


give datasourse to label...:)


C#
label1.Text=Datatable1.Rows[0]["SportCount "].toString();
 
Share this answer
 
Comments
Adarsh chauhan 9-Jul-13 7:28am    
+5 Nirav.. nice one
Nirav Prabtani 9-Jul-13 7:29am    
thanks Adarsh.... :)
gvprabu 9-Jul-13 8:47am    
How "select count(sportID)as SportCount from sportTable" this Query will work without GROUP BY?
gvprabu 9-Jul-13 8:31am    
Is this Solution is help full to you?
Nirav Prabtani 9-Jul-13 8:33am    
is it wrong???

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