| Author |
Topic |
|
dbaz
Starting Member
5 Posts |
Posted - 2005-10-13 : 00:44:29
|
| Dear All.I have below tables:[Docs]DocID DocName1 Doc12 Doc23 Doc3....[DocTasks]DocID TaskID1 11 22 12 32 23 23 14 54 24 14 35 35 2....[Tasks]TaskID TaskName1 Task12 Task23 Task34 Task4...Now I want to get TaskID-s from DocTasks table, where each that TaskID belongs to all DocID-s in DocTasks table. For example:TaskID 1 belongs to DocIDs(1, 2, 3, 4) -> This TaskID is not neededTaskID 2 belongs to ALL DocIDs(1, 2, 3, 4, 5) -> This TaskID is neededTaskID 3 belongs to DocIDs(2, 4, 5) -> This TaskID is not needed. So on..So, Query must return only TaskID=2, because only this TaskID belongs to All DocIDs.Thanks in advance. |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-10-13 : 01:30:53
|
| Try thisSelect T2.TaskID from DocTasks T1 inner join Tasks T2 on T1.TaskID=T2.TaskIDwhere T1.DocID in (1,2,3,4,5)--you need to include other DocIds if anygroup by T2.DocID having count(*)=5 --Change this if you have manyMadhivananFailing to plan is Planning to fail |
 |
|
|
Hippi
Yak Posting Veteran
63 Posts |
Posted - 2005-10-13 : 01:41:52
|
Not sure if reading your question right but how aboutselect TaskId from DocTasks group by TaskIdhaving count(distinct DocId)=5Hippi |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-10-13 : 02:16:52
|
quote: Originally posted by Hippi Not sure if reading your question right but how aboutselect TaskId from DocTasks group by TaskIdhaving count(distinct DocId)=5Hippi
Seems to be Good Idea MadhivananFailing to plan is Planning to fail |
 |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2005-10-13 : 02:30:13
|
| little modification to the hippi's code.. :-)).. select TaskId From DocTasks group by TaskIdhaving count(distinct DocId)>=(Select Count(1) From Docs)hope this makes any sense. :-)Complicated things can be done by simple thinking |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-10-13 : 04:47:26
|
| What is the need for >=(Select Count(1) From Docs)?MadhivananFailing to plan is Planning to fail |
 |
|
|
dbaz
Starting Member
5 Posts |
Posted - 2005-10-13 : 05:34:22
|
| Hey GUYS!!!What are you doing?Lets forget about Docs and Task table.I habe table as below:[DocTasks]DocID TaskID1 11 22 12 32 23 23 14 54 24 14 35 35 26 16 46 27 17 27 38 58 29 49 59 2Ok.What are the dictinct DocIDs in this table:1, 2, 3, 4, 5, 6, 7, 8, 9What are the dictinct TaskIDs in this table:1, 2, 3, 4, 5.Now I want the TaskIDs, which it has been assigned to all the above distinct DocIDs. For example: TaskID = 2. Because there are rows as below[DocTasks]DocID TaskID1 22 23 24 25 26 27 28 29 2So, All DocIDs found.But TaskID = 3. This is not what we want, Because there are rows as below[DocTasks]DocID TaskID2 34 35 37 3So, there is not DocIDs(1, 3, 6, 8, 9).Thanks in advance. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-10-13 : 05:45:36
|
| Did you try the queries suggested?MadhivananFailing to plan is Planning to fail |
 |
|
|
dbaz
Starting Member
5 Posts |
Posted - 2005-10-13 : 06:11:24
|
| Yes. Select T2.TaskID from DocTasks T1 inner join Tasks T2 on T1.TaskID=T2.TaskIDwhere T1.DocID in (1,2,3,4,5)--you need to include other DocIds if anygroup by T2.DocID having count(*)=5 --Change this if you have manyResult:Server: Msg 207, Level 16, State 3, Line 1Invalid column name 'DocID'T2 is alias to Tasks, but it has no DocID FieldAny idea? |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-10-13 : 06:12:51
|
| Use T1.DocID and try itMadhivananFailing to plan is Planning to fail |
 |
|
|
dbaz
Starting Member
5 Posts |
Posted - 2005-10-13 : 07:23:54
|
| Server: Msg 8120, Level 16, State 1, Line 1Column 'T2.TaskID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-10-13 : 07:34:28
|
| Use Hippi's queryselect TaskId from DocTasks group by TaskIdhaving count(distinct DocId)=9MadhivananFailing to plan is Planning to fail |
 |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2005-10-13 : 08:03:05
|
[quote]Originally posted by madhivanan What is the need for >=(Select Count(1) From Docs)?I have given that clause, according to what i thought the logic goes like this Get the task id from the doctask table and group by it and then in the having clause check with the total number of doc id present .. so instead of hardcodding the number of docid in the docs table i gave (count(1) query.. that what i thought but i guess it should be somthing like this.. select TaskId From DocTasks group by TaskIdhaving count(distinct DocId)>=(Select Count(Distinct DocID) From Docs)Hope its work out what he wants. Complicated things can be done by simple thinking |
 |
|
|
|