April 27, 2009 at 12:54 am
I Have a Table like
ScheduleuserId UseridStatus
1 1 0
1 2 1
1 3 2
1 4 0
1 5 1
1 6 0
1 7 0
1 8 1
1 9 0
I need output as
ScheduleUserId Attempted NotAttempted Completed
1 3 5 1
NotAttempted means status is 0
Attempted means status is 1
Completed means status is 2
But I Need the Result without using SubQuery Or Using three joins...
April 27, 2009 at 3:47 am
Why not the subquery?
Is it a Quiz...:w00t:
April 27, 2009 at 5:10 am
If u use the Sub query its killing the Performance.. So..;-)
April 27, 2009 at 5:21 am
[Code]
DECLARE @YourTable TABLE
(ScheduleuserId INT,Userid INT, Status INT)
INSERT INTO @YourTable
SELECT 1, 1, 0 UNION ALL
SELECT 1, 2, 1 UNION ALL
SELECT 1, 3, 2 UNION ALL
SELECT 1, 4, 0 UNION ALL
SELECT 1, 5, 1 UNION ALL
SELECT 1, 6, 0 UNION ALL
SELECT 1, 7, 0 UNION ALL
SELECT 1, 8, 1 UNION ALL
SELECT 1, 9, 0
SELECT * FROM @YourTable
SELECT
ScheduleuserId,
SUM(CASE WHEN Status = 1 THEN 1 ELSE 0 END) as [Attempted],
SUM(CASE WHEN Status = 0 THEN 1 ELSE 0 END) as [NotAttempted],
SUM(CASE WHEN Status = 2 THEN 1 ELSE 0 END) as [Completed]
FROM @YourTable
GROUP BY ScheduleuserId
[/code]
----------------------------------------------
Try to learn something about everything and everything about something. - Thomas Henry Huxley
:w00t:
Posting Best Practices[/url]
Numbers / Tally Tables[/url]
April 27, 2009 at 9:16 pm
Thanks Christopher Stobbs
April 27, 2009 at 10:50 pm
ningaraju.n (4/27/2009)
If u use the Sub query its killing the Performance.. So..;-)
Not always true if the subqueries only use "=". Anyway, looks like Chris get's the hat-trick on this one. Nicely done, Chris.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply