SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Please suggest for count with inner query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

kamii47
Constraint Violating Yak Guru

352 Posts

Posted - 10/30/2013 :  07:54:23  Show Profile  Reply with Quote
Select dbo.[User].FirstName
, dbo.[User].LastName
, dbo.[User].Email
, dbo.FormInstance.FormInstanceId
, dbo.FormInstance.IsLocked
, dbo.FormInstance.InsertedDate
, dbo.FormInstance.UpdatedDate
, dbo.Form.FormName
, ts.SectionOrder
, (Select Count(*)
From dbo.FormSection tsstotal
Where tsstotal.FkFormId = dbo.Form.FormId) TotalSections
From dbo.[User]
Inner Join dbo.FormInstance
On dbo.[User].UserId = dbo.FormInstance.FkUserId
Inner Join dbo.InstanceLastVistiedSectionInfo
On dbo.FormInstance.FormInstanceId = dbo.InstanceLastVistiedSectionInfo.FkFormInstanceId
Inner Join dbo.Form
On dbo.FormInstance.FkFormId = dbo.Form.FormId
Inner Join dbo.FormSection ts
On dbo.InstanceLastVistiedSectionInfo.FkFormSectionId = ts.FormSectionId
And dbo.Form.FormId = ts.FkFormId
Where dbo.FormInstance.FormInstanceId = @FormInstanceId


What I wants is remove
(Select Count(*)
From dbo.FormSection tsstotal
Where tsstotal.FkFormId = dbo.Form.FormId)
With some sort of join but the dbo.FormSection is already there in for some other prespective

Kamran Shahid
Principle Engineer Development
(MCSD.Net,MCPD.net)

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 10/30/2013 :  08:14:32  Show Profile  Reply with Quote
you dont need any extra join. just tweak derived table as below and see

Select dbo.[User].FirstName
, dbo.[User].LastName
, dbo.[User].Email
, dbo.FormInstance.FormInstanceId
, dbo.FormInstance.IsLocked
, dbo.FormInstance.InsertedDate
, dbo.FormInstance.UpdatedDate
, dbo.Form.FormName
, ts.SectionOrder
, ts.Cnt AS TotalSections
From dbo.[User]
Inner Join dbo.FormInstance
On dbo.[User].UserId = dbo.FormInstance.FkUserId
Inner Join dbo.InstanceLastVistiedSectionInfo
On dbo.FormInstance.FormInstanceId = dbo.InstanceLastVistiedSectionInfo.FkFormInstanceId
Inner Join dbo.Form
On dbo.FormInstance.FkFormId = dbo.Form.FormId
Inner Join (SELECT *, COUNT(1) OVER (PARTITION BY FkFormId) AS Cnt FROM  dbo.FormSection) ts
On dbo.InstanceLastVistiedSectionInfo.FkFormSectionId = ts.FormSectionId
And dbo.Form.FormId = ts.FkFormId
Where dbo.FormInstance.FormInstanceId = @FormInstanceId


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

kamii47
Constraint Violating Yak Guru

352 Posts

Posted - 10/30/2013 :  08:31:08  Show Profile  Reply with Quote
thanks visakh16

Kamran Shahid
Principle Engineer Development
(MCSD.Net,MCPD.net)

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 10/30/2013 :  08:44:13  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.14 seconds. Powered By: Snitz Forums 2000