Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Need help with SQL select inner joins using count

Author  Topic 

mldardy
Starting Member

13 Posts

Posted - 2010-03-17 : 13:17:21
I have a SQL select statement with inner joins to get a total number of records for the statement. I have tried using count with group by's but it just counts one for each record instead of giving me the total number of records for the query. Here is the query:
# select  z.iIndividualId,z.vchFirstName,z.vchLastName,z.vchEmailAddress,  
# z.vchAddress1,z.vchCity,z.chRegionCode,dbo.cnfnFormatPostCode(z.vchPostCode)
# as PostCode,dbo.cnfnFormatUSPhone(z.vchPhoneNumber) as PhoneNumber,
# convert(nvarchar(10),cpd.dtDate1,101) as SessionStartDate from individual z with
# (nolock) inner join customerProduct cp with (nolock) on
# z.iIndividualId = cp.iOwnerId and z.tiRecordStatus = cp.tiRecordStatus
# and z.iSiteId = cp.iSiteId inner join customerProductDetail cpd with (nolock)
# on cp.iProductId = cpd.iProductId and cp.iSiteId = cpd.iSiteId
# and cp.tiRecordStatus = cpd.tiRecordStatus where z.tiRecordStatus = 1
# and z.iSiteId = 1 and cpd.dtDate1 is not null and convert(nvarchar(10),
# cpd.dtDate1,101) = '03/03/2010' order by cpd.dtDate1

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-17 : 13:20:58
for getting just the count use below

# select count(1)
# from individual z with
# (nolock) inner join customerProduct cp with (nolock) on
# z.iIndividualId = cp.iOwnerId and z.tiRecordStatus = cp.tiRecordStatus
# and z.iSiteId = cp.iSiteId inner join customerProductDetail cpd with (nolock)
# on cp.iProductId = cpd.iProductId and cp.iSiteId = cpd.iSiteId
# and cp.tiRecordStatus = cpd.tiRecordStatus where z.tiRecordStatus = 1
# and z.iSiteId = 1 and cpd.dtDate1 is not null and convert(nvarchar(10),
# cpd.dtDate1,101) = '03/03/2010' order by cpd.dtDate1


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

mldardy
Starting Member

13 Posts

Posted - 2010-03-17 : 13:37:00
Cool Thanks that definitely helped.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-17 : 13:58:18
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -