| Author |
Topic |
|
swatib
Posting Yak Master
173 Posts |
Posted - 2006-07-26 : 05:56:04
|
I have 2 tables - WorkScope and Activity* Table : WorkScope *SiteID - ActivityID768 - 101 768 - 102768 - 103* Table : Activity *SiteID - ActivityID - Status768 - 101 - Not Completed768 - 102 - CompletedNow I want the following resultSiteID - Status768 - Not CompletedBecause SiteId 768 has 3 activities and those all 3 are not completed or entered in the Activity table.May be the solution is simple, help meNjoy Life  |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2006-07-26 : 06:09:43
|
| >> all 3 are not completed or entered in the Activity tableHow do you knkow a task is not completed if it's not enetered in the activity table?looks to me from this that if it's in the activity table you want it and if it's not you want it soselect distinct SiteID from WorkScope==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
swatib
Posting Yak Master
173 Posts |
Posted - 2006-07-26 : 06:20:46
|
See there is a logic that for an activity, its start & end dates are entered then that Activity's status becomes 'Completed' in Activity tableBut for e.g 103 activity is not partially entered or fully entered and thats why its not in Activity table for that SiteIDNjoy Life |
 |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2006-07-26 : 06:23:25
|
This is what I got (I added a few mor activities and workscopes to test it better):DECLARE @WorkScope table (SiteID int, ActivityID int)DECLARE @Activity table (SiteID int, ActivityID int, Status varchar(20))INSERT INTO @WorkScopeSELECT 768, 101 UNION ALL SELECT 768, 102 UNION ALL SELECT 768, 103 UNION ALL SELECT 770, 103 UNION ALL SELECT 780, 500INSERT INTO @ActivitySELECT 768, 101, 'Not Completed' UNION ALL SELECT 768, 102, 'Completed' UNION ALLSELECT 780, 500, 'Completed' SELECT * FROM @WorkScopeSELECT * FROM @ActivitySELECT dt1.SiteID, Activities, Completed, Status = CASE WHEN ISNULL(Activities, 0) <> ISNULL(Completed, 0) THEN 'Not completed' ELSE 'Completed' ENDFROM (SELECT SiteID, Count(*) AS Activities FROM @WorkScope GROUP BY SiteID) AS dt1 LEFT OUTER JOIN (SELECT SiteID, Count(*) AS Completed FROM @Activity WHERE Status = 'Completed' GROUP BY SiteID) AS dt2 ON dt1.SiteID = dt2.SiteID If you have more than one activity with the same SiteID in the @Activity table it wount work but if you need it I'm sure I can find a workaround.--Lumbago"Real programmers don't document, if it was hard to write it should be hard to understand" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-07-26 : 06:25:56
|
This is the most simplest way to accomplish your task, I think  -- Prepare test datadeclare @WorkScope table (SiteID int, ActivityID int)insert @workscopeselect 768, 101 union allselect 768, 102 union allselect 768, 103declare @Activity table (SiteID int, ActivityID int, Status varchar(20))insert @activityselect 768, 101, 'Not Completed' union allselect 768, 102, 'Completed'-- Do the work!select SiteID, -- Select all Sites having status 'Not Completed' Statusfrom @activitywhere status = 'not completed'unionselect w.SiteID, -- Select all Sites not having all activities 'Not completed'from @workscope wleft join @activity a on a.siteid = w.siteid and a.activityid = w.activityidgroup by w.siteidhaving count(distinct w.activityid) <> count(distinct a.activityid) This is only an error list. You haven't told what to do if all activities for a site is status 'Completed'...Peter LarssonHelsingborg, Sweden |
 |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2006-07-26 : 06:27:17
|
Heh, I belive we did the same thing...didn't we? --Lumbago"Real programmers don't document, if it was hard to write it should be hard to understand" |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2006-07-26 : 06:27:46
|
| You mean you want those SiteIDs whose tasks are not all complleted in Activity?select t.SiteIDfrom (select SiteID, num = count(*) from WorkScope group by SiteID) tleft join (select SiteID, num = count(*) from Activity where Status = 'Completed' group by SiteID) t2where t.num <> coalesce(t2.num,0)==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-07-26 : 06:32:42
|
quote: Originally posted by nr select t.SiteIDfrom (select SiteID, num = count(*) from WorkScope group by SiteID) tleft join (select SiteID, num = count(*) from Activity where Status = 'Completed' group by SiteID) t2where t.num <> coalesce(t2.num,0)
Nice solution!Just add on t2.siteid = t.siteid after t2 in the left join, then you're done.Peter LarssonHelsingborg, Sweden |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2006-07-26 : 06:36:41
|
| oops - thanksselect t.SiteIDfrom (select SiteID, num = count(*) from WorkScope group by SiteID) tleft join (select SiteID, num = count(*) from Activity where Status = 'Completed' group by SiteID) t2on t2.SiteID = t.SiteIDwhere t.num <> coalesce(t2.num,0)==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-07-26 : 07:52:27
|
I think I'd do one of these...select distinct SiteID, 'Not Completed' as Statusfrom workscope awhere not exists (select * from activity where ActivityID = a.ActivityID and Status = 'Completed')select distinct a.SiteID, 'Not Completed' as Statusfrom workscope a left outer join activity b on a.ActivityID = b.ActivityIDwhere not Status = 'Completed' But then I think I must be missing something because the other suggestions seem more complicated.Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
mahesh_bote
Constraint Violating Yak Guru
298 Posts |
Posted - 2006-07-26 : 08:46:45
|
| hoeps it will work.Select W.SiteId ,A.Status From WorkScope As W ,Activity As AWhere W.SiteId = A.SiteId And A.ActivityId = 101BMahesh |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2006-07-26 : 09:23:41
|
| RyanRandall's should include the SiteID in the join select distinct a.SiteID, 'Not Completed' as Statusfrom workscope a left outer join activity b on a.SiteID = b.SiteIDand a.ActivityID = b.ActivityIDand Status = 'Completed'where b.ActivityID is null==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
swatib
Posting Yak Master
173 Posts |
Posted - 2006-07-27 : 04:29:36
|
Thanks all of uNjoy Life |
 |
|
|
|