Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Is there a way to increment a variable within a subquery? If I try something like this, I get an error.
DECLARE @Count intSELECT a.fieldFROM Table1 aWHERE EXISTS (SELECT * FROM Table2 b WHERE b.field2 = 36 AND EXISTS (SELECT @Count = @Count + 1 FROM Table3 c WHERE c.field1 = b.field1 AND c.field = a.field ) )
Thanks
SwePeso
Patron Saint of Lost Yaks
30421 Posts
Posted - 2007-09-18 : 09:28:57
This?
SELECT COUNT(DISTINCT c.Field1)FROM Table1 AS aLEFT JOIN Table2 AS b ON b.Field = a.Field AND b.Field2 = 36LEFT JOIN Table3 AS c ON c.Field1 = b.Field1
Or this?
SELECT COUNT(DISTINCT c.Field1)FROM Table1 AS aINNER JOIN Table2 AS b ON b.Field = a.FieldINNER JOIN Table3 AS c ON c.Field1 = b.Field1WHERE b.Field2 = 36