SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseSQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old April 12th, 2009, 06:25 PM
j1a4l0 j1a4l0 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Apr 2009
Posts: 1 j1a4l0 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 11 m 55 sec
Reputation Power: 0
Transact-SQL - Help me - Count function

Please help me to sort out my problem!!!

I have 2 tables:

1. News (NewsID, UserID, Content)
2. User (UserID, Name)


I want to return a table such as:

| UserID | Name | Number of news posted by this user |
------------------------------------------------------
| | | |


I believe I should use a T-SQL such as:

CREATE PROCEDURE [dbo].[User_Get_All]

AS

SELECT
User.UserID,
User.Name,
COUNT ?????? "Number of news posted by this user"
FROM
User INNER JOIN News ON News.UserID = User.UserID


Thanks in advance

Reply With Quote
  #2  
Old April 15th, 2009, 04:59 AM
sync_or_swim's Avatar
sync_or_swim sync_or_swim is offline
Moderator
Click here for more information.
 
Join Date: Mar 2006
Location: South Wales
Posts: 3,461 sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 1 Day 16 h 50 m 26 sec
Reputation Power: 1806
Hi, and welcome to the forums. I'm not entirely sure what you are trying to achieve, do you just want the name and userid of the usedr along with a count of the number of of news records posted by this user, or do you want to return the user's name and id along with all of the ews items on the same line? If you wanted to do the latter it would be more difficult, but to simply return a count of the news items you should be able to do something like this:
Code:
CREATE PROCEDURE [dbo].[User_Get_All]

AS

SELECT
u.UserID,
u.Name,
COUNT(n.NewsID)
FROM
User u INNER JOIN n ON n.UserID = u.UserID
GROUP BY u.UserID, u.Name

Reply With Quote
  #3  
Old April 21st, 2009, 06:07 AM
bklr bklr is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Feb 2009
Posts: 28 bklr User rank is Sergeant (500 - 2000 Reputation Level)bklr User rank is Sergeant (500 - 2000 Reputation Level)bklr User rank is Sergeant (500 - 2000 Reputation Level)bklr User rank is Sergeant (500 - 2000 Reputation Level)bklr User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 h 53 m 3 sec
Reputation Power: 0
Quote:
Originally Posted by j1a4l0
Please help me to sort out my problem!!!

I have 2 tables:

1. News (NewsID, UserID, Content)
2. User (UserID, Name)


I want to return a table such as:

| UserID | Name | Number of news posted by this user |
------------------------------------------------------
| | | |


I believe I should use a T-SQL such as:

CREATE PROCEDURE [dbo].[User_Get_All]

AS

SELECT
User.UserID,
User.Name,
COUNT ?????? "Number of news posted by this user"
FROM
User INNER JOIN News ON News.UserID = User.UserID


Thanks in advance


CREATE PROCEDURE [dbo].[User_Get_All]

AS
set nocount on
begin
SELECT
u.UserID,
u.Name,
(select COUNT(NewsID) from news where userid = u.userid)as cnt
FROM
User u

--or
-- when ever you use aggregate function and values then u ---should use group by clause or partion by clause (2005)
--SELECT u.UserID, u.Name,COUNT(n.NewsID)
--FROM
--User u INNER JOIN news n ON n.UserID = u.UserID
--GROUP BY u.UserID, u.Name

end
set nocount off

Reply With Quote
  #4  
Old November 3rd, 2009, 01:40 AM
sakthi.tnj sakthi.tnj is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2009
Posts: 23 sakthi.tnj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 48 m 35 sec
Reputation Power: 0
Re: Helpme -Count function

Hi,

I hope the below SQL query will be very Useful for you

Code:
Select a.Userid, b.[name] ,count(b.userid)as [Number of news posted by User] from news a
inner join [user]b on a.userid=b.userid
group by a.Userid, b.[name]


Thanks & Regards
Sakthimeenakshi.S

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseSQL Development > Transact-SQL - Help me - Count function


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump





 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek