I am having an issue with the below mentioned sql query. Basically I am trying to list all the system users that have captured data in the current week and previous week for each day.
Problem with my query below I get the "Only one expression can be specified in the select list when the subquery is not introduced with EXISTS. ". I have changed a part of this query to uses EXISTS but still get the same error on the last line of the subquery.
DECLARE @Dates date;
SET @Dates = 20091012 ;
DECLARE @FirstDayOfWeek date;
DECLARE @lastDayOfWeek date;
SET @FirstDayOfWeek = dbo.[fn_GetFirstDayOfWeek](@Dates);
SET @lastDayOfWeek = DATEADD(day, 6,@FirstDayOfWeek);
SET NOCOUNT ON
SELECT DISTINCT q.CapturerId, u.Firstname, u.Lastname INTO #Users
from Wop_Questionnaires q JOIN
Frwk_Users u
ON
q.CapturerId = u.Id JOIN
Core_Areas a ON
u.X_Wop_ProvinceId = a.Id
WHERE u.X_Wop_ProvinceId = 5
SELECT (SELECT #Users.Firstname+ +#Users.Lastname, COUNT(q.CapturerId)
from Wop_Questionnaires q JOIN #Users on q.CapturerId = #Users.CapturerId
where EXISTS(SELECT #Users.CapturerID from #Users WHERE q.CapturerId = #Users.CapturerId)
AND q.CaptureDate >=DATEADD(DAY,DATEDIFF(DAY, 19000101 ,@FirstDayOfWeek), 19000101 )
And q.CaptureDate < DATEADD(DAY,DATEDIFF(DAY, 19000101 ,@FirstDayOfWeek)+1, 19000101 )
GROUP BY q.CapturerId, #Users.Firstname,#Users.Lastname) Sun,
(SELECT #Users.Firstname+ +#Users.Lastname, COUNT(q.CapturerId)
from Wop_Questionnaires q JOIN #Users on q.CapturerId = #Users.CapturerId
where EXISTS (SELECT #Users.CapturerID from #Users WHERE q.CapturerId = #Users.CapturerId)
AND q.CaptureDate >=DATEADD(DAY,DATEDIFF(DAY, 19000101 ,DATEADD(DAY,1,@FirstDayOfWeek)), 19000101 )
AND q.CaptureDate < DATEADD(DAY,DATEDIFF(DAY, 19000101 ,DATEADD(DAY,1,@FirstDayOfWeek))+1, 19000101 )
GROUP BY q.CapturerId, #Users.Firstname,#Users.Lastname) Mon,
(SELECT #Users.Firstname+ +#Users.Lastname, COUNT(q.CapturerId)
from Wop_Questionnaires q JOIN #Users on q.CapturerId = #Users.CapturerId
where EXISTS(SELECT #Users.CapturerID from #Users WHERE q.CapturerId = #Users.CapturerId)
AND q.CaptureDate >=DATEADD(DAY,DATEDIFF(DAY, 19000101 ,DATEADD(DAY,2,@FirstDayOfWeek)), 19000101 )
AND q.CaptureDate < DATEADD(DAY,DATEDIFF(DAY, 19000101 ,DATEADD(DAY,2,@FirstDayOfWeek))+1, 19000101 )
GROUP BY q.CapturerId, #Users.Firstname,#Users.Lastname) Tue;
--;--
DROP TABLE #Users;
SET NOCOUNT OFF
Thanks Tebogo